We will give multiple examples here of how you are supposed to pass the function parameters for different parameter types. It will help you figure out how you should format the data you have to provide when calling the smart contract function. The formatting is quite easy and straightforward and as a matter of fact, the first idea that comes up to your mind is probably the correct choice (other than maybe more complex scenarios when encoding structs).
constcontractInstance; // fetched from sdk (explained in section before)constfunction1Result=awaitcontractInstance.read('myFunctionWithNoParameters',// solidity function name [ ] // empty array because because function accepts 0 parameters);constfunction2Result=awaitcontractInstance.read('myFunctionWithOneParameter',// solidity function name [ 123 ] // one number passed as parameter);constfunction3Result=awaitcontractInstance.read('myFunctionWithOneParameter',// solidity function name [ 123,'test string' ] // one number and one string passed as parameters);
Example 2: Arrays as parameters
👉🏻 Example function signature:
// Solidity Contract signaturesfunctionmyFunction(uint256[] a,uint256[] b) externalviewreturns (uint256) {}
⚡️ How you call the functions using the SDK:
constcontractInstance; // fetched from sdk (explained in section before)constfunctionResult=awaitcontractInstance.read('myFunction',// solidity function name [ [1,2,3],// first parameter: array of numbers [4,5,6] // second parameter: array of numbers ]);
constcontractInstance; // fetched from sdk (explained in section before)constfunctionResult=awaitcontractInstance.read('myFunction', [ [ 'random name',13,42 ] // struct is encoded as an array of elements ]);
Example 4: Arrays of structs as parameters
Now let's say the function from example above accepts not one but an array of elements of type DataPoint.
constcontractInstance; // fetched from sdk (explained in section before)constfunctionResult=awaitcontractInstance.read('myFunction', [ [ 'random name 1',13,42 ],// first struct element [ 'random name 2',22,315 ],// second struct element [ 'random name 3',0,1 ] // third (and final) element in a list ]);