[Ethereum] Including transactions fees when sending out ETH from a smart contract

contract-designfeesgassoliditytransactions

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);
        }
    }

}

Best Answer

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().

Related Topic