1

Metamask (or other browser plug-in) helps a lot the user to submit transactions using his personal account since it manages his private keys.

When interacting with an dapp front-end, web3 will create a transaction and the user can easily sign it and submit it with his personal account using metamask.

For example, the user can easily submit a transaction to breed kitties on cryptokitties if he owns them.

In this similar context of web3 creating a transaction to sign, what is the feasible way to submit the transaction using a wrapper smart contract as a relay to the user personal account? In other words, the personal account signs a transaction to a wrapper smart contract and this wrapper smart contract calls the dapp.

It could be useful to do this if this wrapper smart contract owns the kitties (and not the user personal account) for example. In this case, the personal account can be the owner of the smart contract.

The relay function of the wrapper contract could be a function like this.

function a (address to, uint256 valueInWei, bytes hexData) { require( to.call.value(valueInWei)(hexData), "failed" );
}

hexdata in this case is composed by the name and arguments of the function-to-be-called.

1 Answer 1

1

You can use the encodeParameters function of web3.js function to convert the function arguments into a hexadecimal string. Here's a documentation link:

https://web3js.readthedocs.io/en/1.0/web3-eth-abi.html#encodeparameters

You will have to put the 4 byte function signature before this parameter data. You can get that using the encodefunctionsignature function:

https://web3js.readthedocs.io/en/1.0/web3-eth-abi.html#encodefunctionsignature

Then you can call the a relay function with all that as the hexData argument like you would normally call a smart contract function.

I hope this helps :)

Edit: I just found the encodeFunctionCall function which does both of these and combines them for you:

https://web3js.readthedocs.io/en/1.0/web3-eth-abi.html#encodefunctioncall

1
  • It helps a lot! However, I was trying to have a solution without changing the original dapp. My ideal solution was: metamask+web3 (or similar) support the user to indicate that he wants to answer that call with a contract (and not only with personal account). So, the user can choose not only between his personal accounts, but also between his contracts (if there is a proper function to use in that smart contract). Commented Dec 16, 2018 at 8:47

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.