It costs 3.5ETH to deploy a contract - GasLimit and GasPrice are set carefully

insert image description here
Not much to say, please see the picture above

Under normal circumstances, when we deploy a contract on the Ethereum network, it will cost 0.01 - 1 ETH. When you deploy a contract, the Gas fee exceeds 1 ETH. The deployment script will tell you to deploy a contract. It is recommended not to exceed 1 ETH. ETH, you should check your code or script, and then deploy the script to terminate the deployment.

But in a certain situation, you use the contract deployed by web3.js or ethers.js. The gas limit and gas price are the values ​​​​set by yourself. You did not write logic such as prompt interception, and then set a large value. The result This leads to the deployment of a simple contract, which costs a huge amount of money.

const fs = require('fs');
const solc = require('solc');
const Web3 = require('web3');

// 获取到合约sol文件
const myContractSol = fs.readFileSync('../contracts/MyContractLogic.sol');
// 编译前格式化合约
var input = {
    
    
  language: 'Solidity',
  sources: {
    
     
    'myContractSol': {
    
    
        content: myContractSol().toString()
    }
  },
  settings: {
    
    
    outputSelection: {
    
    
      '*': {
    
    
        '*': ['*']
      }
    }
  }
};

// 编译合约
var output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log(output)

// 获取编译后合约的字节码
const bytecode = output.contracts.SuperConfig.SuperConfig.evm.bytecode.object;
// 获取编译后合约的abi
const abi = output.contracts.SuperConfig.SuperConfig.abi;

 // Ethereum的RPC
let rpcUrl = "https://mainnet.infura.io/v3/your-key";
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl)); 

// 设置使用哪个账号进行部署
const privateKey = '0x00xxxxxxx';
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;

// 使用web3.js部署
const contract = new web3.eth.Contract(abi);
contract.deploy({
    
    
    data: "0x" + bytecode,
})
.send({
    
    
    from: account.address,   
    gas: 5000000,
    gasPrice: 800 * (10 ** 9), 
}, function(error, transactionHash){
    
     
    console.log("=====error, transactionHash: ", error, transactionHash)  
})
.on('error', function(error){
    
     
    console.log("=====error: ", error)  
 })
.on('transactionHash', function(transactionHash){
    
     
    console.log("=====transactionHash: ", transactionHash)    
})
.on('receipt', function(receipt){
    
    
    console.log("receipt合约地址:", receipt.options) 
})
.on('confirmation', function(confirmationNumber, receipt){
    
     
    console.log("=====confirmation:", confirmationNumber)
 })
.then(function(newContractInstance){
    
    
    console.log("last合约地址:", newContractInstance.options) 
});

Do you know which line it is on? It was the Send() function. The situation at that time was that the network was very congested. I tried for 2 days but couldn’t deploy it successfully, so I kept increasing the gas limit and gas price. As a result, some of the transactions sent before were sold , some timeouts were canceled.

Then the two transactions with relatively high gas settings were accidentally closed, which is a pity for my ETH.

Posting this article is to warn yourself, set the gas limit and gas price carefully, you can learn from MetaMask, and slowly increase the gas limit and gas price.

Guess you like

Origin blog.csdn.net/u013538542/article/details/122387974