One way to deploy smart contracts on Nibiru EVM is by using the ethers npm library. You'll need a mnemonic phrase and the EVM RPC endpoint of your Nibiru chain (e.g., localnet is 127.0.0.1:8545(opens new window)).
Assuming you have a simple smart contract with a getValue function:
Copy
// SimpleStorage.sol
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private value;// Function to set a new value. This modifies the blockchain state.functionsetValue(uint256 newValue)public{
value = newValue;}// Function to get the current value. This does not modify the blockchain state.functiongetValue()public view returns(uint256){return value;}}
Here’s how you can call the getValue function:
Copy
import{ ethers }from"ethers";import SimpleStorageABI from"./SimpleStorage.json";// Assuming ABI is in JSON formatimport{ config }from"dotenv";// Load environment variables from .env fileconfig();// Set up the RPC endpoint and mnemonic phrase from environment variablesconst rpcEndpoint = process.env.JSON_RPC_ENDPOINT;const mnemonic = process.env.MNEMONIC;// Connect to the Ethereum network using the RPC endpointconst provider =newethers.JsonRpcProvider(rpcEndpoint);// Create a wallet instance using the mnemonic phraseconst wallet = ethers.Wallet.fromMnemonic(mnemonic);// Connect the wallet to the providerconst account = wallet.connect(provider);// Address of the deployed SimpleStorage contractconst contractAddress ="your_contract_address_here";// Create a contract instance using the contract address, ABI, and providerconst simpleStorage =newethers.Contract(contractAddress, SimpleStorageABI, provider);// Function to call the read-only getValue function of the contractconstgetValue=async()=>{// Call the getValue functionconst value =await simpleStorage.getValue();// Log the returned value
console.log(`Stored value: ${value}`);};// Execute the getValue functiongetValue();
To call a function that changes the contract’s state (e.g., setValue):
Copy
// Function to call the state-changing setValue function of the contractconstsetValue=async(newValue)=>{// Connect the contract instance to the wallet for signing transactionsconst contractWithSigner = simpleStorage.connect(account);// Call the setValue function with the new valueconst txResponse =await contractWithSigner.setValue(newValue);// Wait for the transaction to be minedawait txResponse.wait();// Log the new value that was set
console.log(`New value set: ${newValue}`);};// Execute the setValue function with a new valuesetValue(42);
Copy
pragma solidity ^0.8.24;import{ERC20}from"@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract FunToken is ERC20{// Define the initial supply of FunToken: 1,000,000 tokens
uint256 constant initialSupply =1000000*(10**18);// Constructor will be called on contract creationconstructor()ERC20("FunToken","FUN"){// Mint the initial supply to the deployer's address_mint(msg.sender, initialSupply);}}
After deploying your contract via the deploy function above,
Copy
import{ deployContract }from"./deploy";// Import the deploy functionimport{ ethers }from"ethers";import FunTokenCompiled from"./FunTokenCompiled.json";// Assuming ABI and bytecode are in JSON formatimport{ config }from"dotenv";// Load environment variables from .env fileconfig();// Set up the RPC endpoint and mnemonic phrase from environment variablesconst rpcEndpoint = process.env.JSON_RPC_ENDPOINT;const mnemonic = process.env.MNEMONIC;// Connect to the Ethereum network using the RPC endpointconst provider =newethers.JsonRpcProvider(rpcEndpoint);// Create a wallet instance using the mnemonic phraseconst wallet = ethers.Wallet.fromMnemonic(mnemonic);// Connect the wallet to the providerconst account = wallet.connect(provider);// Main function to deploy and interact with the FunToken contractconstmain=async()=>{// Deploy the FunToken contract and get the contract instanceconst contract =(awaitdeployContract("FunTokenCompiled.json"))as ethers.Contract;// Get the address of the deployed contractconst contractAddress = contract.address;
console.log(`FunToken deployed at: ${contractAddress}`);// Generate a random address to receive tokensconst shrimpAddress = ethers.Wallet.createRandom().address;// Define the amount of tokens to send (1,000 tokens)const amountToSend = ethers.utils.parseUnits("1000",18);// Get the initial balances of the owner and the recipientlet ownerBalance =await contract.balanceOf(account.address);let shrimpBalance =await contract.balanceOf(shrimpAddress);// Log the initial balances
console.log(`Owner balance before: ${ethers.utils.formatUnits(ownerBalance,18)}`);
console.log(`Shrimp balance before: ${ethers.utils.formatUnits(shrimpBalance,18)}`);// Connect the contract instance to the wallet for signing transactionsconst contractWithSigner = contract.connect(account);// Transfer tokens from the owner to the recipientconst tx =await contractWithSigner.transfer(shrimpAddress, amountToSend);// Wait for the transaction to be minedawait tx.wait();// Get the updated balances of the owner and the recipient
ownerBalance =await contract.balanceOf(account.address);
shrimpBalance =await contract.balanceOf(shrimpAddress);// Log the updated balances
console.log(`Owner balance after: ${ethers.utils.formatUnits(ownerBalance,18)}`);
console.log(`Shrimp balance after: ${ethers.utils.formatUnits(shrimpBalance,18)}`);};// Execute the main functionmain();