Aleks Shenshin
Aleks Shenshin

Reputation: 2196

How to calculate a smart contract deployment price on RSK?

Say I have an ERC20 smart contract which I'm developing and testing in Hardhat:

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract Erc20Token is ERC20, Ownable {
  constructor(uint initialSupply) ERC20("ERC20 Token", "ETK") {
    ERC20._mint(msg.sender, initialSupply);
  }
}

I intend to deploy it to the RSK network with the following Hardhat deploy.js script:

async function deploy() {
  try {
    const initialSupply = 1e6;
    const ERC20TokenFactory = await ethers.getContractFactory('Erc20Token');
    const erc20Token = await ERC20TokenFactory.deploy(initialSupply);
    await erc20Token.deployed();
    console.log(
      `ERC20 Token was deployed on ${hre.network.name} with an address: ${erc20Token.address} `,
    );
    process.exit(0);
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
}
deploy();

To run this script and deploy my smart contract on RSK mainnet I need to run the following command in terminal:

npx hardhat run scripts/deploy.js --network rskmainnet

However, before deploying the smart contract on a real blockchain, I would really like to know how much this deployment will cost (in EUR)! How to calculate the price of my smart contract deployment transaction on the RSK mainnet, priced in fiat?

For reference, this is the hardhat.config.js file I'm using:

require('@nomiclabs/hardhat-waffle');
const { mnemonic } = require('./.secret.json');

module.exports = {
  solidity: '0.8.4',
  defaultNetwork: 'rsktestnet',
  networks: {
    rsktestnet: {
      chainId: 31,
      url: 'https://public-node.testnet.rsk.co/',
      accounts: {
        mnemonic,
        path: "m/44'/60'/0'/0",
      },
    },
    rskmainnet: {
      chainId: 30,
      url: 'https://public-node.rsk.co',
      accounts: {
        mnemonic,
        path: "m/44'/60'/0'/0",
      },
    },
  },
};

Upvotes: 6

Views: 885

Answers (1)

bguiz
bguiz

Reputation: 28587

First you need to estimate the amount of gas which your deploy transaction is going to use. To accomplish this, use ethers.js signer.estimateGas() method.

const deployTx = ERC20TokenFactory.getDeployTransaction(initialSupply);
const estimatedGas = await ERC20TokenFactory.signer.estimateGas(deployTx);

Then you need to know the current gas price in your network:

const gasPrice = await ERC20TokenFactory.signer.getGasPrice();

To get the deployment RBTC price, multiply gas amount by the gas price:

const deploymentPriceWei = gasPrice.mul(estimatedGas);
const deploymentPriceRBTC =
    ethers.utils.formatEther(deploymentPriceWei);

Next you can use the Coingecko API to get current RBTC/EUR rate:

const rbtcEurRate = (
      await axios.get('https://api.coingecko.com/api/v3/coins/rootstock')
    ).data.market_data.current_price.eur;

(Note that CoinGecko uses rootstock to denote the native currency of the RSK network, RBTC.)

And finally, to get the deployment price in EUR, multiply the deployment RBTC price by RBTC/EUR exchange rate:

const deploymentPriceEUR = (deploymentPriceRBTC * rbtcEurRate).toFixed(2);

We can make all of the above repeatable by creating a hardhat script that combines the above. To do this create scripts/estimateDeploymentPrice.js, with the following:

const axios = require('axios').default;

async function estimateDeploymentPrice() {
  try {
    const initialSupply = 1e6;
    const contractFactory = await ethers.getContractFactory('Erc20Token');
    const deployTx = contractFactory.getDeployTransaction(initialSupply);
    const estimatedGas = await contractFactory.signer.estimateGas(deployTx);
    const gasPrice = await contractFactory.signer.getGasPrice();
    const deploymentPriceWei = gasPrice.mul(estimatedGas);
    const deploymentPriceRBTC = ethers.utils.formatEther(deploymentPriceWei);
    const rbtcEurRate = (
      await axios.get('https://api.coingecko.com/api/v3/coins/rootstock')
    ).data.market_data.current_price.eur;
    const deploymentPriceEUR = (deploymentPriceRBTC * rbtcEurRate).toFixed(2);
    console.log(
      `ERC20 token deployment price on ${hre.network.name} is ${deploymentPriceEUR} EUR`,
    );
    process.exit(0);
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
}

estimateDeploymentPrice();

Run the script with a command:

npx hardhat run scripts/estimateDeploymentPrice.js --network rskmainnet

(Note that the script needs to be run through hardhat in order for hre to be injected.)

The script outputs:

ERC20 token deployment price on rskmainnet is 3.34 EUR

Upvotes: 8

Related Questions