UmiUmi

Deploying EVM Smart Contract

In this guide, we will walk through deploying a simple Solidity smart contract on Umi. We'll use a basic counter example, build the contract and deploy it with Hardhat.

1. Setting Up the Hardhat Environment

  1. Install Hardhat and required dependencies:

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

    npx hardhat init

    Follow steps with the Create a TypeScript project option.

  3. Add the Umi plugin and network description to hardhat.config.ts:

    import { HardhatUserConfig } from "hardhat/config";
    import "@nomicfoundation/hardhat-toolbox";
    import '@moved/hardhat-plugin';
    
    const config: HardhatUserConfig = {
      solidity: "0.8.28",
      defaultNetwork: "devnet",
      networks: {
        devnet: {
          url: "https://devnet.uminetwork.com",
          accounts: ["YOUR_PRIVATE_KEY"]
        }
      }
    };
    
    export default config;

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

2. Creating a Solidty Smart Contract

We'll start with a simple counter contract that increments a stored value. First, replace the existing Lock project with the Counter project.

mv contracts/Lock.sol contracts/Counter.sol

Change the content of the Counter.sol Solidity file with:

pragma solidity 0.8.28;

contract Counter{

    uint public count;

    function increment() public {
        count++;
    }
}

Finally let's compile the Solidity contract, from the top project folder:

npx hardhat compile

3. Deploying the Contract

  1. Create a script to deploy the generated contract artifact. Create a file under scripts/deploy.ts with the following code. Make sure the scripts folder is inside the Hardhat folder and not the contracts project folder.

    import { ethers } from 'hardhat';
    
    async function main() {
      const Counter = await ethers.getContractFactory('Counter');
      const counter = await Counter.deploy();
      await counter.waitForDeployment();
      // Get the generated contract address from the transaction receipt, don't use `await counter.getAddress()`
      const receipt = await ethers.provider.getTransactionReceipt(counter.deploymentTransaction()?.hash!);
      console.log('Counter is deployed to:', receipt?.contractAddress);
    }
    
    main()
      .then(() => process.exit(0))
      .catch((err) => {
        console.error(err);
        process.exit(1);
      });
  2. Ensure your wallet has sufficient test ETH for the address used above. Get tokens from the Faucet.

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

    npx hardhat run scripts/deploy.ts

    The first time you run this command, it will fail. However, Hardhat will provide modified code that includes the bytecode. To proceed:

    1. Copy the printed bytecode.
    2. Open artifacts/contracts/Counter.sol/Counter.json.
    3. Replace the bytecode value with the copied code.

    Re-run the deployment command. This time, the contract should deploy successfully. Take note of the contract address printed in the output.

  4. After deployment, locate your contract on the Umi block explorer. Search the deployer address to verify its status and details.

With these steps, you've successfully deployed and now checkout Use Contract to interact with your Solidity smart contract!

On this page