5

Let's say I have a simple Solidity wallet contract as shown below. When I want to withdraw from the contract using send the transaction fees (gas cost) are reduced from the amount the receiving party gets. How can I send ether from the smart contract so that gas cost is imposed on the contract itself, not on the receiver?

Also, I want to know the fee imposed on the wallet before calling the contract. Is it possible to determine the fee that is imposed for send() over JSON-RPC interface?

/**
 * Simple hosted wallet contract.
 */
contract Wallet {

    event Deposit(address from, uint value);
    event Withdraw(address to, uint value);

    address owner;

    function Wallet() {
        owner = msg.sender;
    }

    /**
     * Simple withdrawal operation.
     */
    function withdraw(address _to, uint _value) {
        if(msg.sender != owner) {
            throw;
        }

        Withdraw(_to, _value);

        _to.send(_value);
    }


    /**
     * Somebody sends ETH to this contract address
     */
    function() {
        // just being sent some cash?
        if (msg.value > 0) {
            Deposit(msg.sender, msg.value);
        }
    }

}
  • This information can be fetched from internal transaction as here: testnet.etherscan.io/tx/… – Mikko Ohtamaa Jul 16 '16 at 18:13
  • Title seems opposite from the question "How can I send ether from the smart contract so that gas cost is imposed on the contract itself, not on the receiver?" Should the title be rephrased? – eth Jul 17 '16 at 0:00
3

According to the solidity docs, I don't think your first request is possible. In fact, the docs recommend switching to a withdraw scheme:

Warning: There are some dangers in using send: The transfer fails if the call stack depth is at 1024 (this can
always be forced by the caller) and it also fails if the recipient runs out of gas. So in order to make safe Ether
transfers, always check the return value of send or even better: Use a pattern where the recipient withdraws the
money.

After switching to a withdrawal pattern, you can simply use web3.eth.estimateGas().

  • 1
    Good point! In this case I want to run a Ethereum wallet in a hosted web service. All normal Ethereum transactions should be available to the user, so unfortunately recipient withdrawal pattern cannot be done here. – Mikko Ohtamaa Jul 16 '16 at 18:11
3

When I want to withdraw from the contract using send the transaction fees (gas cost) are reduced from the amount the receiving party gets. How can I send ether from the smart contract so that gas cost is imposed on the contract itself, not on the receiver?

The gas is paid by whoever invokes withdraw.

_to.send(_value) will always try to send the number of wei specified by _value to the address _to. _value is not reduced by any gas costs. (send may fail (returns false) if the call depth of 1024 is exceeded or if the _to has a fallback function that consumes more than 2300 gas. Generally it is recommended to check the return value of send.)

determine the fee that is imposed for send() over geth JSON-RPC interface?

eth_estimateGas is a JSON-RPC method for estimating the gas cost. For the specific case of withdraw, it will give a reasonable estimate but it will not just be for the _to.send(_value). For general cases, consider What are the limitations to estimateGas and when would its estimate be considerably wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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