3

My contract needs eth to operate, but truffle deploys it with no balance, so I can't execute functions through it. The constructor doesn't require gas, but some other functions do. Is there a way I can deploy a contract with ether? Or am I misunderstanding something about how contracts work?

3 Answers 3

11

I think possibly some misunderstanding. A handful of clarifications might help.

  1. Contracts cannot (yet) pay the gas for anything. Gas is always paid by a sender who sends a transaction to a contract function.
  2. Deploying a contract is a transaction that costs gas, and this gas is paid for by the account that deploys the contract.
  3. If a contract requires ether sent during deployment (for some reason other than paying for gas) then it will have a constructor function marked payable. Truffle can indeed transfer ether (using value:) during the migration process if needed. In my opinion, it will almost always be better to separate concerns and keep deployment and funding functions separate.
  4. When Truffle deploys a contract, that transaction costs gas plus constructor cost (if any) and the deployment transactions are always from: an account/address. By default, this is the first account in the wallet. If any funds are transferred to the contract (not gas), then those funds will be drawn from the same sender account.

Hope it helps.

4
  • That did help, it seems I misunderstood something. Can you help me with my specific use case? I'm making a contract in which the user guesses heads or tails, and gets a token (MetaCoin) on correct guesses, and pays one on unsuccessful guesses. Right now I have the contract with the guess logic Guess, and another contract MetaCoin, and I have Guess instantiating MetaCoin through an internal function (initCoin()), and then Guess is in charge of distributing or taking MetaCoin. Is this incorrect? Any suggestions?
    – wtk219
    Jul 10, 2017 at 17:32
  • 1
    The way you have it set up, every time there's a new game, there's new kind of money and everybody goes back to initial state of 0 balance. Wouldn't it be better to fully separate those concerns and deploy the coin as a persistent stand-alone contract? That way, a new game could start without resetting everyone's balances. To fully separate things, separate MetaCoin as a separate project with a unique migration. Deploy it once. Pass the token contract address into the game constructor so it knows where the balances are. Jul 10, 2017 at 18:21
  • That makes a lot of sense. If I were to do this in Truffle/testrpc, do you mean I should actually create a new truffle project, migrate that, and then migrate my Guess project using MetaCoin's address? Or is it sufficient to have them both in the same project, but deploy them separately (by having a separate migrations file in the migrations folder)?
    – wtk219
    Jul 10, 2017 at 18:29
  • 1
    The first way. Two projects, because Truffle executes all the migrations it can find in order by filename, so first 1_deployMigrations.js, then your stuff. You can't tell it to just do part of it. After you deploy MetaCoin, you can get fancy in you Game migration by referencing MetaCoin.deployed() ... so you can tell Game's constructor about the address it needs to use for the coin. Jul 10, 2017 at 18:34
5

If using migration file from Truffle to deploy the contract:

const YourContract = artifacts.require("YourContract");

module.exports = (deployer, accounts) => {
    if (accounts) {
        // Create contract with 1 ether (contract must be payable)
        deployer.deploy(YourContract, { from: accounts[0], value: "1000000000000000000" });
    };
};

Make sure your constructor is payable:

constructor() public payable {
    // ...
}
2

Yes, you can deploy a contract with ether. Declare the constructor "payable" and then add some ether like so: {value: some_amount_here} when invoking the constructor. The contract will then be pre-supplied with that amount of wei/ether.

Your Answer

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

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