getApprovalForTransaction

When dealing with transactions that involve ERC20 tokens (Airdropping ERC20, buy NFTs with ERC20, etc.) you often have to do a pre-check to see if the targeted contract has the sufficient allowance to "take" the ERC20 tokens from the caller's wallet.

This extension is a handy method that checks for the allowance and requests to approve for more if current allowance is insufficient

Example

import { getApprovalForTransaction } from "thirdweb/extensions/erc20";
import { sendAndConfirmTransaction } from "thirdweb";
async function buyNFT() {
const buyTransaction = ... // could be a marketplacev3's buyFromListing
// Check if you need to approve spending for the involved ERC20 contract
const approveTx = await getApprovalForTransaction({
transaction: buyTransaction,
account, // the connected account
});
if (approveTx) {
await sendAndConfirmTransaction({
transaction: approveTx,
account,
})
}
// Once approved, you can finally perform the buy transaction
await sendAndConfirmTransaction({
transaction: buyTransaction,
account,
});
}
function getApprovalForTransaction(
): Promise<PreparedTransaction | null>;

Parameters

GetApprovalForTransactionParams

Type

let options: { account: Account; transaction: PreparedTransaction };

Returns

let returnType: Readonly<options> & {
__contract?: ThirdwebContract<abi>;
__preparedMethod?: () => Promise<PreparedMethod<abiFn>>;
};

a PreparedTransaction