UNPKG

@molecularlabs/nucleus-frontend

Version:
123 lines 4.43 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var approvals_exports = {}; __export(approvals_exports, { isDepositSpendApproved: () => isDepositSpendApproved, isWithdrawalSpendApproved: () => isWithdrawalSpendApproved }); module.exports = __toCommonJS(approvals_exports); var import_viem = require("viem"); var import_erc20 = require("../api/erc20"); var import_vault_config = require("../api/vault-config"); var import_constants = require("../constants"); var import_try_catch = require("../utils/try-catch"); var import_vaults = require("../vaults"); const isDepositSpendApproved = async ({ vaultKey, sourceChainId, depositTokenSymbol, userAddress }) => { const { data: vault, error: vaultError, success: vaultSuccess } = await (0, import_try_catch.tryCatch)((0, import_vaults.getVaultByKey)(vaultKey)); if (!vaultSuccess) { throw new Error(`Vault not found for ${vaultKey}: ${vaultError.message}`); } if (!vault.contracts) { throw new Error(`Contracts not found: ${vaultKey}`); } const normalizedSourceChainId = (0, import_vault_config.toChainId)(sourceChainId); const depositAssetAddress = vault.deposit.sourceChains[normalizedSourceChainId].depositTokens[depositTokenSymbol].address; if (!depositAssetAddress) { throw new Error(`Deposit asset address not found: ${depositTokenSymbol}`); } const { deposit } = vault; const { sourceChains } = deposit; const sourceChain = sourceChains[normalizedSourceChainId]; if (!sourceChain) { throw new Error(`Source chain not found: ${normalizedSourceChainId}`); } const [allowance, decimals] = await (0, import_erc20.getErc20AllowanceWithDecimals)({ chainId: normalizedSourceChainId, tokenAddress: depositAssetAddress, userAddress, spenderAddress: vault.contracts.boringVault }); if (allowance.status === "failure" || decimals.status === "failure") { return { isApproved: false, allowance: "0", allowanceAsBigInt: "0", decimals: "0", error: allowance.error || decimals.error }; } return { isApproved: allowance.result > 0n, allowance: (0, import_viem.formatUnits)(allowance.result, decimals.result), allowanceAsBigInt: allowance.result.toString(), decimals: decimals.result, error: null }; }; const isWithdrawalSpendApproved = async ({ vaultKey, destinationChainId, userAddress }) => { const { data: vault, error: vaultError, success: vaultSuccess } = await (0, import_try_catch.tryCatch)((0, import_vaults.getVaultByKey)(vaultKey)); if (!vaultSuccess) { throw new Error(`Vault not found for ${vaultKey}: ${vaultError.message}`); } if (!vault.contracts) { throw new Error(`Contracts not found for ${vaultKey}`); } const normalizedDestinationChainId = (0, import_vault_config.toChainId)(destinationChainId); const boringVaultAddress = vault.contracts.boringVault; const [allowance, decimals] = await (0, import_erc20.getErc20AllowanceWithDecimals)({ chainId: normalizedDestinationChainId, tokenAddress: boringVaultAddress, userAddress, spenderAddress: import_constants.ATOMIC_QUEUE_CONTRACT_ADDRESS }); if (allowance.status === "failure" || decimals.status === "failure") { return { isApproved: false, allowance: "0", allowanceAsBigInt: "0", decimals: "0", error: allowance.error || decimals.error }; } return { isApproved: allowance.result > 0n, allowance: (0, import_viem.formatUnits)(allowance.result, decimals.result), allowanceAsBigInt: allowance.result.toString(), decimals: decimals.result, error: null }; }; //# sourceMappingURL=approvals.js.map