Cronos developer series: deploy & verify your contracts using Hardhat (2024)

Cronos developer series: deploy & verify your contracts using Hardhat (1)

[Note: The repository may have been updated since this blog post.]

Smart contract verification is sometimes overlooked when teams are rushing to ship, but it is an essential tenet of Web3 application development.

In this blog post, we review a simple approach to deploy and verify a smart contract on Cronos mainnet, using:

  • A simple ERC20 contract from the OpenZeppelin library

  • The Hardhat smart contract development and testing framework

  • The Hardhat-Etherscan plugin, used to automate the verification of the smart contract, after deployment, on Cronoscan (the Cronos version of Etherscan)

  • The Hardhat-Cronoscan plugin, which is used to configure the Hardhat-Etherscan plugin (listed above) for Cronos

  • Cronoscan, the block explorer on Cronos mainnet

Let’s set-up our working directory

If you want to go quickly through this step, you can simply clone our Cronos hardhat boilerplate repository and then type the npm install command in the root directory.

Alternatively, here is how you can re-create this boilerplate directory. In your project folder with node already installed, create your Hardhat project by typing npx hardhat and select “❯ Create an advanced sample project that uses TypeScript”. Reply yes to all.

Install the following dependencies:

npm install ethers dotenv @openzeppelin/contractsnpm install --save-dev @nomiclabs/hardhat-etherscan@^3.1.0npm install --save-dev @cronos-labs/hardhat-cronoscannpm install --save-dev solidity-coveragenpm install --save-dev hardhat-gas-reporter

Remove the files that you are not going to use and replace them by the actual contract, deployment and test files (see repository for the actual example files):

  • Remove contracts/Greeter.sol => create contracts/MyERC20.sol

  • Remove scripts/deploy.ts => create scripts/DeployMyERC20.script.ts

  • Remove tests/index.ts => create scripts/MyERC20.test.ts

Then, update your hardhat.config.ts file, using the hardhat.config.ts file of the repository as template:

const config: HardhatUserConfig = { networks: { hardhat: {}, cronosTestnet: { url: "https://evm-t3.cronos.org/", chainId: 338, accounts: [myPrivateKey], gasPrice: 5000000000000, }, cronos: { url: "https://evm.cronos.org/", chainId: 25, accounts: [myPrivateKey], gasPrice: 5000000000000, }, }, etherscan: { apiKey: { cronosTestnet: <string>process.env["CRONOSCAN_TESTNET_API_KEY"], cronos: <string>process.env["CRONOSCAN_API_KEY"], }, },...}

Environment variables

Create the.env file, using the.env.example file of the repository as template. The environment variables are:

  • MY_PRIVATE_KEY: this is the private key of a wallet address on Cronos (or Cronos Testnet), funded with CRO or TCRO, which will deploy the smart contract and be its owner. If you are using Cronos Testnet, you can visit cronos.org/faucet to get some TCRO.

  • CRONOSCAN_API_KEY and CRONOSCAN_TESTNET_API_KEY: you can get API keys from cronoscan.com and testnet.cronoscan.com respectively. This is used by Hardhat to connect to Cronoscan (the Cronos version of Etherscan) in order to verify your smart contract.

  • COINMARKETCAP_API: you can get an API key from Coinmarketcap.com. This is used by hardhat-gas-reporter to convert the deployment cost of the smart contract in USD or EUR.

Time to test yourcode

It is really important to achieve 100% unit test coverage with smart contracts. You can measure your test coverage thanks to the solidity-coverage tool, which we have installed in the above section.

For example you can type in the project folder:

npx hardhat coverage

Using our illustrative repository, you will see the following report showing that your test coverage is far from complete, as only 57% of lines are covered:

Cronos developer series: deploy & verify your contracts using Hardhat (2)

The next step would be to complete the test script in order to cover more cases.

Let’s deploy the smart contract toCronos

The deployment to Cronos mainnet is fairly simple using the scripts/DeployMyERC20.script.ts file provided in our example repository.

