UNPKG

@contractjs/erc20

Version:

A TypeScript utility library for ERC20 contracts.

145 lines 5.33 kB
import { createPublicClient, erc20Abi, formatUnits, http, publicActions } from "viem"; import { approveCalldata, burnCalldata, burnFromCalldata, permitCalldata, transferCalldata, transferFromCalldata } from "./erc20Utils"; import { normalizeChain } from "./utils"; import { oracleAbi, PRICE_ORACLE_ADDRESSES } from "./constants"; // Read functions export const allowance = async (owner, spender, tokenAddress, publicClient) => { const allowance = await publicClient.readContract({ address: tokenAddress, abi: erc20Abi, functionName: "allowance", args: [owner, spender] }); return allowance; }; export const balanceOf = async (owner, tokenAddress, publicClient) => { return await publicClient.readContract({ address: tokenAddress, abi: erc20Abi, functionName: "balanceOf", args: [owner] }); }; export const balanceOfMultichain = async (owner, tokenAddresses, chains, rpcUrls) => { let totalBalance = 0n; if (chains && tokenAddresses) { for (const _chain of chains) { const index = chains.indexOf(_chain); const chain = normalizeChain(_chain); const client = createPublicClient({ chain, transport: http(rpcUrls?.[index] || ''), }); const balance = await client.readContract({ address: tokenAddresses[index], abi: erc20Abi, functionName: "balanceOf", args: [owner] }); totalBalance += balance; } } return totalBalance; }; export const getBalanceValueInUSD = async (tokenAddress, tokenDecimals, balance, publicClient) => { if (!publicClient.chain) { throw new Error('Public client must have a chain'); } const chainId = publicClient.chain.id; const price = await publicClient.readContract({ address: PRICE_ORACLE_ADDRESSES[chainId], abi: oracleAbi, functionName: 'getAssetPrice', args: [tokenAddress], }); const formattedBalance = formatUnits(balance, tokenDecimals); const userBalanceNum = Number(formattedBalance); const usdValue = (userBalanceNum * Number(price) / 1e8); return usdValue; }; // Write functions export const approve = async (spender, amount, tokenAddress, walletClient) => { const extendedWalletClient = walletClient.extend(publicActions); const data = approveCalldata(spender, amount); const hash = await walletClient.sendTransaction({ account: walletClient.account, to: tokenAddress, chain: walletClient.chain, data }); return { hash, waitForReceipt: () => extendedWalletClient.waitForTransactionReceipt({ hash }) }; }; export const transfer = async (to, amount, tokenAddress, walletClient) => { const extendedWalletClient = walletClient.extend(publicActions); const data = transferCalldata(to, amount); const hash = await walletClient.sendTransaction({ account: walletClient.account, to: tokenAddress, chain: walletClient.chain, data }); return { hash, waitForReceipt: () => extendedWalletClient.waitForTransactionReceipt({ hash }) }; }; export const transferFrom = async (from, to, amount, tokenAddress, walletClient) => { const extendedWalletClient = walletClient.extend(publicActions); const data = transferFromCalldata(from, to, amount); const hash = await walletClient.sendTransaction({ account: walletClient.account, to: tokenAddress, chain: walletClient.chain, data }); return { hash, waitForReceipt: () => extendedWalletClient.waitForTransactionReceipt({ hash }) }; }; export const burn = async (value, tokenAddress, walletClient) => { const extendedWalletClient = walletClient.extend(publicActions); const data = burnCalldata(value); const hash = await walletClient.sendTransaction({ account: walletClient.account, to: tokenAddress, chain: walletClient.chain, data }); return { hash, waitForReceipt: () => extendedWalletClient.waitForTransactionReceipt({ hash }) }; }; export const burnFrom = async (account, value, tokenAddress, walletClient) => { const extendedWalletClient = walletClient.extend(publicActions); const data = burnFromCalldata(account, value); const hash = await walletClient.sendTransaction({ account: walletClient.account, to: tokenAddress, chain: walletClient.chain, data }); return { hash, waitForReceipt: () => extendedWalletClient.waitForTransactionReceipt({ hash }) }; }; export const permit = async (owner, spender, value, deadline, v, r, s, tokenAddress, walletClient) => { const extendedWalletClient = walletClient.extend(publicActions); const data = permitCalldata(owner, spender, value, deadline, v, r, s); const hash = await walletClient.sendTransaction({ account: walletClient.account, to: tokenAddress, chain: walletClient.chain, data }); return { hash, waitForReceipt: () => extendedWalletClient.waitForTransactionReceipt({ hash }) }; }; //# sourceMappingURL=erc20.js.map