Deploying a Move Smart Contract on Moved Network

In this guide, we will walk through deploying a simple Move smart contract on Moved Network. We'll use a basic counter example, build the contract using Moved SDK, deploy it with Hardhat.

1. Setting Up the Hardhat Environment

  1. Install Hardhat and required dependencies:

    npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox @moved/hardhat-plugin
    
  2. Initialize a Hardhat project:

    npx hardhat init
    

    Follow steps with the Create an empty hardhat.config.js option.

  3. Add the Moved plugin and network description to hardhat.config.js:

    require("@moved/hardhat-plugin");
    
    module.exports = {
      defaultNetwork: "devnet",
      networks: {
        devnet: {
          url: "https://devnet.moved.network",
          accounts: ["YOUR_PRIVATE_KEY"]
        }
      }
    };
    

    Make sure to put in your private key in place of YOUR_PRIVATE_KEY. You can obtain this key from MetaMask. We recommend creating a burner account for testing.

    Also please follow the Introduction step to give your account some free test tokens to pay for transaction fees.

2. Creating a Move Smart Contract

We'll start with a simple counter contract that increments a stored value. First, create a contracts/counter folder to keep our Move contract project.

mkdir contracts contracts/counter
cd contracts/counter

Inside this folder run the following command to create a simple counter project:

aptos move init --name counter

Inside the contracts/sources/ folder create a file named counter.move and paste the following in the file:

module example::Counter {
    use std::signer;

    struct Counter has key, store {
        value: u64,
    }

    public entry fun initialize(account: &signer) {
        move_to(account, Counter { value: 0 });
    }

    public entry fun increment(account: &signer) acquires Counter {
        let counter = borrow_global_mut<Counter>(signer::address_of(account));
        counter.value = counter.value + 1;
    }

    public fun get(account: address): u64 acquires Counter {
        let counter = borrow_global<Counter>(account);
        counter.value
    }
}

Next we'll define our example address in the Move.toml file. So, add the address under the [addresses] list. In the same file change the rev of the Aptos Framework dependency to the latest supported version.

[addresses]
example = "ACCOUNT_ADDRESS"

[dependencies.AptosFramework]
git = "https://github.com/aptos-labs/aptos-framework.git"
rev = "aptos-release-v1.14"
subdir = "aptos-framework"

Finally let's compile the Move contract:

npx hardhat compile

3. Deploying the Contract

  1. Create a script to deploy the generated contract artifact. Create a file under scripts/deploy.js with the following code. Make sure the scripts folder is inside the Hardhat folder and not the counter project folder.
import { ethers } from 'hardhat';

async function main() {
    const [deployer] = await ethers.getSigners();
    console.log('Deploying contracts for the account:', deployer.address);

    const Counter = await ethers.getContractFactory('counter');
    const counter = await Counter.deploy();
    await counter.waitForDeployment();
    const moduleAddress = await counter.getAddress();
    console.log(`Counter is deployed to: ${deployer.address}::counter`);
}

main()
  .then(() => process.exit(0))
  .catch((err) => {
    console.error(err);
    process.exit(1);
  });

  1. Deploy the contract using a single command, Hardhat takes care of the rest:

    npx hardhat run scripts/deploy.js
    
  2. After deployment, locate your contract on the Moved Network block explorer. Search the deployer address to verify its status and details.

With these steps, you've successfully deployed and interacted with a Move smart contract on Moved Network!