New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for struct parameter for contract calls #1993
Labels
Comments
A possible solution (if one doesn't exist):
//example what it could look like
const struct = ContractStructParameter();
struct.addUint256(5); //value 1
struct.addUint256(10); //value 2
const params = new ContractFunctionParameters();
params.addStruct(struct);
const result = await new ContractExecuteTransaction()
.setContractId(mathContractId)
.setGas(gasGwei)
.setFunction('doCalc', params)
.execute(client);
//the above will then get reduced to
`doCalc((uint256,uint256))`, method hash = 0x07328a21 |
I found a suitable workaround using Ethers.js, so this 'feature request' is now a low priority. But would be nice to have it built into the SDK one day to make it easier Here's how I did it for anyone who's interested. import * as ethers from 'ethers'; //V6
const abi = [
{
'name': 'doCalc',
'inputs': [
{
'name': 'params',
'type': 'tuple',
'components': [
{ 'name': 'value1', 'type': 'uint256' },
{ 'name': 'value2', 'type': 'uint256' },
]
}
],
'outputs': [{ 'name': 'result', 'type': 'uint256' }],
'type': 'function'
}
];
const abiInterface = new ethers.Interface(abi);
const params = {
value1: 5,
value2: 10
};
const encodedData = abiInterface.encodeFunctionData('doCalc', [params]);
const encodedDataAsUint8Array = hexToUint8Array(encodedData.substring(2)); //remove leading 0x
const result = await new ContractExecuteTransaction()
.setContractId(ContractId.fromString(config.mathContract))
.setGas(gasGwei)
.setFunctionParameters(encodedDataAsUint8Array)
.execute(client); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem
Let's assume I have a solidity contract with the following code
The correct method signature for this is:
doCalc((uint256,uint256))
which has a hash of 0x07328a21The other method signature (the incorrect one) is:
doCalc(uint256,uint256)
which has a hash of 0x88e5e21dThe following code:
Will fail because that gets reduced to:
doCalc(uint256,uint256)
(0x88e5e21d).How can I reduce it to:
doCalc((uint256,uint256))
(0x07328a21)`Does ContractFunctionParameters / ContractExecuteTransaction have support for Struct based parameters? If so, how can I achieve it? If not, can it be added? (the enhancement)
Solution
Alternatives
No response
The text was updated successfully, but these errors were encountered: