1

Truffle is telling me that it costs 1 Eth to deploy. Is that seriously the cost of deploying a simple contract (this is my first Mainnet deploy)? Or is there something else going on?

 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.

Upfront cost is 994313868000000000 Wei??

That's almost 1 Eth!


2 Answers 2

1

First, I am not an expert on this and you can very well see me as a beginner.

This upfront cost is gas price * gas you are sending which is set in your config. This does not necessarily match the real cost. What is important is how much gas your deployment will cost. The rest of the gas sent will not be used. In your case, you are willing to pay: 211000000000 wei for per gas price. You are also stating that you are willing the spend at most 4712388 amount of gas. Then at start of your deployment or dry-run it multiplies the gas price you will pay with the amount of gas you are willing to spend and calculate "upfront cost". If you do not have that amount in your account, It will fail. You should either pay less for gas price or reduce the amount of gas you are willing to spend. I hope it helps.

0

As another valid answer pointed out, the eventual gas cost is gas price * gas used. In your case your gas price is 211000000000. Your questions doesn't state the required gas used, but it states the maximum allowed: 4712388.

Now, if we assume that your contract requires all of the allowed gas, its gas cost would be: 4712388 * 211000000000. And, the result happens to be 994 313 868 000 000 000 - exactly the same amount your deployer gives you.

So, yes, the calculations are correct. And, yes, that is almost 1 Eth.

The contract looks simple, but it's importing some other contracts which are not simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.