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?
I think possibly some misunderstanding. A handful of clarifications might help.
- Contracts cannot (yet) pay the gas for anything. Gas is always paid by a sender who sends a transaction to a contract function.
- Deploying a contract is a transaction that costs gas, and this gas is paid for by the account that deploys the contract.
- 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.
- 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.