UNPKG

pharos-agent-kit

Version:
66 lines 2.64 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.approve = approve; exports.applyGasMultiplier = applyGasMultiplier; 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)); } __exportStar(require("../utils/formatPharos"), exports); __exportStar(require("../utils/getTokenDecimals"), exports); __exportStar(require("../utils/isContract"), exports); __exportStar(require("../utils/getBalance"), exports); __exportStar(require("../utils/approveTokens"), exports); //# sourceMappingURL=index.js.map