Chapter 9 of 9

Putting it all online on the test network

Goal. Publish the two contracts on Polygon Amoy, verify them on Polygonscan and check that everything works.

So far you have worked locally. Deploying means sending the compiled code to the network: the contract gets an address and from then on is visible to everyone. You do it with Hardhat, a development environment for contracts.

Analogy. It is like registering a business with the chamber of commerce: the project stays the same, but it now has an official number anyone can look up.
// hardhat.config.ts (extract)
networks: {
  amoy: {
    url: "https://polygon-amoy-bor-rpc.publicnode.com",
    chainId: 80002,
    accounts: [process.env.DEPLOYER_PRIVATE_KEY],
  },
},

// scripts/deploy.ts (extract)
const oracle = await ethers.deployContract("FuelPriceOracle", [deployer.address]);
await oracle.waitForDeployment();
const token = await ethers.deployContract("FuelToken", [await oracle.getAddress()]);
await token.waitForDeployment();
await oracle.grantRole(await oracle.UPDATER_ROLE(), UPDATER_ADDRESS);

Checklist

  1. Create two test keys: one to deploy, one to update prices. Never reuse real keys.
  2. Get test POL for both from the chapter 3 faucets.
  3. Put the variables in the .env file (never shared or uploaded to GitHub): the deployer key, the updater address, and a Polygonscan API key for step 6.
  4. Deploy with npx hardhat run scripts/deploy.ts --network amoy and note the two addresses.
  5. Configure the chapter 7 program with the oracle address and the updater key, then run it once.
  6. Verify on Polygonscan (amoy.polygonscan.com): verification publishes the source code next to the contract, so anyone can read what is really running.
  7. Check: the oracle shows today's prices and date, and isFresh(259200) answers true (259,200 seconds are 3 days).
  8. Try a purchase of 0.1 POL from the web page and look at the transaction in the explorer.

If a purchase fails with "old price", the oracle is not fresh: run the chapter 7 program again.

One last tip: do a full run on Hardhat's local network first (npx hardhat node), where mistakes cost nothing and everything is fast.

What we built

The chapter 7 program is the project backend and the chapter 8 page is its frontend. These are test-network contracts: tokens and coins have no value.

Source code. The full source code is available on request.

Try it

Deploy only the oracle to Amoy, open its address on Polygonscan and find the "Contract" tab. Then call isFresh from Remix or the explorer: before the first update it must answer false.

What you learned

Going deeper