UNPKG

@tristeroresearch/mach-sdk

Version:

A TypeScript SDK for integrating with Mach's API.

37 lines (36 loc) 1.69 kB
import { createWalletClients } from './createWalletClients.util'; import { getChainFromAssetAddress } from './getChainFromAssetAddress.util'; import { abi } from '../contracts/index.js'; import { readContract } from 'viem/actions'; import { attemptToLoadPrivateKeyFromEnv } from './attemptToLoadPrivateKeyFromEnv.util'; /** * A helper function to retrieve the balance of a specified token for a wallet using the private key from environment variables. * @param asset - The asset to get the balance of * @returns The balance of the specified token * @description This helper function retrieves the balance of a specified token for a wallet using the private key from environment variables. */ export const tokenBalance = async (asset, privateKey) => { //Throws an error if the private key is not found in the environment if (!privateKey) privateKey = attemptToLoadPrivateKeyFromEnv(privateKey); const chain = await getChainFromAssetAddress(asset.address); const clients = await createWalletClients(chain, privateKey); const publicClient = clients.publicClient; const account = clients.account; const balance = await readContract(publicClient, { address: asset.address, abi: abi.erc20, functionName: 'balanceOf', args: [account.address], }); return balance; }; /** * A type guard function to check if a contract has a read function * @param contract - The contract to check * @returns True if the contract has a read function, false otherwise * @description This type guard function checks if a contract has a read function. */ function hasReadFunction(contract) { return contract.read !== undefined; }