@iqai/mcp-bamm
Version:
MCP server for bamm (borrow automated market maker)
36 lines • 1.3 kB
JavaScript
import { erc20Abi } from "viem";
export async function ensureTokenApproval(tokenAddress, spenderAddress, amount, publicClient, walletClient) {
if (!walletClient.account) {
throw new Error("Wallet account is not set");
}
const userAddress = walletClient.account.address;
const currentAllowance = await publicClient.readContract({
address: tokenAddress,
abi: erc20Abi,
functionName: "allowance",
args: [userAddress, spenderAddress],
});
if (currentAllowance < amount) {
const { request: approveRequest } = await publicClient.simulateContract({
address: tokenAddress,
abi: erc20Abi,
functionName: "approve",
args: [spenderAddress, amount],
account: walletClient.account,
});
await walletClient.writeContract(approveRequest);
}
}
export async function checkTokenBalance(tokenAddress, userAddress, amount, publicClient) {
const balance = await publicClient.readContract({
address: tokenAddress,
abi: erc20Abi,
functionName: "balanceOf",
args: [userAddress],
});
if (balance < amount) {
throw new Error("Insufficient token balance");
}
return balance;
}
//# sourceMappingURL=token-utils.js.map