Skip to main content

approve

The approve method is used by token holders to give permission to another Ethereum address (typically a contract) to spend a specified amount of tokens on their behalf. This is a common operation in various Ethereum protocols and dApps, especially when tokens are used as part of another system (like decentralized exchanges or lending platforms). The method belongs to the ERC20Instance object, which is returned by the getERC20Instance method of EdexaClient.

Method Signature

approve(spender: string, amount: BigNumberish, signer: WalletSigner): Promise<TransactionResponse>

Parameters

ParameterTypeDescription
spenderstringThe Ethereum address that will be given permission to spend the specified amount of tokens on the owner's behalf.
amountBigNumberishThe maximum number of tokens that the spender can transfer from the owner. This value is specified in the smallest unit of the token (e.g., Wei for Ether). It can be provided as a number, string, or BigNumber.
signerWalletSignerThe wallet signer instance responsible for signing the transaction. Typically generated using the createWalletSigner method of EdexaClient.

Returns

  • A Promise that resolves to a TransactionResponse object. This object provides details about the transaction, such as its status, transaction hash, and more.

Usage Example

First, instantiate the desired ERC20 token contract using its contract address:

const erc20 = edexaClient.getERC20Instance("0x4DB67190e915C15aA8CCd889F35185D73dA37878");

Then, use the approve method to set an allowance for a specific address:

const spenderAddress = "0x2c360D20cE6b3D8b466511eF093C9177c3817B94";
const approvalAmount = "2000000000000000000"; // This represents 2 tokens, assuming 18 decimals
const signer = edexaClient.createWalletSigner("your private key");

const transactionResponse = await erc20.approve(spenderAddress, approvalAmount, signer);
console.log(`Transaction Hash: ${transactionResponse.hash}`);

Notes

  • The amount is specified in the smallest divisible unit of the token. Ensure correct conversions before determining the approval amount.

  • Safeguard your private keys. Always handle the signer securely and avoid exposing private keys in insecure or client-side environments.