What I am trying to achieve is like , i should be able to call a set of solidity functions dynamically based on some criteria. i'm using call() method to achieve the same.
(bool success, bytes memory result) =APContract.call(abi.encodeWithSignature(string(data), "hello"));
data
variable contain the function call string "testCall(string)". instead of encoding from the contract I should be able to encode the same with javascript (because of some argument generation logic). so that i can call the function like APContract.call(data)
. Is there any method that help to achieve the same?
Best Answer
Not sure if I totally understood your question but you can take a look at the Solidity Documentation Contract ABI Specification: https://docs.soliditylang.org/en/latest/abi-spec.html#examples
To see how to encode the parameters on your own. It is basically a big concatenated strings set of function name hash and function arguments.
You can also take a look at this lib which performs "custom contract calls" (so not relying on any ABI): https://github.com/ERC725Alliance/erc725.js/blob/main/src/lib/utils.ts
Manual way
ABI way
You can always construct your own ABI dict, then web3 will handle the above conversion for you:
ABI utils way
Use the helpers fonctions from: https://web3js.readthedocs.io/en/v1.3.0/web3-eth-abi.html to build what you need.
Edit: you can also check web3 source code