UNPKG

@coinbase/agentkit

Version:

Coinbase AgentKit core primitives

88 lines (87 loc) 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.approve = approve; exports.applyGasMultiplier = applyGasMultiplier; exports.retryWithExponentialBackoff = retryWithExponentialBackoff; const viem_1 = require("viem"); const viem_2 = require("viem"); /** * Approves a spender to spend tokens on behalf of the owner * * @param wallet - The wallet provider * @param tokenAddress - The address of the token contract * @param spenderAddress - The address of the spender * @param amount - The amount to approve in atomic units (wei) * @returns A success message or error message */ async function approve(wallet, tokenAddress, spenderAddress, amount) { try { const data = (0, viem_1.encodeFunctionData)({ abi: viem_2.erc20Abi, functionName: "approve", args: [spenderAddress, amount], }); const txHash = await wallet.sendTransaction({ to: tokenAddress, data, }); await wallet.waitForTransactionReceipt(txHash); return `Successfully approved ${spenderAddress} to spend ${amount} tokens`; } catch (error) { return `Error approving tokens: ${error}`; } } /** * Scales a gas estimate by a given multiplier. * * This function converts the gas estimate to a number, applies the multiplier, * rounds the result to the nearest integer, and returns it as a bigint. * * @param gas - The original gas estimate (bigint). * @param multiplier - The factor by which to scale the estimate. * @returns The adjusted gas estimate as a bigint. */ function applyGasMultiplier(gas, multiplier) { return BigInt(Math.round(Number(gas) * multiplier)); } /** * Utility function to sleep for a given number of milliseconds * * @param ms - Number of milliseconds to sleep * @returns Promise that resolves after the specified delay */ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); /** * Retry function with exponential backoff * * @param fn - The function to retry * @param maxRetries - Maximum number of retries (default: 3) * @param baseDelay - Base delay in milliseconds for retries (default: 1000) * @param initialDelay - Initial delay before the first attempt in milliseconds (default: 0) * @returns Promise that resolves with the function result or rejects with the last error */ async function retryWithExponentialBackoff(fn, maxRetries = 3, baseDelay = 1000, initialDelay = 0) { let lastError; // Wait before the first attempt if initialDelay is specified if (initialDelay > 0) { await sleep(initialDelay); } for (let attempt = 0; attempt <= maxRetries; attempt++) { try { return await fn(); } catch (error) { lastError = error; // If this was the last attempt, throw the error if (attempt === maxRetries) { throw lastError; } // Wait after failed attempt with exponential backoff // Calculate delay with exponential backoff: baseDelay * 2^attempt const delay = baseDelay * Math.pow(2, attempt); await sleep(delay); } } throw lastError; }