5

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.

2 Answers 2

5

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);
0
2

Something like this maybe?

contract Own {
    address public owner;
    address public admin;

    function Own(){
        owner = msg.sender;
        admin = msg.sender;
    }
    function transfer(address to){
        //if it's not the admin or the owner
        if (msg.sender != admin && msg.sender != owner) throw;
        owner = to;
    }
}

It would work if the administration (create and edit) is done through an admin contract:

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

    function createContract(){
        //Create the contracts here
    }


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

Also... is there any special reason why you are using throw?

6
  • That's not quite what I am looking for - I am trying to find a way of performing an atomic state transfer between two separately mined transactions. Either both happen, or neither do.
    – billett
    Commented Mar 23, 2016 at 13:23
  • Using throw to raise an exception on unauthorised access - should probably use modifiers.
    – billett
    Commented Mar 23, 2016 at 13:26
  • Calling the two transfer functions in a single participate( ) function will provide you with the functionality that either both transfers happen or none. In this way a third party (the admin) is the solution. You could probably find other ways of doing it too. Not exactly sure what you mean by separately mined transactions. Two transactions in the same block done by independent callers? Commented Mar 23, 2016 at 13:42
  • Ah I mistyped - by 'separately mined transactions' I meant two contracts that have been mined independently of one another (not created within the same Tx contract as per your example). Having an admin 3rd account would do it, but I'm still keen to see if it is technically possible to do it without an admin.
    – billett
    Commented Mar 23, 2016 at 14:42
  • You could potentially make a system where they allow transfer to a specific address beforehand. Commented Mar 23, 2016 at 23:27

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.