14

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?

16

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.

  • 11
    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) {} } – Edmund Edgar Apr 10 '16 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 '16 at 8:19
  • wait... does Name name = Name(...) not call the constructor of the contract Name? – Richard Barker Aug 5 '17 at 6:02
6

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.

0

Im a solidity noob of sorts. Im trying these examples and my expected results are that when i call the register() func in the proxy contract with a string and then call the same func in the original contract with the address of the account that set the data in the proxy, that the text is displayed.. instead i get nothing...

Am I missing something or is there a way to call a function in an already deployed contract to get its return value and then ammend some value to

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.