hashgraph / hedera-sdk-reference

Hedera SDK specification repository.
Apache License 2.0
6 stars 1 forks source link

ABI support in SDKs #140

Open ochikov opened 1 year ago

ochikov commented 1 year ago

Problem

Across SDKs, we've got the following functionalities:

All those flows use ContractFunctionParameters(). In the ContractFunctionParameters() we’ve got the following methods:

and so on…. a file with 1700 lines and a lot of methods, switch cases, and IFs.

The workflow is as follows:

Then we got ContractFunctionSelector file with 1400 lines of code.

We are using that file with all the helper methods inside to create the signature of the function.

For example:

case ArgumentType.uint8:
            s = "uint8";

This all is created because we are not requiring the ABI of a contract when we are doing:

The above workflow assumes that the user will know the exact parameters that a function is accepting when performing the above calls. According to my opinion, this could lead to errors and unpredicted behavior because of the missing or wrong passed params.

We can use the ABI of the contract and then pass params. We can leave the transforming of the contract function params to bytes to the SDK and then decode them back to the SDK.

For example, when we are retrieving results from:

we need to decode manually the response again using similar functions as in ContractFunctionParameters :

As you can see, this is not convenient, because the users must know the signature of the functions (what is accepting and what is returning).

Solution

Encoding and passing data to contract

callFunction
.setAbi(setDataContract.abi)

Decoding and retrieving data from the contract

I’ve implemented a method called:

const result = txResponse.getResult(["uint32", "uint64", "string"]);

Where you can pass the signature of the function (from the ABI) and use it to decode and return the result.

The method getResult is tested in production (actually it is released because a lof of people had problems decoding data from contract calls).

Alternatives

Notes from the last meeting:

For passing the input:

For getting the output:

For different SDKs: