UNPKG

viem-erc20

Version:
36 lines (33 loc) 928 B
import { type Address, Client, erc20Abi, isAddress } from "viem"; import { writeContract } from "viem/actions"; export const approveErc20Token = async function ( client: Client, parameters: { address: Address; amount: bigint; spender: Address; }, ) { const { address, amount, spender } = parameters; if (!isAddress(address)) { throw new Error("Invalid address"); } if (typeof amount !== "bigint") { throw new Error("Invalid amount"); } if (!isAddress(spender)) { throw new Error("Invalid spender address"); } if (amount <= BigInt(0)) { throw new Error("Invalid amount, must be greater than 0"); } return writeContract(client, { abi: erc20Abi, // @ts-expect-error: TS is complaining about client.account definition, but this works account: client.account, address, args: [spender, amount], chain: client.chain, functionName: "approve", }); };