1

Is there a generic way to simply take an ABI function, something like:

{
   "name":"foo",
   "inputs":{
      "interalType":"uint256",
      "name":"bar",
      "type":"uint256"
   },
   "outputs":{
      "interalType":"uint256",
      "name":"baz",
      "type":"uint256"
   }
}

and have it encode the function signature in web3j? In web3js this seems to be incredibly simple, something like:

const eth = new Eth()
eth.abi.encodeFunctionSignature(myFunction)

Where myFunction is the section of the abi. Is there no way to do this in web3j? Or am I just missing something basic? I thought I might be able to do if I build up a Function object and then use the DefaultFunctionEncoder and then make a call to encodeFunction, but I don't see an easy way to do this on the fly. All examples I've see are using pre-built solidity contracts.

Is there a generic way to mimic what the javascript is doing?

Update:

I was able to 'mostly' do this by creating a horrible function containing a switch statement on the type and iterating through each potential type to generate a 'default version of the object. For instance return new Uint256(BigInteger.ZERO); sort of thing. It's hideous, and doesn't handle tuples (mostly because I haven't figured out how to handle those yet), but when I build my inputs that way for the function, when I encode it the value does match was web3js gives on that.

There 'has' to be a better way to handle this.

Update 2: Tuple types seem to be working now, but now I've discovered that working with byte32[] type, the signatures seem to differ from web3js and web3j. I'm having my object creator return Bytes32.DEFAULT as the Type in that case, but I'm not sure this is correct.

0

AFAIK, there is no way to accomplish what you're trying directly using web3j. However, what you can do is generate the contract wrappers, copy the generated Functions definitions. Then, encode/decode the calls as we are doing in the Encoder/Decoder tests.

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.