假设我有一个ERC20智能合同,我正在硬帽中开发和测试
//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);
}
}我打算使用以下硬帽子RSK脚本将其部署到deploy.js网络:
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();要运行此脚本并在RSK主干网上部署智能契约,需要在终端中运行以下命令:
npx hardhat run scripts/deploy.js --network rskmainnet但是,在将智能契约部署在真正的区块链上之前,我真的很想知道这个部署将花费多少(欧元)!如何计算我的智能合同部署交易在RSK主干网上的价格,价格以法定价格计算?
作为参考,这是我使用的hardhat.config.js文件:
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",
},
},
},
};发布于 2022-05-24 22:28:21
首先,您需要估计部署事务将要使用的气体量。为此,请使用ethers.js signer.estimateGas()方法。
const deployTx = ERC20TokenFactory.getDeployTransaction(initialSupply);
const estimatedGas = await ERC20TokenFactory.signer.estimateGas(deployTx);那么你需要知道你的网络中当前的汽油价格:
const gasPrice = await ERC20TokenFactory.signer.getGasPrice();要获得部署RBTC的价格,请将气体量乘以天然气价格:
const deploymentPriceWei = gasPrice.mul(estimatedGas);
const deploymentPriceRBTC =
ethers.utils.formatEther(deploymentPriceWei);接下来,您可以使用Coingecko API获取当前的RBTC/EUR比率:
const rbtcEurRate = (
await axios.get('https://api.coingecko.com/api/v3/coins/rootstock')
).data.market_data.current_price.eur;(请注意,CoinGecko使用rootstock表示RSK网络RBTC的本机货币。)
最后,为了得到以欧元计算的部署价格,将部署RBTC价格乘以RBTC/EUR汇率:
const deploymentPriceEUR = (deploymentPriceRBTC * rbtcEurRate).toFixed(2);我们可以通过创建一个结合上述内容的硬帽子脚本来使上述所有内容都可重复。要执行此操作,请创建scripts/estimateDeploymentPrice.js,如下所示:
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();使用命令运行脚本:
npx hardhat run scripts/estimateDeploymentPrice.js --network rskmainnet(请注意,为了注入hre,脚本需要在硬草帽中运行。)
脚本输出:
ERC20 token deployment price on rskmainnet is 3.34 EURhttps://stackoverflow.com/questions/72370093
复制相似问题