Deploying the Contract¶
To deploy the contract we can go two different routes:
- We can use the
forge create ...
command, which is detailed here: https://book.getfoundry.sh/forge/deploying - Deployment using a Foundry Script, which is detailed here: https://book.getfoundry.sh/tutorials/solidity-scripting
In our case, I want to create a script, because I personally think its more re-usable. Both things should work equally fine though.
Furthermore, we will need three things to deploy the contract:
- A private key where the address to that private key is sufficiently funded
- An RPC endpoint for Polygon to interact with the blockchain
- Somewhat optional: An polygonscan API Key to verify the contracts
Let's start with the script. Create a new file script/SilverNft.s.sol
with the following content:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Script} from "forge-std/Script.sol";
import {SilverOunce} from "../src/SilverNft.sol";
contract MyScript is Script {
function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
new SilverOunce(
msg.sender,
0x110016975ce40F7cB1Dae96a9da51ae1037db3c4, //data provider
0xfc13Eca5251CDbC1ED703da32c8E3038Da227E24 //oracle entrypoint
);
vm.stopBroadcast();
}
}
Make sure you get the provider and Oracle entrypoint from https://oracle.morpher.com/feeds, because these might change at some point. Before deploying your own contracts, make sure you have the right addresses!
For the Private Key I love to use a tool called vanityeth. Use at your own risk, it just works for me. You can use any private key if you want. Transfer a little bit of POL into the address so deployment works (if you use a test-chain, use that corresponding gas token)
For the API key, go to polygonscan.com then signup/login and copy the API key from the API Key Page
Create a new .env
file (in the contracts folder) and fill in the blanks:
RPC_URL=https://...
PRIVATE_KEY=0x123123...
ADDRESS=0x1234...
ETHERSCAN_API_KEY=FROMPOLYGONSCAN
After that its time to deploy the token. Run the following command:
source .env
forge script --chain-id 137 ./script/SilverNft.s.sol --rpc-url ${RPC_URL} --broadcast --verify --sender ${ADDRESS} -vvvv
you will see an output like this:
And then right away it will try to verify the contract:
Hint
If, for whatever reason, some of your transactions do not work and the deployment fails in the middle, you can resume the deployment using --resume instead of --broadcast:
forge script --chain-id 137 ./script/SilverNft.s.sol --rpc-url ${RPC_URL} --resume --verify --sender ${ADDRESS} -vvvv
The contract is now deployed, specifically in our screenshot its here: https://polygonscan.com/address/0x8d56087058f75fb8cb402890856962d9ef649188, you can also see the code is verified.
Before interacting with the code from our NextJS app, let's commit the changes to Git:
git add . && git commit -a -m "Added script to deploy the SilverNFT contract"
Next up, lets see how we can interact with our contract from NextJS