diff --git a/docs/02-developers/05-smart-contracts/02-hardhat/interact-with-frontend.md b/docs/02-developers/05-smart-contracts/02-hardhat/interact-with-frontend.md index 0d1e2728..7728226f 100644 --- a/docs/02-developers/05-smart-contracts/02-hardhat/interact-with-frontend.md +++ b/docs/02-developers/05-smart-contracts/02-hardhat/interact-with-frontend.md @@ -1,6 +1,6 @@ --- sidebar_label: Interact with the Front-end -sidebar_position: 106 +sidebar_position: 107 title: Interact with the Front-end description: "Learn how to integrate your Rootstock smart contract with front-end applications." tags: diff --git a/docs/02-developers/05-smart-contracts/02-hardhat/troubleshooting.md b/docs/02-developers/05-smart-contracts/02-hardhat/troubleshooting.md index 0f2efcd0..0ae64ebe 100644 --- a/docs/02-developers/05-smart-contracts/02-hardhat/troubleshooting.md +++ b/docs/02-developers/05-smart-contracts/02-hardhat/troubleshooting.md @@ -1,6 +1,6 @@ --- sidebar_label: Debugging and Troubleshooting -sidebar_position: 106 +sidebar_position: 108 title: Common Errors and Tips description: "Learn about some potential issues you can run into and tips on how to resolve them." tags: [guides, developers, smart contracts, rsk, rootstock, hardhat, dApps, ethers] diff --git a/docs/02-developers/05-smart-contracts/02-hardhat/verify-smart-contracts.md b/docs/02-developers/05-smart-contracts/02-hardhat/verify-smart-contracts.md new file mode 100644 index 00000000..45313b7e --- /dev/null +++ b/docs/02-developers/05-smart-contracts/02-hardhat/verify-smart-contracts.md @@ -0,0 +1,159 @@ +--- +sidebar_label: Verify Smart Contracts +sidebar_position: 106 +title: Verify Smart Contracts +description: "Learn how to verify your Rootstock smart contract on Rootstock and Blockscout explorers." +tags: + [guides, developers, smart contracts, rsk, rootstock, hardhat, dApps, ethers, verify] +--- + +In this section, we'll verify your token contract on the Rootstock and Blockscout explorers, so the users of you dApp can be able to see the actual code of your contract to analyze that it doesn't have malicious code, and they can also interact with it. + +Once you have been working on your contract using Hardhat, you can execute the verification in both explorers simultaneously with a simple command. + +## Step 1: Install hardhat-verify plugin + +In case it isn't installed yet: + +```shell + npm install --save-dev @nomicfoundation/hardhat-verify +``` + + +And add the following code to your `hardhat.config.ts` file: + +```bash +require("@nomicfoundation/hardhat-verify"); +``` + +Or, if you are using TypeScript, add this to your hardhat.config.ts: + +```bash +import "@nomicfoundation/hardhat-verify"; +``` + +## Sept 2: Update Hardhat config + +You need to add the following Etherscan config to your `hardhat.config.ts` file: + + +```bash +require('@nomicfoundation/hardhat-toolbox'); +require('@nomicfoundation/hardhat-verify'); +require('dotenv').config(); + +module.exports = { + defaultNetwork: 'rskTestnet', + networks: { + rskMainnet: { + url: "https://rpc.testnet.rootstock.io/{YOUR_APIKEY}", + chainId: 30, + gasPrice: 60000000, + accounts: [PRIVATE_KEY], + }, + rskTestnet: { + url: "https://rpc.mainnet.rootstock.io/{YOUR_APIKEY}", + chainId: 31, + gasPrice: 60000000, + accounts: [PRIVATE_KEY], + }, + }, + solidity: { + compilers: [ + { + version: '0.8.25', + }, + ], + }, + sourcify: { + enabled: false, + }, + etherscan: { + apiKey: { + // Is not required by Blockscout or Rootstock explorers, + // but must be any non-empty string, so leave it as "rootstock". + rskTestnet: 'rootstock', + rskMainnet: 'rootstock', + }, + customChains: [ + { + network: 'rskTestnet', + chainId: 31, + urls: { + apiURL: 'https://be.explorer.testnet.rootstock.io/api/v3/etherscan', + browserURL: 'https://explorer.testnet.rootstock.io/', + }, + }, + { + network: 'rskMainnet', + chainId: 30, + urls: { + apiURL: 'https://be.explorer.rootstock.io/api/v3/etherscan', + browserURL: 'https://explorer.rootstock.io/', + }, + }, + ], + }, + blockscout: { + enabled: true, + customChains: [ + { + network: 'rskTestnet', + chainId: 31, + urls: { + apiURL: 'https://rootstock-testnet.blockscout.com/api/', + browserURL: 'https://rootstock-testnet.blockscout.com/', + }, + }, + { + network: 'rskMainnet', + chainId: 30, + urls: { + apiURL: 'https://rootstock.blockscout.com/api/', + browserURL: 'https://rootstock.blockscout.com/', + }, + }, + ], + }, +}; +``` + +## Step 3: Verify yor smart contract + +Now, run the verify task, passing the address of the contract, +the network where it's deployed, and the constructor arguments that were used to deploy the contract: +```bash +npx hardhat verify --network rskTestnet DEPLOYED_CONTRACT_ADDRESS CONSTRUCTOR_ARGUMENTS +``` +or +```bash +npx hardhat verify --network rskMainnet DEPLOYED_CONTRACT_ADDRESS CONSTRUCTOR_ARGUMENTS +``` + +:::tip[Tip] + +- Replace `DEPLOYED_CONTRACT_ADDRESS` with the contract address that you want to verify. + +::: + +**The response should look like this:** +```bash +npx hardhat verify --network rskTestnet 0x28eb8D29e4713E211D1dDab19dF3de16086BB8fa 1000 +Successfully submitted source code for contract +contracts/MyToken.sol:MyToken at 0x28eb8D29e4713E211D1dDab19dF3de16086BB8fa +for verification on the block explorer. Waiting for verification result... + +Successfully verified contract MyToken on the block explorer. +https://explorer.testnet.rootstock.io/address/0x28eb8D29e4713E211D1dDab19dF3de16086BB8fa#code + +Successfully verified contract MyToken on the block explorer. +https://rootstock-testnet.blockscout.com/address/0x28eb8D29e4713E211D1dDab19dF3de16086BB8fa#code +``` + +With that, the contract has been successfully verified in both block explorers. + +## Resources +- [Deploy, Interact and Verify Smart Contracts using Remix and Rootstock Explorer](/developers/quickstart/remix/) +- Visit [hardhat-verify](https://hardhat.org/hardhat-runner/plugins/nomicfoundation-hardhat-verify#hardhat-verify) +- Visit [blockscout](https://docs.blockscout.com/for-users/verifying-a-smart-contract/hardhat-verification-plugin) +- [Hardhat Starter Kit for Rootstock](https://github.com/rsksmart/rootstock-hardhat-starterkit) diff --git a/docs/02-developers/05-smart-contracts/04-verify-smart-contracts/hardhat-verify-plugin.md b/docs/02-developers/05-smart-contracts/04-verify-smart-contracts/hardhat-verify-plugin.md index 9749ad94..12d977c5 100644 --- a/docs/02-developers/05-smart-contracts/04-verify-smart-contracts/hardhat-verify-plugin.md +++ b/docs/02-developers/05-smart-contracts/04-verify-smart-contracts/hardhat-verify-plugin.md @@ -8,7 +8,7 @@ tags: [hardhat, tutorial, developers, quick starts, rsk, rootstock] Smart contracts are the backbone of decentralized applications (dApps). They automate agreements and processes, but their code can be complex and prone to errors. Verifying your smart contracts is crucial to ensure they function as intended. -This tutorial will guide you through verifying your contracts using the Hardhat Verification Plugin on the Rootstock Blockscout Explorer. This plugin simplifies the verification of Solidity smart contracts deployed on the Rootstock network. By verifying the contracts, you allow Blockscout, an open-source block explorer, to link your contract's source code with its deployed bytecode on the blockchain, allowing for more trustless interaction with the code. +This tutorial will guide you through verifying your contracts using the Hardhat Verification Plugin on the Rootstock Explorer and Blockscout Explorer with a single command. This plugin simplifies the verification of Solidity smart contracts deployed on the Rootstock network. By verifying the contracts, you allow block explorers like Rootstock and Blockscout to link your contract's source code with its deployed bytecode on the blockchain, allowing for more trustless interaction with the code. In this tutorial, we'll do the following steps: - Set up your hardhat config environment in your project @@ -29,9 +29,7 @@ A [Hardhat Starter dApp](https://github.com/rsksmart/rootstock-hardhat-starterki ## What is hardhat-verify? [Hardhat](https://hardhat.org/) is a full-featured development environment for contract compilation, deployment and verification. -The [hardhat-verify plugin](https://hardhat.org/hardhat-runner/plugins/nomicfoundation-hardhat-verify) supports contract verification on the [Rootstock Blockscout Explorer](https://rootstock.blockscout.com/). - -> Note: The `hardhat-verify` plugin will be available soon on the [Rootstock Explorer](https://explorer.rootstock.io/). +The [hardhat-verify plugin](https://hardhat.org/hardhat-runner/plugins/nomicfoundation-hardhat-verify) supports contract verification on the [Rootstock Explorer](https://explorer.rootstock.io/) and [Rootstock Blockscout Explorer](https://rootstock.blockscout.com/). ### Installation @@ -53,112 +51,120 @@ import "@nomicfoundation/hardhat-verify"; ### Usage -You need to add the following Etherscan config to your `hardhat.config.ts` file and To obtain a Rootstock API key, follow the steps outlined in the [Obtaining a Rootstock API Key from Blockscout](/developers/smart-contracts/verify-smart-contracts/foundry-blockscout/) guide: +You need to add the following Etherscan config to your `hardhat.config.ts` file: ```bash -// Hardhat configuration -const config: HardhatUserConfig = { - defaultNetwork: "hardhat", - networks: { - hardhat: { - // If you want to do some forking, uncomment this - // forking: { - // url: MAINNET_RPC_URL - // } - }, - localhost: { - url: "http://127.0.0.1:8545", - }, - rskMainnet: { - url: RSK_MAINNET_RPC_URL, - chainId: 30, - gasPrice: 60000000, - accounts:[PRIVATE_KEY] - }, - rskTestnet: { - url: RSK_TESTNET_RPC_URL, - chainId: 31, - gasPrice: 60000000, - accounts:[PRIVATE_KEY] - }, +require('@nomicfoundation/hardhat-toolbox'); +require('@nomicfoundation/hardhat-verify'); +require('dotenv').config(); + +module.exports = { + defaultNetwork: 'rootstockTestnet', + networks: { + rootstockMainnet: { + url: 'https://public-node.rsk.co', + chainId: 30, + accounts: [PRIVATE_KEY], }, - namedAccounts: { - deployer: { - default: 0, // Default is the first account - mainnet: 0, - }, - owner: { - default: 0, - }, + rootstockTestnet: { + url: 'https://public-node.testnet.rsk.co', + chainId: 31, + accounts: [PRIVATE_KEY], }, - solidity: { - compilers: [ - { - version: "0.8.25", - }, - ], + }, + solidity: { + compilers: [ + { + version: '0.8.25', + }, + ], + }, + sourcify: { + enabled: false, + }, + etherscan: { + apiKey: { + // Is not required by Blockscout or Rootstock explorers, + // but must be any non-empty string, si leave it as "rootstock". + rootstockTestnet: 'rootstock', + rootstockMainnet: 'rootstock', }, - sourcify: { - enabled: false - }, - etherscan: { - apiKey: { - // Is not required by blockscout. Can be any non-empty string - rskTestnet: 'RSK_TESTNET_RPC_URL', - rskMainnet: 'RSK_MAINNET_RPC_URL' + customChains: [ + { + network: 'rootstockTestnet', + chainId: 31, + urls: { + apiURL: 'https://be.explorer.testnet.rootstock.io/api/v3/', + browserURL: 'https://explorer.testnet.rootstock.io/', + }, + }, + { + network: 'rootstockMainnet', + chainId: 30, + urls: { + apiURL: 'https://be.explorer.rootstock.io/api/v3/', + browserURL: 'https://explorer.rootstock.io/', }, - customChains: [ - { - network: "rskTestnet", - chainId: 31, - urls: { - apiURL: "https://rootstock-testnet.blockscout.com/api/", - browserURL: "https://rootstock-testnet.blockscout.com/", - } - }, - { - network: "rskMainnet", - chainId: 30, - urls: { - apiURL: "https://rootstock.blockscout.com/api/", - browserURL: "https://rootstock.blockscout.com/", - } - }, - - ] }, + ], + }, + blockscout: { + enabled: true, + customChains: [ + { + network: 'rootstockTestnet', + chainId: 31, + urls: { + apiURL: 'https://rootstock-testnet.blockscout.com/api/', + browserURL: 'https://rootstock-testnet.blockscout.com/', + }, + }, + { + network: 'rootstockMainnet', + chainId: 30, + urls: { + apiURL: 'https://rootstock.blockscout.com/api/', + browserURL: 'https://rootstock.blockscout.com/', + }, + }, + ], + }, }; - -export default config; ``` Now, run the verify task, passing the address of the contract, -the network where it's deployed, and any other arguments that was used to deploy the contract: +the network where it's deployed, and the constructor arguments that were used to deploy the contract: ```bash -npx hardhat verify --network rskTestnet DEPLOYED_CONTRACT_ADDRESS +npx hardhat verify --network rootstockTestnet DEPLOYED_CONTRACT_ADDRESS CONSTRUCTOR_ARGUMENTS ``` or ```bash -npx hardhat verify --network rskMainnet DEPLOYED_CONTRACT_ADDRESS +npx hardhat verify --network rootstockMainnet DEPLOYED_CONTRACT_ADDRESS CONSTRUCTOR_ARGUMENTS ``` :::tip[Tip] -- Replace `DEPLOYED_CONTRACT_ADDRESS` with the contract address. -- This can be gotten from the `MockERC721.json` file containing the addresses and abi under the `deployments/network` folder. +- Replace `DEPLOYED_CONTRACT_ADDRESS` with the contract address that you want to verify. ::: **The response should look like this:** ```bash -npx hardhat verify --network rskTestnet 0x33aC0cc41B11282085ff6db7E1F3C3c757143722 +npx hardhat verify --network rootstockTestnet 0x1b4951c57ce2c53addcfa173d1106b5e12f11e38 1000 MyToken23 MTK23 Successfully submitted source code for contract -contracts/ERC721.sol:MockERC721 at 0x33aC0cc41B11282085ff6db7E1F3C3c757143722 +contracts/MyToken.sol:MyToken at 0x1b4951c57ce2c53addcfa173d1106b5e12f11e38 for verification on the block explorer. Waiting for verification result... -Successfully verified contract MockERC721 on the block explorer. -https://rootstock-testnet.blockscout.com/address/0x33aC0cc41B11282085ff6db7E1F3C3c757143722#code + +Successfully verified contract MyToken on the block explorer. +https://explorer.testnet.rootstock.io/address/0x1b4951c57ce2c53addcfa173d1106b5e12f11e38#code + +Successfully verified contract MyToken on the block explorer. +https://rootstock-testnet.blockscout.com/address/0x1b4951c57ce2c53addcfa173d1106b5e12f11e38#code ``` + +With that, the contract has been successfully verified in both block explorers. + ## Resources - [Deploy, Interact and Verify Smart Contracts using Remix and Rootstock Explorer](/developers/quickstart/remix/) - Visit [hardhat-verify](https://hardhat.org/hardhat-runner/plugins/nomicfoundation-hardhat-verify#hardhat-verify) diff --git a/docs/02-developers/05-smart-contracts/05-foundry/index.md b/docs/02-developers/05-smart-contracts/05-foundry/index.md index c3d86c7c..7f16d692 100644 --- a/docs/02-developers/05-smart-contracts/05-foundry/index.md +++ b/docs/02-developers/05-smart-contracts/05-foundry/index.md @@ -23,5 +23,6 @@ tags: [rsk, foundry, developers, developer tools, rootstock, testing, dApps, sma | [Smart Contract](/developers/smart-contracts/foundry/smart-contracts/) | Check foundry demo smart contract.| | [Test Smart Contract](/developers/smart-contracts/foundry/test-smart-contracts/) | Learn how to test your smart contract using `forge`. | | [Deploy Smart Contract](/developers/smart-contracts/foundry/deploy-smart-contracts/) | Learn how to deploy your smart contract using `forge`. | +| [Verify Smart Contract](/developers/smart-contracts/foundry/verify-smart-contracts/) | Learn how to verify your smart contract using `forge`. | | [Interact with Smart Contract](/developers/smart-contracts/foundry/interact-with-contract/) | Learn how to interact with your smart contract using `cast`. | | [Debugging and Troubleshooting Tips](/developers/smart-contracts/foundry/troubleshooting/) | Learn about the common issues you can come across while building following this guide and how you can solve them. | \ No newline at end of file diff --git a/docs/02-developers/05-smart-contracts/05-foundry/interact-with-contract.md b/docs/02-developers/05-smart-contracts/05-foundry/interact-with-contract.md index aca596db..a46c4ce1 100644 --- a/docs/02-developers/05-smart-contracts/05-foundry/interact-with-contract.md +++ b/docs/02-developers/05-smart-contracts/05-foundry/interact-with-contract.md @@ -1,6 +1,6 @@ --- sidebar_label: Interact with the Smart Contract -sidebar_position: 106 +sidebar_position: 107 title: Interact with the Smart Contract description: "Learn how to interact with your smart contract using cast" tags: [guides, developers, smart contracts, rsk, rootstock, hardhat, dApps, ethers] diff --git a/docs/02-developers/05-smart-contracts/05-foundry/troubleshooting.md b/docs/02-developers/05-smart-contracts/05-foundry/troubleshooting.md index 74430010..534620aa 100644 --- a/docs/02-developers/05-smart-contracts/05-foundry/troubleshooting.md +++ b/docs/02-developers/05-smart-contracts/05-foundry/troubleshooting.md @@ -1,6 +1,6 @@ --- sidebar_label: Debugging and Troubleshooting -sidebar_position: 106 +sidebar_position: 108 title: Common Errors and Tips description: "Learn about some potential issues you can run into and tips on how to resolve them." tags: [guides, developers, smart contracts, rsk, rootstock, foundry, dApps, ethers] diff --git a/docs/02-developers/05-smart-contracts/05-foundry/verify-smart-contracts.md b/docs/02-developers/05-smart-contracts/05-foundry/verify-smart-contracts.md new file mode 100644 index 00000000..f4dfbfbe --- /dev/null +++ b/docs/02-developers/05-smart-contracts/05-foundry/verify-smart-contracts.md @@ -0,0 +1,78 @@ +--- +sidebar_label: Verify Smart Contract +sidebar_position: 105 +title: Verify Smart Contract +description: "Learn how to verify your Rootstock smart contract using forge." +tags: [guides, developers, smart contracts, rsk, rootstock, foundry, dApps, verify] +--- + +In this section, you'll verify your `counter` smart contract to the Rootstock Explorer using Foundry, so the users of you dApp can be able to see the actual code of your contract to analyze that it doesn't have malicious code, and they can also interact with it. + +## Verify simple contract + +After you have deployed your smart contract, you can verify it using Foundry with a simple command. + +```bash +forge verify-contract \ + --chain-id 31 \ + --watch \ + --compiler-version v0.8.24 \ + --verifier custom \ + --verifier-url https://be.explorer.testnet.rootstock.io/api/v3/etherscan \ + 0x499e802a6825d30482582d9b9dd669ba82ba8ba4 \ + src/Counter.sol:Counter +``` + +The verification will be executed, and you will receive the following response: + +```bash +Start verifying contract `0x499e802a6825d30482582d9b9dd669ba82ba8ba4` deployed on rsk-testnet +Compiler version: v0.8.28 +Optimizations: 0 + +Submitting verification for [src/Counter.sol:Counter] 0x499e802a6825d30482582d9b9dd669ba82ba8ba4. +Submitted contract for verification: + Response: `OK` + GUID: `72f0b154-6d94-40bc-bf7d-61b3b266ed5b` + URL: https://be.explorer.testnet.rootstock.io/api/v3/etherscan/address/0x499e802a6825d30482582d9b9dd669ba82ba8ba4 +Contract verification status: +Response: `NOTOK` +Details: `Pending in queue` +Warning: Verification is still pending...; waiting 15 seconds before trying again (7 tries remaining) +Contract verification status: +Response: `OK` +Details: `Pass - Verified` +Contract successfully verified +``` + +## Verify with constructor arguments + +If your contract has constructor arguments, you must pass them in order to successfully verify it. Foundry accepts the constructor arguments as ABI encoded. + +For that, you can use the [cast abi-encode](https://getfoundry.sh/cast/reference/abi-encode/) foundry tool. + +As an example, for a contract that has a constructor argument like `constructor(uint256 initialSupply)`, initialized with the value of `1000` at the contract deploy, you can execute the following command: + +```bash +cast abi-encode "constructor(uint)" 1000 +``` + +result: + +```bash +0x00000000000000000000000000000000000000000000000000000000000003e8 +``` + +And, then, you can run the verification command passing the constructor argment as ABI encoded: + +```bash +forge verify-contract \ + --constructor-args 0x00000000000000000000000000000000000000000000000000000000000003e8 + --chain-id 31 \ + --watch \ + --compiler-version v0.8.24 \ + --verifier custom \ + --verifier-url https://be.explorer.testnet.rootstock.io/api/v3/etherscan \ + 0x499e802a6825d30482582d9b9dd669ba82ba8ba4 \ + src/Counter.sol:Counter +``` \ No newline at end of file