Deploy a smart contract
Overview
This tutorial shows how to deploy a smart contract on the Saigon testnet and interact with it using the Ronin app. Additionally, the tutorial describes how to verify a smart contract using the Ronin Sourcify service.
Before you start
Before you start the tutorial, you should have the following:
- A Hardhat project. If you are not familiar with Hardhat, follow this tutorial.
- npm or yarn. This tutorial uses yarn.
Step 1. Install hardhat-deploy
hardhat-deploy is a Hardhat plugin used for deploying smart contracts on any network.
- Install
hardhat-deploy
in your Hardhat project:
yarn add --dev hardhat-deploy
- Enable
hardhat-deploy
by adding this line to yourhardhat.config.js
file:
require('hardhat-deploy')
Step 2. Set up the environments
To deploy to the Ronin mainnet or Saigon testnet environments, you need to provide network information to Hardhat.
In the hardhat.config.js
file, add a networks
object with the following code:
module.exports = {
solidity: "0.8.18",
networks: {
ronin: {
chainId: 2020,
url: 'https://api.roninchain.com/rpc',
},
saigon: {
chainId: 2021,
url: 'https://saigon-testnet.roninchain.com/rpc',
},
}
};
Step 3. Set up a deployer account
To deploy the smart contract, you need a deployer account with some RON to cover transaction gas fees.
- In the
hardhat.config.js
file, add anamedAccounts
object with the following code:
module.exports = {
solidity: "0.8.18",
namedAccounts: {
deployer: 'privatekey://<your deployer private key>',
},
networks: {
ronin: {
chainId: 2020,
url: 'https://api.roninchain.com/rpc',
},
saigon: {
chainId: 2021,
url: 'https://saigon-testnet.roninchain.com/rpc',
},
}
};
- Retrieve the private key: open the Ronin Wallet browser extension, go to Account > Manage, select the account from which you deploy, and then click View private key.
- Specify the private key in the
namedAccounts
object.
Top up your Saigon testnet deployer account using the RON faucet.
Step 4. Write a deployment script
- In your project's root directory, create a sub-directory for a deployment script:
mkdir deploy
- Create a file called
1_deploy_token.js
and paste the deployment script's code:
const func = async function (hre) {
const { deployments, getNamedAccounts } = hre;
const { deploy } = deployments;
const { deployer } = await getNamedAccounts();
await deploy('Token', {
from: deployer,
args: [],
log: true,
});
};
module.exports = func;
func.tags = ['Token'];
Step 5. Deploy to the Saigon testnet
Now that you are ready to deploy, run the following command to deploy the smart contract:
yarn hardhat deploy --network saigon
You should see output with the deployed address similar to this:
deploying "Token" (tx: 0x6a3ecf86a07cbde9505bdfc233b9949a276ee54c04bc52269c161e9aad128d8b)...: deployed at 0x2D7b763d4A86dd105d8878f31993c7995e9261A1 with 686956 gas
✨ Done in 16.14s.
After your smart contract is deployed to the Saigon testnet, you can find it in the testnet explorer.
Step 6. Verify the smart contract
Although this step is optional, verifying your smart contract and publishing its source code can build trust for your app. The Ronin network uses Sourcify to make this task easier.
- Run this command to publish the source code of your smart contract to Ronin Sourcify:
yarn hardhat --network saigon sourcify --endpoint https://sourcify.roninchain.com/server
- Open your smart contract in the Ronin app's Contracts tab.
