Your first EVM Smart Contract
Your First Smart Contract
Overview
In this guide, we'll create, deploy, and interact with a simple smart contract on UOMI's EVM network using Hardhat. We'll build a basic "Counter" contract that can increment and retrieve a number.
Prerequisites
Before starting, ensure you have:
Node.js installed
A code editor (VS Code recommended)
A MetaMask wallet with some test tokens
Basic knowledge of Solidity
Project Setup
Create a new directory and initialize the project:
mkdir counter-contract
cd counter-contract
npm init -y
Install required dependencies:
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox
Create a Hardhat project:
npx hardhat init
Select "Create a JavaScript project" when prompted.
Writing the Contract
Create a new file contracts/Counter.sol
:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract Counter {
uint256 private count;
// Event to emit when the counter changes
event CountUpdated(uint256 newCount);
constructor() {
count = 0;
}
function increment() public {
count += 1;
emit CountUpdated(count);
}
function getCount() public view returns (uint256) {
return count;
}
}
Deployment Script
Create/modify scripts/deploy.js
:
async function main() {
const Counter = await ethers.getContractFactory("Counter");
const counter = await Counter.deploy();
await counter.waitForDeployment();
console.log("Counter deployed to:", await counter.getAddress());
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Test the Contract
Create test/Counter.js
:
const { expect } = require("chai");
describe("Counter", function () {
let counter;
beforeEach(async function () {
const Counter = await ethers.getContractFactory("Counter");
counter = await Counter.deploy();
});
it("Should start with count of 0", async function () {
expect(await counter.getCount()).to.equal(0);
});
it("Should increment count", async function () {
await counter.increment();
expect(await counter.getCount()).to.equal(1);
});
});
Run the tests:
npx hardhat test
Deploy to Testnet
Configure your network in
hardhat.config.js
:
require("@nomicfoundation/hardhat-toolbox");
// Go to https://finney.uomi.ai and replace this with your own RPC URL
const FINNEY_RPC_URL = "https://finney.uomi.ai";
// Replace this private key with your own
// To export your private key from Metamask, go to Account Details > Export Private Key
const PRIVATE_KEY = "YOUR-METAMASK-PRIVATE-KEY";
module.exports = {
solidity: "0.8.19",
networks: {
finney: {
url: FINNEY_RPC_URL,
accounts: [PRIVATE_KEY],
},
},
};
Deploy to Finney testnet:
npx hardhat run scripts/deploy.js --network finney
Interact with Your Contract
After deployment, you can interact with your contract using:
The Hardhat console:
npx hardhat console --network finney
Example interactions:
// Get the contract
const Counter = await ethers.getContractFactory("Counter");
const counter = await Counter.attach("YOUR-DEPLOYED-CONTRACT-ADDRESS");
// Get the current count
const count = await counter.getCount();
console.log("Current count:", count);
// Increment the counter
await counter.increment();
Next Steps
Now that you've deployed your first contract, you can:
Add more functionality to your contract
Create a frontend to interact with it
Learn about contract security and best practices
Explore more complex smart contract patterns
Common Issues and Solutions
⚠️ Common Problems
Transaction Failed: Make sure you have enough tokens for gas
Contract Not Found: Verify the contract address is correct
Network Issues: Ensure you're connected to the correct network
Resources
Last updated