3

When deploying contracts, to what end can/is the ether value associated to the deployment transaction be used? In the documentation, it's stated that:

The value field is used the same as in ether transfer transactions between normal accounts. In other words transferring funds is available between any two accounts, either normal (i.e. externally controlled) or contract.

but who receives this value? or is it the value of the product between gasPrice and gas deducted from the creating account upon deployment (and received by miners)?

1 Answer 1

4

Q: When deploying contracts, to what end can/is the ether value associated to the deployment transaction be used?

The value amount in ethers is transferred from the deploying account to the newly deployed contract. The contract is then able to transfer these amounts to other accounts or contracts.

Q: but who receives this value? or is it the value of the product between gasPrice and gas deducted from the creating account upon deployment (and received by miners)?

The deployed contract receives the value amount. The winning miner is paid (gasPrice x gasUsed) ethers, and this fee amount is deducted from the deploying account IN ADDITION to the value amount deducted.



An example:

  • I want to deploy a poker contract (I won't as I do not want to encourage gambling)
  • I have 100 ETH in the account I'm deploying the poker contract from
  • The fee to deploy the contract is (gasUsed x gasPrice) = 0.01 ETH
  • I want to load the poker contract with an initial amount of 50 ETH
  • I deploy the poker contract with amount = 50 ETH
  • 50.01 ETH will get deducted from my account
  • The poker contract will have a 50 ETH balance on creation
  • The winning miner receives a 0.01 ETH fee
  • A gambler could win a round of poker and my poker contract will make a payment to the gambler from the 50 ETH balance
  • A gamble could lose a round of poker and their payment will increase the ETH balance of the poker contract


Response to additional question in the comment:

So according to the answer to the first question the contract is an account which can, in turn, make transactions (including new contracts). But the contract is stored in the block chain meaning that it's publicly available to any node who synchronized with the block containing it, meaning that it's private key is visible to all peers? Doesn't this mean that it is possible to make transactions on behalf of a contract?

The contract does not have an associated private key. The contract will have a public key that is the account address the contract is deployed to. The contract code will have to be written to ensure that only the correct transactions are made to the right recipients.

Take the poker example:

  • When deploying the poker contract, the contract stores the "owner" of the poker contract by saving the deploying account address.
  • When players win a round of poker, the contract sends the winning account the ethers won
  • When the owner wants to withdraw the accumulated winnings from the poker contract, the owner may call a withdrawProfits() function, and this checks that the withdrawal is only made to the original deploying account address (which was stored in the poker contract's data when the contract was originally deployed).
6
  • So according to the answer to the first question the contract is an account which can, in turn, make transactions (including new contracts). But the contract is stored in the block chain meaning that it's publicly available to any node who synchronized with the block containing it, meaning that it's private key is visible to all peers? Doesn't this mean that it is possible to make transactions on behalf of a contract?
    – Sebi
    Commented Jul 13, 2016 at 16:42
  • 1
    I've edited the answer to respond to your question. Commented Jul 13, 2016 at 16:53
  • So the public key/address of the contract is distinct from the address of the account that has deployed it, no? So when transactions are made (the contract sends ethers or deploys other contracts) does the contract use this key for signing?
    – Sebi
    Commented Jul 14, 2016 at 8:24
  • 1
    Yes, the contract address is distinct from the address deploying the contract. The contract does not need a key for signing. In the poker contract example, another user can send ethers to the poker contract. When executing this transaction, the code in the poker contract is executed, and determines whether to send ethers back to the user. Commented Jul 14, 2016 at 8:34
  • 1
    The contract is able to hold a balance that external transactions can add value to. The contract is able to use internal transactions to transfer part or all of the balance to other accounts and contracts - when the code is executed - and the code is only executed when an externally owned account executes a transaction (the EOA can sent ETH to a contract which runs the contract code, and the contract code can run another contract code, ...). Commented Jul 14, 2016 at 8:47

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.