For Cronos Mainnet:

npx hardhat run scripts/DeployMyERC20.script.ts --network cronos
Cronos developer series: deploy & verify your contracts using Hardhat (3)

Or, for Cronos Testnet:

npx hardhat run scripts/DeployMyERC20.script.ts --network cronosTestnet

Do not forget to verify the smart contract!

The verification of the smart contract consists in uploading the smart contract code to Cronoscan so that any user can check the source code of the contract that they are interacting with, simply by entering the smart contract address in Cronoscan.

Fortunately, Hardhat makes it easy to automate this process.

If you have installed everything properly as explained above, you can check that the Hardhat contract verification plug-in is configured for Cronos by typing:

npx hardhat verify --list-networks

The response should include, at the bottom:

Cronos developer series: deploy & verify your contracts using Hardhat (4)

The syntax to verify the contract on Cronos Mainnet is:

npx hardhat verify --network cronos DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1" "Constructor argument 2"

So, for example:

npx hardhat verify --network cronos "0x481F53b0Bb0e6fBF3eDcFd4e2790aF7A4B89A1f9" "My token name" "My token symbol"

The response should look like:

Cronos developer series: deploy & verify your contracts using Hardhat (5)

Or, on Cronos Testnet:

npx hardhat verify --network cronosTestnet DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1" "Constructor argument 2"

And if you head to the cronoscan.com URL returned by the verify command, you should be able to see something like this:

Cronos developer series: deploy & verify your contracts using Hardhat (6)

As a bonus, users can now use Cronoscan to read your smart contracts or send transactions directly:

Cronos developer series: deploy & verify your contracts using Hardhat (7)

Happy coding!

Cronos developer series: deploy & verify your contracts using Hardhat (2024)
Top Articles
ET to EST Converter - Savvy Time
Disgust Asl
Dairy Queen Lobby Hours
Pet For Sale Craigslist
Design215 Word Pattern Finder
Ets Lake Fork Fishing Report
Body Rubs Austin Texas
Brendon Tyler Wharton Height
Vanadium Conan Exiles
THE 10 BEST River Retreats for 2024/2025
Chase Claypool Pfr
World of White Sturgeon Caviar: Origins, Taste & Culinary Uses
Jet Ski Rental Conneaut Lake Pa
De Leerling Watch Online
Koop hier ‘verloren pakketten’, een nieuwe Italiaanse zaak en dit wil je ook even weten - indebuurt Utrecht
Urban Dictionary: hungolomghononoloughongous
Canvas Nthurston
Stardew Expanded Wiki
Mahpeople Com Login
Hermitcraft Texture Pack
Cbssports Rankings
Isaidup
Rs3 Eldritch Crossbow
Craigslist Houses For Rent In Milan Tennessee
Urban Dictionary Fov
3 Ways to Drive Employee Engagement with Recognition Programs | UKG
Leben in Japan &#8211; das muss man wissen - Lernen Sie Sprachen online bei italki
Rgb Bird Flop
Miles City Montana Craigslist
Helpers Needed At Once Bug Fables
Ringcentral Background
Dentist That Accept Horizon Nj Health
Armor Crushing Weapon Crossword Clue
Metro By T Mobile Sign In
Worlds Hardest Game Tyrone
El agente nocturno, actores y personajes: quién es quién en la serie de Netflix The Night Agent | MAG | EL COMERCIO PERÚ
Pillowtalk Podcast Interview Turns Into 3Some
Midsouthshooters Supply
Tokyo Spa Memphis Reviews
Pp503063
Is The Nun Based On a True Story?
Pay Entergy Bill
Bartow Qpublic
How to Print Tables in R with Examples Using table()
Carteret County Busted Paper
Dwc Qme Database
2013 Honda Odyssey Serpentine Belt Diagram
Pixel Gun 3D Unblocked Games
Craigslist St Helens
Automatic Vehicle Accident Detection and Messageing System – IJERT
Sml Wikia
Minecraft Enchantment Calculator - calculattor.com
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 5914

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.