Chapter 9 of 9
Putting it all online on the test network
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.
// 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);
networks.amoytells Hardhat where to publish: RPC address and Chain ID 80002.accountsuses the deployer key taken from the environment: a fresh test key, never a real one.deployContractpublishes the oracle first, then the token, passing it the oracle's address.- The last line gives the updater address (the chapter 7 program) the role that lets it write prices.
Checklist
- Create two test keys: one to deploy, one to update prices. Never reuse real keys.
- Get test POL for both from the chapter 3 faucets.
- Put the variables in the
.envfile (never shared or uploaded to GitHub): the deployer key, the updater address, and a Polygonscan API key for step 6. - Deploy with
npx hardhat run scripts/deploy.ts --network amoyand note the two addresses. - Configure the chapter 7 program with the oracle address and the updater key, then run it once.
- Verify on Polygonscan (
amoy.polygonscan.com): verification publishes the source code next to the contract, so anyone can read what is really running. - Check: the oracle shows today's prices and date, and
isFresh(259200)answers true (259,200 seconds are 3 days). - 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
FuelPriceOracle.solon Polygon Amoy (Chain ID 80002):0xf30D07696476e97103730BE0dFb55045945409D8FuelToken.solon Polygon Amoy (Chain ID 80002):0x87eAE9eF4A2dD56D04b4798bA9Bc7B777E70c9ac
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
- Deploying gives the contracts a public address on the network.
- Test keys live in an
.envfile that is never shared. - Verifying on Polygonscan makes the running code readable by everyone.