Welcome to Dev3
Search
⌃K

Function Parameters

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

Example 1: Primitive types as parameters

👉🏻 Example function signatures:
// Solidity Contract signatures
function myFunctionWithNoParameters() external view returns (uint256) {}
function myFunctionWithOneParameter(uint256 x) view returns (uint256) {}
function myFunctionWithTwoParameters(uint256 x, string memory y) view returns (uint256) {}
⚡️ How you call the functions using the SDK:
const contractInstance; // fetched from sdk (explained in section before)
​
const function1Result = await contractInstance.read(
'myFunctionWithNoParameters', // solidity function name
[ ] // empty array because because function accepts 0 parameters
);
const function2Result = await contractInstance.read(
'myFunctionWithOneParameter', // solidity function name
[ 123 ] // one number passed as parameter
);
const function3Result = await contractInstance.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 signatures
function myFunction(uint256[] a, uint256[] b) external view returns (uint256) {}
⚡️ How you call the functions using the SDK:
const contractInstance; // fetched from sdk (explained in section before)
​
const functionResult = await contractInstance.read(
'myFunction', // solidity function name
[
[1,2,3], // first parameter: array of numbers
[4,5,6] // second parameter: array of numbers
]
);

Example 3: Structs as parameters

👉🏻 Example function signature:
// Solidity Contract signatures
struct DataPoint {
string name;
uint256 frequency;
uint256 timestamp;
}
function myFunction(DataPoint memory point) external view returns (uint256) {}
⚡️ How you call the functions using the SDK:
const contractInstance; // fetched from sdk (explained in section before)
​
const functionResult = await contractInstance.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.
👉🏻 Example function signature:
// Solidity Contract signatures
struct DataPoint {
string name;
uint256 frequency;
uint256 timestamp;
}
function myFunction(DataPoint[] memory data) external view returns (uint256) {}
⚡️ How you call the functions using the SDK:
const contractInstance; // fetched from sdk (explained in section before)
​
const functionResult = await contractInstance.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
]
);