In this part of the tutorial, we will learn to deploy the MyFirstEthicContract.sol contract and fetch the latest price from Ethic network.
This part of the tutorial will conver the following:
Deploy the contract on OP Sepolia testnet.
Interact with the contract from the command line.
Update and fetch the price from the contract using Ethic-evm-js.
Deploy the contract
Now, let's deploy the contract to a test network. We will use Optimism Sepolia, but any EVM network will work. We will also use the Foundry command line tools -- specifically cast -- to perform many of the necessary operations.
First, create a new wallet:
cast wallet new
This command will generate a new Ethereum keypair, producing output similar to the following. Note that the address and private key will be different hexadecimal values.
Successfully created new keypair.
Address: 0xB806824fdA4b2b6631e9B87a86d42C9dfd04D129
Private key: 0x0d510c72fd2279155c717eb433ae598a83cfb34b09c2ada86bc424b481082023
We will export the values from the command above as environment variables to simplify the commands below. We will also export the RPC URL of the network. Run the following commands in your shell substituting the address and private key in the indicated places:
export ADDRESS=<address from above>
export PRIVATE_KEY=<your private key from above>
export RPC_URL="https://sepolia.optimism.io"
Next, use the Superchain Faucet to claim some test Sepolia ETH. Paste the address from the command above into the faucet to get your ETH. You can verify that the ETH has arrived in your wallet by running the command cast balance $ADDRESS -r $RPC_URL -e
The final step before deploying is to get the arguments for the contract's constructor: the Ethic contract address for Optimism Sepolia and the price feed id for ETH/USD. We will also export these values as environment variables for convenience:
This command should produce output along the following lines:
[ā ] Compiling...
No files changed, compilation skipped
Deployer: 0xB806824fdA4b2b6631e9B87a86d42C9dfd04D129
Deployed to: 0xcb952c0D1E19E4cA5660Cc2F5E0284690bDd2fE9
Transaction hash: 0x3b933816760d6a0070503ab251de0a21618e7634ca7c6c49aea75ff640d470db
The contract is deployed! Let's save the contract address as another environment variable for later use:
export DEPLOYMENT_ADDRESS=<deployed to address from above>
Interact from command line
Let's try out the deployed contract. In order to do so, we will need to get a price update to pass to the updateAndMint function. We can fetch this update from Hermes:
This command will fetch a fresh price update from Hermes and save it in the file price_update.txt. The update is a binary payload represented as a hexadecimal string.
We can check the content of the price update by running cat price_update.txt
Note that this command uses the environment variables defined in the sections above. It passes 0.0005 ETH to the updateAndMint function to cover the minting fee. It also passes the price update we got from Hermes, wrapped in an array to match the function signature. The command above should produce output along the following lines:
You can see the transaction using the Optimism Sepolia Explorer by pasting the transactionHash from your result into the search box. If you get an error in this step, don't worry -- these are easily fixed. There are two possible errors when running this command. The first error looks like this:
The error code 0x025dbdd4 represents the InsufficientFee error. This means that 0.0005 ETH was not sufficient to cover the minting fee. Try increasing the fee to fix the problem.
The error codeĀ 0x19abf40eĀ represents theĀ StalePriceĀ error that we saw before. This means that theĀ price_update.txtĀ was too old to be used by the contract. Simply re-run theĀ curlĀ command above to get a newer price update, then retry sending the transaction.
Interact from Typescript
As a final task, let's interact with our deployed contract from Typescript. First create a new directory calledĀ appĀ that is adjacent toĀ contractsĀ and create a newĀ npmĀ project:
cd .. mkdir app cd app npm init -y npm install --save typescript ts-node viem @Ethic/Ethic-evm-js
Then open src/mintNft.ts and paste in the following content:
Notice: This abi variable which represents the interface of our contract and can be autogenerated by running forge inspect MyFirstEthicContract abi in the contracts directory.
The run function does two different things:
First, it instantiates an interface to our contract using the viem library.
Second, it uses EvmPriceServiceConnection from @Ethic/Ethic-evm-js to retrieve
It works! Again, you can look up the transaction hash in the Optimism Sepolia explorer to check that the transaction landed.
Congratulations! You've built your first app using Ethic price feeds.
Conclusion
In this tutorial, we created a Solidity contract that updates and reads Ethic prices, tested the contract locally, then deployed the contract and interacted with it both via the command line and Ethic-evm-js sdk.