9

Hello I am trying to build a proxy contract and I've seen two approaches:

1- "Specific" proxy function, where the proxy function can only call a specific function of the other contract.

Ex: (taken from this post)

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

2- "Generic" proxy function, where the function can be used to call any other function of any other contracts:

contract Proxy is Owned {
event Forwarded (
    address indexed destination,
    uint value,
    bytes data
);

function () payable{}

function forward(address destination, uint value, bytes data) onlyOwner {
    if (!destination.call.value(value)(data)) {
        throw;
    }
    Forwarded(destination, value, data);
}}

I have two questions:

1 - What are the advantages/disadvantages of the two techniques?

2 - How does one uses "Generic" proxy functions? I'm not sure how I construct the parameters value and data required to run the transaction. I'm also not sure if I need to fund the contract before (my guess is yes)

1

1 - What are the advantages/disadvantages of the two techniques?

The advantage of the first is, that you don't have to build bytes data yourself, the disadvantage is that you have to change it every time for every new function at the destination.

2 - How does one uses "Generic" proxy functions? I'm not sure how I construct the parameters value and data required to run the transaction.

In web3, having the ABI, you can encode the function call. See https://web3js.readthedocs.io/en/1.0/web3-eth-abi.html#encodefunctioncall

Alternatively, if you need to call some other code before delegating, you can wrap it like this (example without sending value):

contract TargetInterface {
    function(uint x) public;
}

contract Caller is TargetInterface {
    GenericProxy p;
    address target;

    function(uint x) public {
        // ..CODE BEFORE..
        p.forward(target, msg.data)
        // ..CODE AFTER..
    }
}

contract Target is TargetInterface {
    function(uint x) public {...}
}

Finally you can use the abi object in Solidity to craft your own message data: https://solidity.readthedocs.io/en/v0.4.24/units-and-global-variables.html

I'm also not sure if I need to fund the contract before (my guess is yes).

To forward the value, you have to send the value to the Proxy first or make forward payable.

  • How could we decode msg.data in p.forward() function call @ivicaa – alper Mar 12 at 17:39

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.