Call Functions
If the functionality of your smart contract does not come under one of the extension interfaces,
you can use the generic call
method to call any function on the smart contract. This works for both read and write operations.
- If your function is a read operation, i.e. no transaction takes place, you can get the data returned by that function call by awaiting the result of the
call
. - If your function is a write operation, i.e. a transaction is required, the result of that transaction can be read by awaiting the result of the
call
. The call will be awaited until the transaction is mined and the transaction receipt is available.
Usage
const data = await contract.call(
"myFunctionName", // Name of your function as it is on the smart contract
// Arguments to your function, in the same order they are on your smart contract
[
"arg1", // e.g. Argument 1
"arg2", // e.g. Argument 2
],
);
If you have cached the ABI of your smart contract using thirdweb generate
,
the functionName
and args
parameters are strongly typed according to your smart contract’s ABI.
Configuration
functionName
The name of the function, view, mapping, variable, etc. on your smart contract.
const data = await contract.call(
"myFunctionName", // Name of your function as it is on the smart contract
);
args
The arguments to the function/variable, in the same order they are on your smart contract.
If you provide too few or too many arguments, the function will throw an error.
const data = await contract.call(
"myFunctionName", // Name of your function as it is on the smart contract
[
"arg1", // e.g. Argument 1
"arg2", // e.g. Argument 2
],
);
overrides
Optionally override the default transaction options by providing an overrides object as the final, additional argument.
const data = await contract.call(
"myFunctionName", // Name of your function as it is on the smart contract
[
"arg1", // e.g. Argument 1
"arg2", // e.g. Argument 2
],
{
gasLimit: 1000000, // override default gas limit
value: ethers.utils.parseEther("0.1"), // send 0.1 ether with the contract call
},
);