@avalanche-sdk/client
Version:
A TypeScript SDK for interacting with the Avalanche network through JSON-RPC APIs. This SDK provides a comprehensive set of tools to interact with all Avalanche chains (P-Chain, X-Chain, C-Chain) and various APIs, including wallet functionality for transa
36 lines (30 loc) • 1.06 kB
text/typescript
import { waitForTransactionReceipt, writeContract } from "viem/actions";
import { parseAvalancheAccount } from "../../../accounts/utils/parseAvalancheAccount";
import { AvalancheWalletCoreClient } from "../../../clients/createAvalancheWalletCoreClient";
import { erc20ABI } from "../abis/erc20";
import {
ApproveErc20Parameters,
ApproveErc20ReturnType,
} from "./types/approveErc20";
export async function approveErc20(
client: AvalancheWalletCoreClient,
params: ApproveErc20Parameters
): Promise<ApproveErc20ReturnType> {
const { contractAddress, spender, amount, abi, account } = params;
const acc = parseAvalancheAccount(account);
if (!acc && !client.account) {
throw new Error("no account found");
}
const txHash = await writeContract(client, {
account: acc?.evmAccount,
abi: abi ?? erc20ABI,
functionName: "approve",
args: [spender, amount],
address: contractAddress,
} as any);
const tx = await waitForTransactionReceipt(client, { hash: txHash });
return {
status: tx.status,
txHash,
};
}