1

I have two functions on contract:

function 1 receives bytes params variable

function foo1(bytes memory params)

function 2 receives two params: address param1, uint256 param2

function foo2(address param1, uint256 param2)

my goals somehow pass params as bytes to foo1, decode it on function, and pass decoded params to foo2

like:

function foo2(address param1, uint256 param2) {
   // do something
}

function decode(bytes params) private returns(???){
  // decode
}

function foo1(bytes params) public {
  var decodedParams = fromBytes(params)
  foo2(decodedParams.param1, decodedParams.param2)
}

from frontend I expect something like:

  const params = toBytes({param1: '0xAddRess', param2: 1})

  myContract.foo1(params)
3

You can use the Solidity built-in function abi.decode for this.

(address param1, uint256 param2) = abi.decode(params, (address, uint256));

For a real-world example, see how I use abi.decode in a flash swap contract that pulls liquidity from Uniswap V2: HifiFlashUniswapV2.

1
  • exactly what I am looking for, thanks :) Oct 13 at 8: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.