Updated API for calling smart contract functions
The API for interacting with smart contracts via the thirdweb React & TypeScript SDKs has been updated in favor of explicitly defining function arguments and (optionally) call overrides.
Using the contract.call
function in the TypeScript SDK now looks like the following:
const owner = "0x...";
const operator = "0x...";
const overrides = { gasPrice: 800000 };
const res = await contract.call("approve", [owner, operator], overrides);
Note that the arguments to the contract function are now passed in as an array as the second parameter to contract.call
, and the overrides
are passed optionally as the last parameter.
Similarly, the useContractRead
and useContractWrite
hooks in the React SDK have an updated API:
const { contract } = useContract(contractAddress);
// Args is now passed in as an array
const { data } = useContractRead(contract, "isApproved", [operator], overrides);
// Note that args and overrides are now passed into an object
const { mutateAsync } = useContractWrite(contract, "approve")
mutateAsync({ args: [operator], overrides: { ... } });
These changes make the API more conducive to having static types on contract arguments when contract ABIs are available, allowing for strongly typed contract calls for every function.
Keep an eye out for the thirdweb generate
feature coming out soon that takes advantage of this to give static typing for all of your contracts!