|
| 1 | +import { BigNumber, ethers } from "ethers"; |
| 2 | +import { LimitsResponse } from "../_types"; |
| 3 | +import * as sdk from "@across-protocol/sdk"; |
| 4 | +import { |
| 5 | + getTokenByAddress, |
| 6 | + getCachedLimits, |
| 7 | + HUB_POOL_CHAIN_ID, |
| 8 | +} from "../_utils"; |
| 9 | +import { CHAIN_IDs, TOKEN_SYMBOLS_MAP } from "../_constants"; |
| 10 | +import { |
| 11 | + BridgeStrategyData, |
| 12 | + BridgeStrategyDataParams, |
| 13 | +} from "../_bridges/types"; |
| 14 | + |
| 15 | +export function isFullyUtilized(limits: LimitsResponse): boolean { |
| 16 | + // Check if utilization is high (>80%) |
| 17 | + const { liquidReserves, utilizedReserves } = limits.reserves; |
| 18 | + const _liquidReserves = BigNumber.from(liquidReserves); |
| 19 | + const _utilizedReserves = BigNumber.from(utilizedReserves); |
| 20 | + |
| 21 | + const utilizationThreshold = sdk.utils.fixedPointAdjustment.mul(80).div(100); // 80% |
| 22 | + |
| 23 | + // Calculate current utilization percentage |
| 24 | + const currentUtilization = _utilizedReserves |
| 25 | + .mul(sdk.utils.fixedPointAdjustment) |
| 26 | + .div(_liquidReserves.add(_utilizedReserves)); |
| 27 | + |
| 28 | + return currentUtilization.gt(utilizationThreshold); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Fetches bridge limits and utilization data in parallel to determine strategy requirements |
| 33 | + */ |
| 34 | +export async function getBridgeStrategyData({ |
| 35 | + inputToken, |
| 36 | + outputToken, |
| 37 | + amount, |
| 38 | + recipient, |
| 39 | + depositor, |
| 40 | + logger, |
| 41 | +}: BridgeStrategyDataParams): Promise<BridgeStrategyData> { |
| 42 | + logger.debug({ |
| 43 | + at: "getBridgeStrategyData", |
| 44 | + message: "Starting bridge strategy data fetch", |
| 45 | + inputToken: inputToken.address, |
| 46 | + outputToken: outputToken.address, |
| 47 | + amount: amount.toString(), |
| 48 | + }); |
| 49 | + |
| 50 | + try { |
| 51 | + // Get token details for symbol and decimals first |
| 52 | + const inputTokenDetails = getTokenByAddress( |
| 53 | + inputToken.address, |
| 54 | + inputToken.chainId |
| 55 | + ); |
| 56 | + if (!inputTokenDetails) { |
| 57 | + throw new Error( |
| 58 | + `Input token not found for address ${inputToken.address}` |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + // Get L1 token address using TOKEN_SYMBOLS_MAP logic |
| 63 | + const l1TokenAddress = |
| 64 | + TOKEN_SYMBOLS_MAP[ |
| 65 | + inputTokenDetails.symbol as keyof typeof TOKEN_SYMBOLS_MAP |
| 66 | + ]?.addresses[HUB_POOL_CHAIN_ID]; |
| 67 | + if (!l1TokenAddress) { |
| 68 | + throw new Error( |
| 69 | + `L1 token not found for symbol ${inputTokenDetails.symbol}` |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + const l1Token = getTokenByAddress(l1TokenAddress, HUB_POOL_CHAIN_ID); |
| 74 | + if (!l1Token) { |
| 75 | + throw new Error( |
| 76 | + `L1 token details not found for address ${l1TokenAddress}` |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + const inputUnit = BigNumber.from(10).pow(inputTokenDetails.decimals); |
| 81 | + const limits = |
| 82 | + // Get bridge limits |
| 83 | + await getCachedLimits( |
| 84 | + inputToken.address, |
| 85 | + outputToken.address, |
| 86 | + inputToken.chainId, |
| 87 | + outputToken.chainId, |
| 88 | + inputUnit.toString(), |
| 89 | + recipient || depositor |
| 90 | + ); |
| 91 | + |
| 92 | + // Check if we can fill instantly |
| 93 | + const maxDepositInstant = BigNumber.from(limits.maxDepositInstant); |
| 94 | + const canFillInstantly = amount.lte(maxDepositInstant); |
| 95 | + |
| 96 | + const isUtilizationHigh = isFullyUtilized(limits); |
| 97 | + |
| 98 | + // Get output token details |
| 99 | + const outputTokenDetails = getTokenByAddress( |
| 100 | + outputToken.address, |
| 101 | + outputToken.chainId |
| 102 | + ); |
| 103 | + |
| 104 | + // Check if input and output tokens are both USDC |
| 105 | + const isUsdcToUsdc = |
| 106 | + inputTokenDetails?.symbol === "USDC" && |
| 107 | + outputTokenDetails?.symbol === "USDC"; |
| 108 | + |
| 109 | + // Check if deposit is > 1M USD |
| 110 | + const depositAmountUsd = parseFloat( |
| 111 | + ethers.utils.formatUnits(amount, inputTokenDetails?.decimals || 18) |
| 112 | + ); |
| 113 | + const isInThreshold = depositAmountUsd <= 10_000; // 10K USD |
| 114 | + const isLargeDeposit = depositAmountUsd > 1_000_000; // 1M USD |
| 115 | + |
| 116 | + // Check if eligible for Fast CCTP (Polygon, BSC, Solana) and deposit > 10K USD |
| 117 | + const fastCctpChains = [CHAIN_IDs.POLYGON, CHAIN_IDs.BSC, CHAIN_IDs.SOLANA]; |
| 118 | + const isFastCctpChain = |
| 119 | + fastCctpChains.includes(inputToken.chainId) || |
| 120 | + fastCctpChains.includes(outputToken.chainId); |
| 121 | + const isFastCctpEligible = isFastCctpChain && depositAmountUsd > 10_000; // 10K USD |
| 122 | + |
| 123 | + // Check if Linea is the source chain |
| 124 | + const isLineaSource = inputToken.chainId === CHAIN_IDs.LINEA; |
| 125 | + |
| 126 | + logger.debug({ |
| 127 | + at: "getBridgeStrategyData", |
| 128 | + message: "Successfully completed bridge strategy data fetch", |
| 129 | + results: { |
| 130 | + canFillInstantly, |
| 131 | + isUtilizationHigh, |
| 132 | + isUsdcToUsdc, |
| 133 | + isLargeDeposit, |
| 134 | + isFastCctpEligible, |
| 135 | + isLineaSource, |
| 136 | + }, |
| 137 | + }); |
| 138 | + |
| 139 | + return { |
| 140 | + canFillInstantly, |
| 141 | + isUtilizationHigh, |
| 142 | + isUsdcToUsdc, |
| 143 | + isLargeDeposit, |
| 144 | + isInThreshold, |
| 145 | + isFastCctpEligible, |
| 146 | + isLineaSource, |
| 147 | + }; |
| 148 | + } catch (error) { |
| 149 | + logger.warn({ |
| 150 | + at: "getBridgeStrategyData", |
| 151 | + message: "Failed to fetch bridge strategy data, using defaults", |
| 152 | + error: error instanceof Error ? error.message : String(error), |
| 153 | + }); |
| 154 | + |
| 155 | + // Safely return undefined if we can't fetch bridge strategy data |
| 156 | + return undefined; |
| 157 | + } |
| 158 | +} |
0 commit comments