21

I've created a contract called "Name" which I've deployed to the testnet. this function just register some text for the user and display it back.

My contract is:

contract Name is mortal{

    mapping(address=>string) public text;
    string public test;

    function register(string _text){
        text[msg.sender]    = _text;
    }
}

I want to interact with this contract using a proxy contract:

contract Proxy is mortal {

    address watch_addr  = 0xEB1e2c19bd833b7f33F9bd0325B74802DF187935;
    address user_addr   = msg.sender;

    function register(){
        watch_addr.call(bytes4(sha3("register()")))
    }
}

(To save you from reading unnecessary code I've omitted the Mortal contract with its suicide function)

Now, calling the other function is not a problem using:

watch_addr.call(bytes4(sha3("register()")))

The problem is giving it arguments.

First, I can't parse an arguments with my proxy contract. Second, I'm afraid that even if I was able to hard code the proxy call:

watch_addr.call(bytes4(sha3("register("This text is hard codded")")))

and get my original contract to accept it and insert it into a variable, the address of that variable (msg.sender) will be the (msg.sender) of the proxy contract, and not the address of the user who uses this proxy contract.

I think I'm missing something about the way it suppose to work, any idea?

2 Answers 2

19

You can do:

/* MORTAL CONTRACT HERE */

contract Name is mortal{

    mapping(address=>string) public text;
    string public test;

    function register(string _text){
        text[msg.sender]    = _text;
    }
}

contract Proxy is mortal {

    address watch_addr  = 0xEB1e2c19bd833b7f33F9bd0325B74802DF187935;
    address user_addr   = msg.sender;

    function register(string _text){
        Name name = Name(watch_addr);
        name.register(_text);
    }
}

BUT

Be aware that although you're using a "proxy" contract the Namecontract could access the user_addr just looking at the tx.originvariable.

3
  • 13
    This may be obvious, but just to add to that: The only reason you need contract Name there above the Proxy contract is so that solidity can work out the function signature (function name, parameters and return values). So you could just as well leave out the actual implementation of contract Name - all you need there is contract Name { function register(string _text) {} } Apr 10, 2016 at 6:17
  • 2
    This method work when I'm defining the contract NAME again when I'm deploying the PROXY contract. What I'm trying to do is to use the PROXY contract to interact with a specific contract, that is already on the blockchain, without knowing how its source code might look like.
    – shultz
    Apr 10, 2016 at 8:19
  • wait... does Name name = Name(...) not call the constructor of the contract Name? Aug 5, 2017 at 6:02
7

You can call a function of an arbitrary contract like this

function register(string _text) {
    watch_addr.call(bytes4(sha3("register(string)")), _text);
}

Also check out the Difference between CALL, CALLCODE and DELEGATECALL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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