UNPKG

@getclave/lifi-sdk

Version:

LI.FI Any-to-Any Cross-Chain-Swap SDK

89 lines (88 loc) 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.revokeTokenApproval = exports.setTokenAllowance = exports.setAllowance = void 0; const viem_1 = require("viem"); const actions_1 = require("viem/actions"); const utils_1 = require("viem/utils"); const isZeroAddress_1 = require("../../utils/isZeroAddress"); const abi_1 = require("./abi"); const getAllowance_1 = require("./getAllowance"); const utils_2 = require("./utils"); const setAllowance = async (client, tokenAddress, contractAddress, amount, executionOptions, returnPopulatedTransaction) => { const data = (0, viem_1.encodeFunctionData)({ abi: abi_1.approveAbi, functionName: 'approve', args: [contractAddress, amount], }); if (returnPopulatedTransaction) { return data; } let transactionRequest = { to: tokenAddress, data, maxPriorityFeePerGas: client.account?.type === 'local' ? await (0, utils_2.getMaxPriorityFeePerGas)(client) : undefined, }; if (executionOptions?.updateTransactionRequestHook) { const customizedTransactionRequest = await executionOptions.updateTransactionRequestHook({ requestType: 'approve', ...transactionRequest, }); transactionRequest = { ...transactionRequest, ...customizedTransactionRequest, }; } return (0, utils_1.getAction)(client, actions_1.sendTransaction, 'sendTransaction')({ to: transactionRequest.to, account: client.account, data: transactionRequest.data, gas: transactionRequest.gas, gasPrice: transactionRequest.gasPrice, maxFeePerGas: transactionRequest.maxFeePerGas, maxPriorityFeePerGas: transactionRequest.maxPriorityFeePerGas, }); }; exports.setAllowance = setAllowance; /** * Set approval for a certain token and amount. * @param request - The approval request * @param request.walletClient - The Viem wallet client used to send the transaction * @param request.token - The token for which to set the allowance * @param request.spenderAddress - The address of the spender * @param request.amount - The amount of tokens to approve * @returns Returns Hash or nothing */ const setTokenAllowance = async ({ walletClient, token, spenderAddress, amount, }) => { // native token don't need approval if ((0, isZeroAddress_1.isNativeTokenAddress)(token.address)) { return; } const approvedAmount = await (0, getAllowance_1.getAllowance)(walletClient, token.address, walletClient.account.address, spenderAddress); if (amount > approvedAmount) { const approveTx = await (0, exports.setAllowance)(walletClient, token.address, spenderAddress, amount); return approveTx; } }; exports.setTokenAllowance = setTokenAllowance; /** * Revoke approval for a certain token. * @param request - The revoke request * @param request.walletClient - The Viem wallet client used to send the transaction * @param request.token - The token for which to revoke the allowance * @param request.spenderAddress - The address of the spender * @returns Returns Hash or nothing */ const revokeTokenApproval = async ({ walletClient, token, spenderAddress, }) => { // native token don't need approval if ((0, isZeroAddress_1.isNativeTokenAddress)(token.address)) { return; } const approvedAmount = await (0, getAllowance_1.getAllowance)(walletClient, token.address, walletClient.account.address, spenderAddress); if (approvedAmount > 0) { const approveTx = await (0, exports.setAllowance)(walletClient, token.address, spenderAddress, 0n); return approveTx; } }; exports.revokeTokenApproval = revokeTokenApproval;