approve()

function approve(client, props): Promise<undefined | `0x${string}`>

Approve the given spender to transfer the given amount of tokens from the given account.

If the address is not the zero address, it calls the approve function of the specified ERC20 contract to approve the given amount. If the address is the zero address, it does nothing and returns undefined.

Parameters

ParameterTypeDescription
clientWriteClientWriteClient The write client to use.
propsERC20ApprovePropsERC20ApproveProps The properties containing the address, spender, amount, and account.
type ERC20ApproveProps: WriteContractBaseProps & {
  address: Address;
  spender: Address;
  amount: bigint;
};

Returns

Promise<undefined | `0x${string}`>

The result of the approve call if the address is valid, otherwise undefined.

Examples

erc20.ts
// for default amount
import { erc20 } from '@solio/core';
import { createWalletClient, custom } from 'viem';
import { sepolia } from 'viem/chains';
const writeClient = createWalletClient({
 chain: sepolia,
 transport: custom(window.ethereum),
});
const hash = await erc20.write.approve(writeClient, {
  address: '0x...', // ERC20 contract address
  spender: '0x...'
});
console.log(hash);
erc20.ts
// for custom amount (100wei)
import { erc20 } from '@solio/core';
import { createWalletClient, custom } from 'viem';
import { sepolia } from 'viem/chains';
const writeClient = createWalletClient({
 chain: sepolia,
 transport: custom(window.ethereum),
});
const hash = await erc20.write.approve(writeClient, {
 address: '0x...', // ERC20 contract address
 spender: '0x...',
 amount: 100n // 100 wei
});
console.log(hash);