Truffle

為什麼這個合約需要 1 Eth 來部署?

  • March 3, 2021

Truffle 告訴我部署需要 1 個 Eth。這真的是部署簡單合約的成本嗎(這是我的第一個主網部署)?還是有其他事情發生?

mainnet: {
     provider: () =>
       new HDWalletProvider({
         mnemonic: { phrase: process.env.MNEMONIC },
         providerOrUrl: process.env.RPC_URL_1_WSS,
       }),
     network_id: 1, // Main's id
     from: process.env.DEPLOYERS_ADDRESS,
     gas: 4712388, // Gas limit used for deploys. Default is 4712388.
     gasPrice: 211000000000, //Gas price in Wei
     confirmations: 2, // # of confs to wait between deployments. (default: 0)
     timeoutBlocks: 200, // # of blocks before a deployment times out  (minimum/default: 50)
     skipDryRun: false, // Skip dry run before migrations? (default: false for public nets )
   },
 },
pragma solidity ^0.7.4;

import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";


contract EthMsg {
 address payable public owner;
 uint public messagesSentCount = 0;

 uint decimals = 8;
 uint scale = 10**decimals;

 uint public txtPriceUSD = 1.70 * 10**8;
 uint public txtPriceEth;

 using SafeMath for uint;

 AggregatorV3Interface internal priceFeed;

 event Spend(
   string textMsg,
   string recipient
 );
 event TextMsgError(
   string reason
 );

 constructor(address oracleAddress) {
   owner = msg.sender;
   priceFeed = AggregatorV3Interface(oracleAddress);
 }

 function sendTextMessage(
   string calldata textMsg,
   string calldata recipient
 ) external payable returns(bool) {

   (uint newTxtPriceEth,,) = this.pollTxtPrices();

   require(
     msg.value > newTxtPriceEth,
     'Please send the correct amount of ETH to make send a message'
   );

   emit Spend(textMsg, recipient);
   messagesSentCount += 1;
   return true;
 }

 function setTxtPrices(uint _txtPriceUSD) external ownerOnly() {
   (int price,) = getLatestPrice();
   uint ethPriceUSD = uint(price);

   txtPriceEth = scale.mul(_txtPriceUSD).div(ethPriceUSD);
   txtPriceUSD = _txtPriceUSD;
 }

 function pollTxtPrices() external view returns(uint, uint, uint) {
   (int price,) = getLatestPrice();

   uint newTxtPriceEth = scale.mul(txtPriceUSD).div(uint(price));

   return (newTxtPriceEth, txtPriceUSD, decimals);
 }

 function totalBalance() external view returns(uint) {
   return payable(address(this)).balance;
 }

 function withdrawFunds() external ownerOnly() {
   msg.sender.transfer(this.totalBalance());
 }

 function destroy() external ownerOnly() {
   selfdestruct(owner);
 }

 function getLatestPrice() public view returns (int, uint) {
   (
     ,int price,,,
   ) = priceFeed.latestRoundData();

   return (price, decimals);
 }

 function uintToWei(uint unum) private pure returns(uint p) {
   return unum * (1 wei);
 }

 modifier ownerOnly() {
   require(msg.sender == owner, 'only owner can call this');
   _;
 }
}

truffle migrate --network mainnet --reset

 Replacing 'Migrations'
  ----------------------

Error:  *** Deployment Failed ***

"Migrations" could not deploy due to insufficient funds
  * Account:  0x16c8CF300eRa61f6d8e7S201AB7163FCc6660f1d
  * Balance:  297068575000000000 wei
  * Message:  sender doesn't have enough funds to send tx. The upfront cost is: 994313868000000000 and the sender's account only has: 297068575000000000
  * Try:
     + Using an adequately funded account
     + If you are using a local Geth node, verify that your node is synced.

前期費用是994313868000000000 Wei??

這幾乎是 1 Eth!


首先,我不是這方面的專家,您很可能將我視為初學者。

這個前期成本是您在配置中設置的*gas 價格 您發送的 gas。這不一定符合實際成本。重要的是您的部署將花費多少 gas。發送的其餘氣體將不會被使用。在您的情況下,您願意支付:211000000000 wei 的每 gas 價格。您還聲明您願意花費最多4712388 的 gas。然後在您開始部署或試執行時,它將您將支付的 gas 價格乘以您願意花費的 gas 量併計算“前期成本”。如果您的帳戶中沒有該金額,它將失敗。您應該為 gas 價格支付更少的費用,或者減少您願意花費的 gas 數量。我希望它有所幫助。

正如另一個有效答案指出的那樣,最終的 gas 成本是gas price* gas used。在您的情況下,您的汽油價格是211000000000。您的問題沒有說明使用的所需氣體,但說明了允許的最大值:4712388

現在,如果我們假設您的合約需要所有允許的 gas,那麼它的 gas 成本將是:4712388* 211000000000。而且,結果恰好是 994 313 868 000 000 000 - 與您的部署者給您的數量完全相同。

所以,是的,計算是正確的。而且,是的,這幾乎是 1 Eth。

合約看起來很簡單,但它正在導入一些其他不簡單的合約。

引用自:https://ethereum.stackexchange.com/questions/93954