Solidity Contract Design – Setting ‘From’ Field When Calling Other Contracts

contract-designsolidity

I'm aware a very similar question exists – but I feel my use case is sufficiently different to warrant a new question.

I have two instances of the following contract Own…

contract Own {
    address public owner;

    function Own(){
        owner = msg.sender;
    }
    function transfer(address to){
        if (msg.sender != owner) throw;
        owner = to;
    }
}

I want to write another contract that takes the transaction hashes of the above contract, and swaps the owner of the contract with each other through two calls to the Own.transfer(address) function.

I thought it would be something like the following, but the syntax is incorrect around the o1.transfer(p2,{from:p1});

contract Tx {
    address public p1;
    address public p2;
    Own public o1;
    Own public o2;

    function Tx(address ownAddress){
        p1 = msg.sender;
        o1 = Own(ownAddress);
    }
    function participate(address ownAddress){
        p2 = msg.sender;
        o2 = Own(ownAddress);
        o1.transfer(p2,{from:p1});
        o2.transfer(p1,{from:p2});
    }
}

For note – both Own & Tx contracts are within the same .sol file, so have knowledge about each other.

Best Answer

Any Ethereum contract (an Ethereum account with EVM code and without private key) controls only its private balance. The contract's balance can be increased by sending a message containing some value to the contract's address. What the contract will do with the balance is defined by EVM code of the contract. In particular, the contract can send some value to other addresses using CALL instruction.

In solidity simple CALL that only sends value (no other parameters) is done with .send() method of address type.

address(0x00ea32d8dae74c01ebe293c74921db27a6398d57).send(1 ether);

In case there is a need to both send value to and call a specific method of other contract, there is .value() modifier that can be applied to method execution.

OtherContract c(0x00ea32d8dae74c01ebe293c74921db27a6398d57);
var argument = 2;
c.take_my_money.value(1 ether)(argument);
Related Topic