Skip to content

Deploying the Contract

To deploy the contract we can go two different routes:

  1. We can use the forge create ... command, which is detailed here: https://book.getfoundry.sh/forge/deploying
  2. 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:

  1. A private key where the address to that private key is sufficiently funded
  2. An RPC endpoint for Polygon to interact with the blockchain
  3. 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:

Deploy Contract using foundry script
Deploy Contract using foundry script

And then right away it will try to verify the contract:

Verifying contract on chain
Verifying contract on chain

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