Skip to content
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

Open
iron4548 opened this issue Oct 21, 2023 · 2 comments
Open

Add support for struct parameter for contract calls #1993

iron4548 opened this issue Oct 21, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request p2

Comments

@iron4548
Copy link

iron4548 commented Oct 21, 2023

Problem

Let's assume I have a solidity contract with the following code

struct InputParams {
  uint256 value1;
  uint256 value2; 
}

function doCalc(InputParams calldata params) external returns (uint256 result);

The correct method signature for this is:
doCalc((uint256,uint256)) which has a hash of 0x07328a21

The other method signature (the incorrect one) is:
doCalc(uint256,uint256) which has a hash of 0x88e5e21d

The following code:

const params = new ContractFunctionParameters();
params.addUint256(5); //value1
params.addUint256(10); //value2

 const result = await new ContractExecuteTransaction()
      .setContractId(mathContractId)
      .setGas(gasGwei)
      .setFunction('doCalc', params)
      .execute(client);

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

@iron4548 iron4548 added the enhancement New feature or request label Oct 21, 2023
@iron4548
Copy link
Author

iron4548 commented Oct 22, 2023

A possible solution (if one doesn't exist):

  • Introduce a new ContractStructParameter class
  • Add the the usual methods such as addBytes, addAddress, addUint256 etc.
  • Then introduce a new method called addStruct(param:ContractStructParameter) to ContractFunctionParameters
//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

@iron4548
Copy link
Author

iron4548 commented Oct 22, 2023

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);

@SimiHunjan SimiHunjan added this to the 2.38.0 milestone Nov 1, 2023
@svetoslav-nikol0v svetoslav-nikol0v self-assigned this Nov 10, 2023
@SimiHunjan SimiHunjan added the p2 label Nov 13, 2023
@SimiHunjan SimiHunjan modified the milestones: 2.38.0, 2.39.0 Nov 20, 2023
@SimiHunjan SimiHunjan modified the milestones: 2.39.0, 2.40.0 Dec 4, 2023
@SimiHunjan SimiHunjan modified the milestones: 2.40.0, 2.41.0 Dec 14, 2023
@SimiHunjan SimiHunjan modified the milestones: 2.41.0, 2.42.0 Feb 5, 2024
@SimiHunjan SimiHunjan modified the milestones: 2.42.0, 2.43.0 Feb 20, 2024
@SimiHunjan SimiHunjan removed this from the 2.43.0 milestone Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request p2
Projects
None yet
Development

No branches or pull requests

3 participants