UNPKG

@paxoslabs/earn-sdk

Version:
316 lines (311 loc) 10.2 kB
import { getErc20AllowanceWithDecimals, getPreviewFee, TellerAbi, getRateInQuoteWithAssetDecimals } from './chunk-SBKCBPE3.mjs'; import { getVaultByKey } from './chunk-DHTKSLVZ.mjs'; import { toChainId } from './chunk-4URQP4CS.mjs'; import { tryCatch, bigIntToNumberAsString } from './chunk-MHZTLW64.mjs'; import { ATOMIC_QUEUE_CONTRACT_ADDRESS, NATIVE_TOKEN_FOR_BRIDGE_FEE } from './chunk-NTRZGVUA.mjs'; import { formatUnits } from 'viem'; var isDepositSpendApproved = async ({ vaultKey, sourceChainId, depositTokenSymbol, userAddress }) => { const { data: vault, error: vaultError, success: vaultSuccess } = await tryCatch(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 = 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 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: formatUnits(allowance.result, decimals.result), allowanceAsBigInt: allowance.result.toString(), decimals: decimals.result, error: null }; }; var isWithdrawalSpendApproved = async ({ vaultKey, destinationChainId, userAddress }) => { const { data: vault, error: vaultError, success: vaultSuccess } = await tryCatch(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 = toChainId(destinationChainId); const boringVaultAddress = vault.contracts.boringVault; const [allowance, decimals] = await getErc20AllowanceWithDecimals({ chainId: normalizedDestinationChainId, tokenAddress: boringVaultAddress, userAddress, spenderAddress: 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: formatUnits(allowance.result, decimals.result), allowanceAsBigInt: allowance.result.toString(), decimals: decimals.result, error: null }; }; // src/vaults/bridge.ts var prepareBridgeContractArg = ({ bridgeChainIdentifier, userAddress, nativeTokenForBridgeFee = NATIVE_TOKEN_FOR_BRIDGE_FEE }) => { return { chainSelector: bridgeChainIdentifier, destinationChainReceiver: userAddress, bridgeFeeToken: nativeTokenForBridgeFee, messageGas: BigInt(1e5), data: "0x" }; }; var prepareBridgeTransactionData = async ({ vaultKey, bridgeAmount, sourceChainId, destinationChainId, userAddress }) => { const nativeTokenForBridgeFee = NATIVE_TOKEN_FOR_BRIDGE_FEE; const vault = await getVaultByKey(vaultKey); if (!vault) { throw new Error(`Invalid vault key: ${vaultKey}`); } if (!vault.contracts) { throw new Error(`Contracts not configured for vault ${vaultKey}`); } if (!vault.contracts.boringVault) { throw new Error( `BoringVault contract not configured for vault ${vaultKey}` ); } if (!vault.contracts.accountant) { throw new Error(`Accountant contract not configured for vault ${vaultKey}`); } if (!vault.contracts.teller) { throw new Error(`Teller contract not configured for vault ${vaultKey}`); } const normalizedSourceChainId = toChainId(sourceChainId); const sourceChain = vault.withdraw.sourceChains[normalizedSourceChainId]; if (!sourceChain) { throw new Error(`Source chain not configured for vault ${vaultKey}`); } const normalizedDestinationChainId = toChainId(destinationChainId); const destinationChain = sourceChain.destinationChains[normalizedDestinationChainId]; if (!destinationChain) { throw new Error(`Destination chain not configured for vault ${vaultKey}`); } const bridgeChainIdentifier = destinationChain.bridge.chainIdentifier; const bridgeContractArg = prepareBridgeContractArg({ bridgeChainIdentifier, userAddress, nativeTokenForBridgeFee }); const previewFee = await getPreviewFee({ shareAmount: bridgeAmount, bridgeData: bridgeContractArg, contractAddress: vault.contracts.teller, chainId: normalizedSourceChainId }); return { abi: TellerAbi, address: vault.contracts.teller, functionName: "bridge", args: [bridgeAmount, bridgeContractArg], chainId: normalizedSourceChainId, value: previewFee }; }; // src/display/bridge-fees.ts var getBridgeFee = async ({ vaultKey, bridgeAmount, sourceChainId, destinationChainId, userAddress, nativeTokenForBridgeFee = NATIVE_TOKEN_FOR_BRIDGE_FEE }) => { const vault = await getVaultByKey(vaultKey); if (!vault) { throw new Error(`Invalid vault key: ${vaultKey}`); } if (!vault.contracts) { throw new Error(`Contracts not configured for vault ${vaultKey}`); } if (!vault.contracts.boringVault) { throw new Error( `BoringVault contract not configured for vault ${vaultKey}` ); } if (!vault.contracts.accountant) { throw new Error(`Accountant contract not configured for vault ${vaultKey}`); } if (!vault.contracts.teller) { throw new Error(`Teller contract not configured for vault ${vaultKey}`); } const sourceChain = vault.withdraw.sourceChains[sourceChainId]; if (!sourceChain) { throw new Error(`Source chain not configured for vault ${vaultKey}`); } const destinationChain = sourceChain.destinationChains[destinationChainId]; if (!destinationChain) { throw new Error(`Destination chain not configured for vault ${vaultKey}`); } const bridgeChainIdentifier = destinationChain.bridge.chainIdentifier; const bridgeContractArg = prepareBridgeContractArg({ bridgeChainIdentifier, userAddress, nativeTokenForBridgeFee }); const previewFee = await getPreviewFee({ shareAmount: bridgeAmount, bridgeData: bridgeContractArg, contractAddress: vault.contracts.teller, chainId: sourceChainId }); return previewFee; }; // src/display/exchange-rate.ts var getDepositExchangeRate = async ({ vaultKey, sourceChainId, depositTokenSymbol, minimumFractionDigits, maximumFractionDigits = 3 }) => { const vault = await getVaultByKey(vaultKey); const sourceChain = vault.deposit.sourceChains[sourceChainId]; if (!sourceChain) { throw new Error( `Source chain ${sourceChainId} not found for vault ${vaultKey}` ); } const depositToken = sourceChain.depositTokens?.[depositTokenSymbol]; if (!depositToken) { throw new Error( `Deposit token ${depositTokenSymbol} not found for chain ${sourceChainId}` ); } const tokenAddress = depositToken.address; if (!tokenAddress) { throw new Error(`Token address not found for ${depositTokenSymbol}`); } const [decimals, rate] = await getRateInQuoteWithAssetDecimals({ assetAddress: tokenAddress, accountantAddress: vault.contracts.accountant, chainId: sourceChainId }); if (rate.status === "failure") { throw new Error(`Failed to get rate: ${rate.error.message}`); } if (decimals.status === "failure") { throw new Error(`Failed to get decimals: ${decimals.error.message}`); } return bigIntToNumberAsString(rate.result, { decimals: decimals.result, minimumFractionDigits, maximumFractionDigits }); }; var getWithdrawExchangeRate = async ({ vaultKey, sourceChainId, destinationChainId, wantTokenSymbol, minimumFractionDigits, maximumFractionDigits = 3 }) => { const vault = await getVaultByKey(vaultKey); const sourceChain = vault.withdraw.sourceChains[sourceChainId]; if (!sourceChain) { throw new Error( `Source chain ${sourceChainId} not found for vault ${vaultKey}` ); } const destinationChain = sourceChain.destinationChains[destinationChainId]; if (!destinationChain) { throw new Error( `Destination chain ${destinationChainId} not found for source chain ${sourceChainId}` ); } const wantToken = destinationChain.wantTokens?.[wantTokenSymbol]; if (!wantToken) { throw new Error( `Want token ${wantTokenSymbol} not found for chain ${destinationChainId}` ); } const tokenAddress = wantToken.address; if (!tokenAddress) { throw new Error(`Token address not found for ${wantTokenSymbol}`); } const [decimals, rate] = await getRateInQuoteWithAssetDecimals({ assetAddress: tokenAddress, accountantAddress: vault.contracts.accountant, chainId: sourceChainId }); if (rate.status === "failure") { throw new Error(`Failed to get rate: ${rate.error.message}`); } if (decimals.status === "failure") { throw new Error(`Failed to get decimals: ${decimals.error.message}`); } return bigIntToNumberAsString(rate.result, { decimals: decimals.result, minimumFractionDigits, maximumFractionDigits }); }; export { getBridgeFee, getDepositExchangeRate, getWithdrawExchangeRate, isDepositSpendApproved, isWithdrawalSpendApproved, prepareBridgeTransactionData }; //# sourceMappingURL=chunk-EYR4YMSX.mjs.map //# sourceMappingURL=chunk-EYR4YMSX.mjs.map