UNPKG

@openocean.finance/widget-sdk

Version:

OpenOcean Any-to-Any Cross-Chain-Swap SDK

78 lines 3.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTokenAllowanceMulticall = exports.getTokenAllowance = exports.getAllowanceMulticall = exports.getAllowance = void 0; const actions_1 = require("viem/actions"); const isZeroAddress_js_1 = require("../../utils/isZeroAddress.js"); const abi_js_1 = require("./abi.js"); const publicClient_js_1 = require("./publicClient.js"); const utils_js_1 = require("./utils.js"); const getAllowance = async (chainId, tokenAddress, ownerAddress, spenderAddress) => { const client = await (0, publicClient_js_1.getPublicClient)(chainId); try { const approved = (await (0, actions_1.readContract)(client, { address: tokenAddress, abi: abi_js_1.allowanceAbi, functionName: 'allowance', args: [ownerAddress, spenderAddress], })); return approved; } catch (_e) { return 0n; } }; exports.getAllowance = getAllowance; const getAllowanceMulticall = async (chainId, tokens, ownerAddress) => { if (!tokens.length) { return []; } const multicallAddress = await (0, utils_js_1.getMulticallAddress)(chainId); if (!multicallAddress) { throw new Error(`No multicall address configured for chainId ${chainId}.`); } const client = await (0, publicClient_js_1.getPublicClient)(chainId); const contracts = tokens.map((token) => ({ address: token.token.address, abi: abi_js_1.allowanceAbi, functionName: 'allowance', args: [ownerAddress, token.spenderAddress], })); const results = await (0, actions_1.multicall)(client, { contracts, multicallAddress: multicallAddress, }); if (!results.length) { throw new Error(`Couldn't load allowance from chainId ${chainId} using multicall.`); } return tokens.map(({ token, spenderAddress }, i) => ({ token, spenderAddress, allowance: results[i].result, })); }; exports.getAllowanceMulticall = getAllowanceMulticall; const getTokenAllowance = async (token, ownerAddress, spenderAddress) => { if ((0, isZeroAddress_js_1.isNativeTokenAddress)(token.address)) { return; } const approved = await (0, exports.getAllowance)(token.chainId, token.address, ownerAddress, spenderAddress); return approved; }; exports.getTokenAllowance = getTokenAllowance; const getTokenAllowanceMulticall = async (ownerAddress, tokens) => { const filteredTokens = tokens.filter(({ token }) => !(0, isZeroAddress_js_1.isNativeTokenAddress)(token.address)); const tokenDataByChain = {}; for (const data of filteredTokens) { if (!tokenDataByChain[data.token.chainId]) { tokenDataByChain[data.token.chainId] = []; } tokenDataByChain[data.token.chainId].push(data); } const chainKeys = Object.keys(tokenDataByChain).map(Number.parseInt); const allowances = (await Promise.all(chainKeys.map(async (chainId) => { return (0, exports.getAllowanceMulticall)(chainId, tokenDataByChain[chainId], ownerAddress); }))).flat(); return allowances; }; exports.getTokenAllowanceMulticall = getTokenAllowanceMulticall; //# sourceMappingURL=getAllowance.js.map