@mento-protocol/mento-sdk 3.2.7 → 3.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cache/routes.d.ts +13 -0
- package/dist/cache/routes.js +14649 -0
- package/dist/cache/tokens.d.ts +68 -0
- package/dist/cache/tokens.js +488 -0
- package/dist/core/abis/activePool.d.ts +2 -0
- package/dist/core/abis/activePool.js +14 -0
- package/dist/core/abis/addressesRegistry.d.ts +2 -0
- package/dist/core/abis/addressesRegistry.js +26 -0
- package/dist/core/abis/bipoolmanager.d.ts +34 -0
- package/dist/core/abis/bipoolmanager.js +72 -0
- package/dist/core/abis/borrowerOperations.d.ts +9 -0
- package/dist/core/abis/borrowerOperations.js +89 -0
- package/dist/core/abis/breakerbox.d.ts +13 -0
- package/dist/core/abis/breakerbox.js +8 -0
- package/dist/core/abis/broker.d.ts +2 -0
- package/dist/core/abis/broker.js +9 -0
- package/dist/core/abis/erc20.d.ts +9 -0
- package/dist/core/abis/erc20.js +21 -0
- package/dist/core/abis/fpmm.d.ts +270 -0
- package/dist/core/abis/fpmm.js +49 -0
- package/dist/core/abis/fpmmFactory.d.ts +85 -0
- package/dist/core/abis/fpmmFactory.js +26 -0
- package/dist/core/abis/hintHelpers.d.ts +2 -0
- package/dist/core/abis/hintHelpers.js +14 -0
- package/dist/core/abis/index.d.ts +22 -0
- package/dist/core/abis/index.js +38 -0
- package/dist/core/abis/liquidityStrategy.d.ts +132 -0
- package/dist/core/abis/liquidityStrategy.js +10 -0
- package/dist/core/abis/multiTroveGetter.d.ts +8 -0
- package/dist/core/abis/multiTroveGetter.js +15 -0
- package/dist/core/abis/priceFeed.d.ts +7 -0
- package/dist/core/abis/priceFeed.js +16 -0
- package/dist/core/abis/pricingmodule.d.ts +2 -0
- package/dist/core/abis/pricingmodule.js +6 -0
- package/dist/core/abis/reserve.d.ts +3 -0
- package/dist/core/abis/reserve.js +18 -0
- package/dist/core/abis/router.d.ts +521 -0
- package/dist/core/abis/router.js +45 -0
- package/dist/core/abis/sortedTroves.d.ts +2 -0
- package/dist/core/abis/sortedTroves.js +15 -0
- package/dist/core/abis/systemParams.d.ts +2 -0
- package/dist/core/abis/systemParams.js +14 -0
- package/dist/core/abis/troveManager.d.ts +2 -0
- package/dist/core/abis/troveManager.js +27 -0
- package/dist/core/abis/troveNFT.d.ts +2 -0
- package/dist/core/abis/troveNFT.js +9 -0
- package/dist/core/abis/virtualPool.d.ts +50 -0
- package/dist/core/abis/virtualPool.js +11 -0
- package/dist/core/abis/virtualPoolFactory.d.ts +59 -0
- package/dist/core/abis/virtualPoolFactory.js +17 -0
- package/dist/core/constants/addresses.d.ts +18 -0
- package/dist/core/constants/addresses.js +113 -0
- package/dist/core/constants/borrowConstants.d.ts +10 -0
- package/dist/core/constants/borrowConstants.js +16 -0
- package/dist/core/constants/borrowRegistries.d.ts +7 -0
- package/dist/core/constants/borrowRegistries.js +34 -0
- package/dist/core/constants/chainId.d.ts +7 -0
- package/dist/core/constants/chainId.js +11 -0
- package/dist/core/constants/contractNames.d.ts +21 -0
- package/dist/core/constants/contractNames.js +24 -0
- package/dist/core/constants/index.d.ts +6 -0
- package/dist/core/constants/index.js +22 -0
- package/dist/core/errors/base.d.ts +8 -0
- package/dist/core/errors/base.js +17 -0
- package/dist/core/errors/index.d.ts +4 -0
- package/dist/core/errors/index.js +20 -0
- package/dist/core/errors/oracle.d.ts +9 -0
- package/dist/core/errors/oracle.js +15 -0
- package/dist/core/errors/router.d.ts +14 -0
- package/dist/core/errors/router.js +24 -0
- package/dist/core/types/borrow.d.ts +87 -0
- package/dist/core/types/borrow.js +3 -0
- package/dist/core/types/contractAddresses.d.ts +42 -0
- package/dist/core/types/contractAddresses.js +3 -0
- package/dist/core/types/index.d.ts +10 -0
- package/dist/core/types/index.js +26 -0
- package/dist/core/types/liquidity.d.ts +194 -0
- package/dist/core/types/liquidity.js +3 -0
- package/dist/core/types/pool.d.ts +208 -0
- package/dist/core/types/pool.js +14 -0
- package/dist/core/types/provider.d.ts +45 -0
- package/dist/core/types/provider.js +3 -0
- package/dist/core/types/route.d.ts +62 -0
- package/dist/core/types/route.js +3 -0
- package/dist/core/types/token.d.ts +21 -0
- package/dist/core/types/token.js +3 -0
- package/dist/core/types/tradingLimits.d.ts +91 -0
- package/dist/core/types/tradingLimits.js +3 -0
- package/dist/core/types/tradingMode.d.ts +24 -0
- package/dist/core/types/tradingMode.js +31 -0
- package/dist/core/types/transaction.d.ts +45 -0
- package/dist/core/types/transaction.js +3 -0
- package/dist/esm/cache/routes.js +14644 -0
- package/dist/esm/cache/tokens.js +480 -0
- package/dist/esm/core/abis/activePool.js +10 -0
- package/dist/esm/core/abis/addressesRegistry.js +22 -0
- package/dist/esm/core/abis/bipoolmanager.js +68 -0
- package/dist/esm/core/abis/borrowerOperations.js +85 -0
- package/dist/esm/core/abis/breakerbox.js +4 -0
- package/dist/esm/core/abis/broker.js +5 -0
- package/dist/esm/core/abis/erc20.js +17 -0
- package/dist/esm/core/abis/fpmm.js +45 -0
- package/dist/esm/core/abis/fpmmFactory.js +22 -0
- package/dist/esm/core/abis/hintHelpers.js +10 -0
- package/dist/esm/core/abis/index.js +21 -0
- package/dist/esm/core/abis/liquidityStrategy.js +6 -0
- package/dist/esm/core/abis/multiTroveGetter.js +11 -0
- package/dist/esm/core/abis/priceFeed.js +12 -0
- package/dist/esm/core/abis/pricingmodule.js +2 -0
- package/dist/esm/core/abis/reserve.js +14 -0
- package/dist/esm/core/abis/router.js +41 -0
- package/dist/esm/core/abis/sortedTroves.js +11 -0
- package/dist/esm/core/abis/systemParams.js +10 -0
- package/dist/esm/core/abis/troveManager.js +23 -0
- package/dist/esm/core/abis/troveNFT.js +5 -0
- package/dist/esm/core/abis/virtualPool.js +7 -0
- package/dist/esm/core/abis/virtualPoolFactory.js +13 -0
- package/dist/esm/core/constants/addresses.js +107 -0
- package/dist/esm/core/constants/borrowConstants.js +12 -0
- package/dist/esm/core/constants/borrowRegistries.js +29 -0
- package/dist/esm/core/constants/chainId.js +7 -0
- package/dist/esm/core/constants/contractNames.js +20 -0
- package/dist/esm/core/constants/index.js +5 -0
- package/dist/esm/core/errors/base.js +12 -0
- package/dist/esm/core/errors/index.js +3 -0
- package/dist/esm/core/errors/oracle.js +10 -0
- package/dist/esm/core/errors/router.js +18 -0
- package/dist/esm/core/types/borrow.js +1 -0
- package/dist/esm/core/types/contractAddresses.js +1 -0
- package/dist/esm/core/types/index.js +9 -0
- package/dist/esm/core/types/liquidity.js +1 -0
- package/dist/esm/core/types/pool.js +10 -0
- package/dist/esm/core/types/provider.js +1 -0
- package/dist/esm/core/types/route.js +1 -0
- package/dist/esm/core/types/token.js +1 -0
- package/dist/esm/core/types/tradingLimits.js +1 -0
- package/dist/esm/core/types/tradingMode.js +26 -0
- package/dist/esm/core/types/transaction.js +1 -0
- package/dist/esm/index.js +139 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/services/borrow/BorrowService.js +455 -0
- package/dist/esm/services/borrow/borrowHelpers.js +3 -0
- package/dist/esm/services/borrow/borrowMath.js +127 -0
- package/dist/esm/services/borrow/index.js +3 -0
- package/dist/esm/services/borrow/internal/borrowApprovalService.js +48 -0
- package/dist/esm/services/borrow/internal/borrowContextStore.js +35 -0
- package/dist/esm/services/borrow/internal/borrowErc20.js +38 -0
- package/dist/esm/services/borrow/internal/borrowHints.js +27 -0
- package/dist/esm/services/borrow/internal/borrowPositionParser.js +82 -0
- package/dist/esm/services/borrow/internal/borrowReadService.js +271 -0
- package/dist/esm/services/borrow/internal/borrowRegistryReader.js +108 -0
- package/dist/esm/services/borrow/internal/borrowTransactionService.js +271 -0
- package/dist/esm/services/borrow/internal/borrowTypes.js +1 -0
- package/dist/esm/services/borrow/internal/borrowValidation.js +89 -0
- package/dist/esm/services/index.js +8 -0
- package/dist/esm/services/liquidity/LiquidityService.js +163 -0
- package/dist/esm/services/liquidity/basicLiquidity.js +162 -0
- package/dist/esm/services/liquidity/index.js +1 -0
- package/dist/esm/services/liquidity/liquidityHelpers.js +95 -0
- package/dist/esm/services/liquidity/rebalance.js +59 -0
- package/dist/esm/services/liquidity/zapHelpers.js +181 -0
- package/dist/esm/services/liquidity/zapIn.js +131 -0
- package/dist/esm/services/liquidity/zapOut.js +248 -0
- package/dist/esm/services/pools/PoolService.js +204 -0
- package/dist/esm/services/pools/index.js +1 -0
- package/dist/esm/services/pools/poolDetails.js +209 -0
- package/dist/esm/services/pools/poolDiscovery.js +112 -0
- package/dist/esm/services/pools/rebalancePreview.js +181 -0
- package/dist/esm/services/quotes/QuoteService.js +85 -0
- package/dist/esm/services/quotes/index.js +1 -0
- package/dist/esm/services/routes/RouteService.js +268 -0
- package/dist/esm/services/routes/index.js +1 -0
- package/dist/esm/services/swap/SwapService.js +247 -0
- package/dist/esm/services/swap/index.js +1 -0
- package/dist/esm/services/tokens/index.js +1 -0
- package/dist/esm/services/tokens/tokenService.js +285 -0
- package/dist/esm/services/trading/TradingLimitsService.js +154 -0
- package/dist/esm/services/trading/TradingService.js +222 -0
- package/dist/esm/services/trading/index.js +2 -0
- package/dist/esm/utils/chainConfig.js +118 -0
- package/dist/esm/utils/costUtils.js +56 -0
- package/dist/esm/utils/deadline.js +22 -0
- package/dist/esm/utils/index.js +9 -0
- package/dist/esm/utils/multicall.js +47 -0
- package/dist/esm/utils/pathEncoder.js +69 -0
- package/dist/esm/utils/rateFeed.js +23 -0
- package/dist/esm/utils/retry.js +24 -0
- package/dist/esm/utils/routeUtils.js +361 -0
- package/dist/esm/utils/routes.js +2 -0
- package/dist/esm/utils/sortUtils.js +33 -0
- package/dist/esm/utils/tokens.js +2 -0
- package/dist/esm/utils/tradingLimits.js +163 -0
- package/dist/esm/utils/validation.js +30 -0
- package/dist/index.d.ts +101 -0
- package/dist/index.js +158 -0
- package/dist/services/borrow/BorrowService.d.ts +381 -0
- package/dist/services/borrow/BorrowService.js +460 -0
- package/dist/services/borrow/borrowHelpers.d.ts +4 -0
- package/dist/services/borrow/borrowHelpers.js +13 -0
- package/dist/services/borrow/borrowMath.d.ts +21 -0
- package/dist/services/borrow/borrowMath.js +137 -0
- package/dist/services/borrow/index.d.ts +4 -0
- package/dist/services/borrow/index.js +20 -0
- package/dist/services/borrow/internal/borrowApprovalService.d.ts +14 -0
- package/dist/services/borrow/internal/borrowApprovalService.js +53 -0
- package/dist/services/borrow/internal/borrowContextStore.d.ts +11 -0
- package/dist/services/borrow/internal/borrowContextStore.js +40 -0
- package/dist/services/borrow/internal/borrowErc20.d.ts +5 -0
- package/dist/services/borrow/internal/borrowErc20.js +43 -0
- package/dist/services/borrow/internal/borrowHints.d.ts +7 -0
- package/dist/services/borrow/internal/borrowHints.js +31 -0
- package/dist/services/borrow/internal/borrowPositionParser.d.ts +4 -0
- package/dist/services/borrow/internal/borrowPositionParser.js +87 -0
- package/dist/services/borrow/internal/borrowReadService.d.ts +31 -0
- package/dist/services/borrow/internal/borrowReadService.js +276 -0
- package/dist/services/borrow/internal/borrowRegistryReader.d.ts +5 -0
- package/dist/services/borrow/internal/borrowRegistryReader.js +113 -0
- package/dist/services/borrow/internal/borrowTransactionService.d.ts +23 -0
- package/dist/services/borrow/internal/borrowTransactionService.js +276 -0
- package/dist/services/borrow/internal/borrowTypes.d.ts +15 -0
- package/dist/services/borrow/internal/borrowTypes.js +3 -0
- package/dist/services/borrow/internal/borrowValidation.d.ts +14 -0
- package/dist/services/borrow/internal/borrowValidation.js +104 -0
- package/dist/services/index.d.ts +9 -0
- package/dist/services/index.js +25 -0
- package/dist/services/liquidity/LiquidityService.d.ts +139 -0
- package/dist/services/liquidity/LiquidityService.js +168 -0
- package/dist/services/liquidity/basicLiquidity.d.ts +11 -0
- package/dist/services/liquidity/basicLiquidity.js +172 -0
- package/dist/services/liquidity/index.d.ts +2 -0
- package/dist/services/liquidity/index.js +18 -0
- package/dist/services/liquidity/liquidityHelpers.d.ts +19 -0
- package/dist/services/liquidity/liquidityHelpers.js +104 -0
- package/dist/services/liquidity/rebalance.d.ts +6 -0
- package/dist/services/liquidity/rebalance.js +64 -0
- package/dist/services/liquidity/zapHelpers.d.ts +100 -0
- package/dist/services/liquidity/zapHelpers.js +192 -0
- package/dist/services/liquidity/zapIn.d.ts +18 -0
- package/dist/services/liquidity/zapIn.js +138 -0
- package/dist/services/liquidity/zapOut.d.ts +9 -0
- package/dist/services/liquidity/zapOut.js +255 -0
- package/dist/services/pools/PoolService.d.ts +69 -0
- package/dist/services/pools/PoolService.js +209 -0
- package/dist/services/pools/index.d.ts +2 -0
- package/dist/services/pools/index.js +18 -0
- package/dist/services/pools/poolDetails.d.ts +13 -0
- package/dist/services/pools/poolDetails.js +216 -0
- package/dist/services/pools/poolDiscovery.d.ts +12 -0
- package/dist/services/pools/poolDiscovery.js +117 -0
- package/dist/services/pools/rebalancePreview.d.ts +5 -0
- package/dist/services/pools/rebalancePreview.js +186 -0
- package/dist/services/quotes/QuoteService.d.ts +51 -0
- package/dist/services/quotes/QuoteService.js +91 -0
- package/dist/services/quotes/index.d.ts +2 -0
- package/dist/services/quotes/index.js +18 -0
- package/dist/services/routes/RouteService.d.ts +117 -0
- package/dist/services/routes/RouteService.js +306 -0
- package/dist/services/routes/index.d.ts +2 -0
- package/dist/services/routes/index.js +18 -0
- package/dist/services/swap/SwapService.d.ts +198 -0
- package/dist/services/swap/SwapService.js +252 -0
- package/dist/services/swap/index.d.ts +2 -0
- package/dist/services/swap/index.js +18 -0
- package/dist/services/tokens/index.d.ts +2 -0
- package/dist/services/tokens/index.js +18 -0
- package/dist/services/tokens/tokenService.d.ts +55 -0
- package/dist/services/tokens/tokenService.js +290 -0
- package/dist/services/trading/TradingLimitsService.d.ts +38 -0
- package/dist/services/trading/TradingLimitsService.js +159 -0
- package/dist/services/trading/TradingService.d.ts +115 -0
- package/dist/services/trading/TradingService.js +227 -0
- package/dist/services/trading/index.d.ts +3 -0
- package/dist/services/trading/index.js +19 -0
- package/dist/utils/chainConfig.d.ts +16 -0
- package/dist/utils/chainConfig.js +123 -0
- package/dist/utils/costUtils.d.ts +12 -0
- package/dist/utils/costUtils.js +60 -0
- package/dist/utils/deadline.d.ts +21 -0
- package/dist/utils/deadline.js +26 -0
- package/dist/utils/index.d.ts +10 -0
- package/dist/utils/index.js +26 -0
- package/dist/utils/multicall.d.ts +30 -0
- package/dist/utils/multicall.js +52 -0
- package/dist/utils/pathEncoder.d.ts +34 -0
- package/dist/utils/pathEncoder.js +73 -0
- package/dist/utils/rateFeed.d.ts +18 -0
- package/dist/utils/rateFeed.js +27 -0
- package/dist/utils/retry.d.ts +12 -0
- package/dist/utils/retry.js +28 -0
- package/dist/utils/routeUtils.d.ts +295 -0
- package/dist/utils/routeUtils.js +371 -0
- package/dist/utils/routes.d.ts +3 -0
- package/dist/utils/routes.js +8 -0
- package/dist/utils/sortUtils.d.ts +24 -0
- package/dist/utils/sortUtils.js +39 -0
- package/dist/utils/tokens.d.ts +2 -0
- package/dist/utils/tokens.js +13 -0
- package/dist/utils/tradingLimits.d.ts +41 -0
- package/dist/utils/tradingLimits.js +171 -0
- package/dist/utils/validation.d.ts +19 -0
- package/dist/utils/validation.js +34 -0
- package/package.json +1 -1
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { tryGetContractAddress } from '../../core/constants';
|
|
2
|
+
import { PoolType } from '../../core/types';
|
|
3
|
+
import { FPMM_FACTORY_ABI, FPMM_ABI, VIRTUAL_POOL_FACTORY_ABI, VIRTUAL_POOL_ABI, BIPOOL_MANAGER_ABI, } from '../../core/abis';
|
|
4
|
+
import { sortTokenAddresses } from '../../utils/sortUtils';
|
|
5
|
+
import { multicall } from '../../utils/multicall';
|
|
6
|
+
/**
|
|
7
|
+
* Fetches all FPMM pools from the FPMM Factory
|
|
8
|
+
*/
|
|
9
|
+
export async function fetchFPMMPools(publicClient, chainId) {
|
|
10
|
+
const fpmmFactoryAddress = tryGetContractAddress(chainId, 'FPMMFactory');
|
|
11
|
+
if (!fpmmFactoryAddress) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
// Get all deployed FPMM pool addresses
|
|
16
|
+
const poolAddresses = (await publicClient.readContract({
|
|
17
|
+
address: fpmmFactoryAddress,
|
|
18
|
+
abi: FPMM_FACTORY_ABI,
|
|
19
|
+
functionName: 'deployedFPMMAddresses',
|
|
20
|
+
}));
|
|
21
|
+
if (poolAddresses.length === 0) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
// Batch all token0/token1 reads into a single multicall
|
|
25
|
+
const contracts = poolAddresses.flatMap((poolAddress) => [
|
|
26
|
+
{ address: poolAddress, abi: FPMM_ABI, functionName: 'token0' },
|
|
27
|
+
{ address: poolAddress, abi: FPMM_ABI, functionName: 'token1' },
|
|
28
|
+
]);
|
|
29
|
+
const results = await multicall(publicClient, contracts);
|
|
30
|
+
return poolAddresses.map((poolAddress, i) => {
|
|
31
|
+
const token0Result = results[i * 2];
|
|
32
|
+
const token1Result = results[i * 2 + 1];
|
|
33
|
+
if (token0Result.status === 'failure' || token1Result.status === 'failure') {
|
|
34
|
+
throw new Error(`Failed to read token addresses for pool ${poolAddress}`);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
factoryAddr: fpmmFactoryAddress,
|
|
38
|
+
poolAddr: poolAddress,
|
|
39
|
+
token0: token0Result.result,
|
|
40
|
+
token1: token1Result.result,
|
|
41
|
+
poolType: PoolType.FPMM,
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new Error(`Failed to fetch FPMM pools: ${error.message}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Fetches all active Virtual pools from the VirtualPoolFactory,
|
|
51
|
+
* then resolves token pairs and exchange IDs from each pool and BiPoolManager.
|
|
52
|
+
*/
|
|
53
|
+
export async function fetchVirtualPools(publicClient, chainId) {
|
|
54
|
+
const virtualPoolFactoryAddress = tryGetContractAddress(chainId, 'VirtualPoolFactory');
|
|
55
|
+
const biPoolManagerAddress = tryGetContractAddress(chainId, 'BiPoolManager');
|
|
56
|
+
if (!virtualPoolFactoryAddress || !biPoolManagerAddress) {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
// Fetch active pool addresses and all exchanges in parallel
|
|
61
|
+
const [poolAddresses, exchangesData] = await Promise.all([
|
|
62
|
+
publicClient.readContract({
|
|
63
|
+
address: virtualPoolFactoryAddress,
|
|
64
|
+
abi: VIRTUAL_POOL_FACTORY_ABI,
|
|
65
|
+
functionName: 'getAllPools',
|
|
66
|
+
}),
|
|
67
|
+
publicClient.readContract({
|
|
68
|
+
address: biPoolManagerAddress,
|
|
69
|
+
abi: BIPOOL_MANAGER_ABI,
|
|
70
|
+
functionName: 'getExchanges',
|
|
71
|
+
}),
|
|
72
|
+
]);
|
|
73
|
+
if (poolAddresses.length === 0) {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
// Build a lookup from sorted token pair to exchangeId
|
|
77
|
+
const tokenPairToExchangeId = new Map();
|
|
78
|
+
for (const exchange of exchangesData) {
|
|
79
|
+
if (exchange.assets.length === 2) {
|
|
80
|
+
const [t0, t1] = sortTokenAddresses(exchange.assets[0], exchange.assets[1]);
|
|
81
|
+
tokenPairToExchangeId.set(`${t0}:${t1}`, exchange.exchangeId);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// Batch all tokens() reads into a single multicall
|
|
85
|
+
const contracts = poolAddresses.map((poolAddress) => ({
|
|
86
|
+
address: poolAddress,
|
|
87
|
+
abi: VIRTUAL_POOL_ABI,
|
|
88
|
+
functionName: 'tokens',
|
|
89
|
+
}));
|
|
90
|
+
const results = await multicall(publicClient, contracts);
|
|
91
|
+
return poolAddresses.map((poolAddress, i) => {
|
|
92
|
+
const result = results[i];
|
|
93
|
+
if (result.status === 'failure') {
|
|
94
|
+
throw new Error(`Failed to read token addresses for virtual pool ${poolAddress}`);
|
|
95
|
+
}
|
|
96
|
+
const [token0, token1] = result.result;
|
|
97
|
+
const [sorted0, sorted1] = sortTokenAddresses(token0, token1);
|
|
98
|
+
const exchangeId = tokenPairToExchangeId.get(`${sorted0}:${sorted1}`);
|
|
99
|
+
return {
|
|
100
|
+
factoryAddr: virtualPoolFactoryAddress,
|
|
101
|
+
poolAddr: poolAddress,
|
|
102
|
+
token0: sorted0,
|
|
103
|
+
token1: sorted1,
|
|
104
|
+
poolType: PoolType.Virtual,
|
|
105
|
+
exchangeId,
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
throw new Error(`Failed to fetch Virtual pools: ${error.message}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { getAddress } from 'viem';
|
|
2
|
+
import { LIQUIDITY_STRATEGY_ABI } from '../../core/abis';
|
|
3
|
+
import { multicall } from '../../utils/multicall';
|
|
4
|
+
// Liquidity strategy incentive rates are stored as 18-decimal percentages.
|
|
5
|
+
const FEE_DENOMINATOR = 10n ** 18n;
|
|
6
|
+
function toBigIntValue(value) {
|
|
7
|
+
return typeof value === 'bigint' ? value : BigInt(value);
|
|
8
|
+
}
|
|
9
|
+
function toNumberValue(value) {
|
|
10
|
+
return typeof value === 'number' ? value : Number(value);
|
|
11
|
+
}
|
|
12
|
+
function parseDirection(value) {
|
|
13
|
+
const normalized = toNumberValue(value);
|
|
14
|
+
if (normalized === 0)
|
|
15
|
+
return 'Expand';
|
|
16
|
+
if (normalized === 1)
|
|
17
|
+
return 'Contract';
|
|
18
|
+
throw new Error(`Unsupported liquidity strategy direction: ${normalized}`);
|
|
19
|
+
}
|
|
20
|
+
function parsePoolConfig(raw) {
|
|
21
|
+
return {
|
|
22
|
+
isToken0Debt: raw[0],
|
|
23
|
+
lastRebalance: toNumberValue(raw[1]),
|
|
24
|
+
rebalanceCooldown: toNumberValue(raw[2]),
|
|
25
|
+
protocolFeeRecipient: raw[3],
|
|
26
|
+
liquiditySourceIncentiveExpansion: toBigIntValue(raw[4]),
|
|
27
|
+
protocolIncentiveExpansion: toBigIntValue(raw[5]),
|
|
28
|
+
liquiditySourceIncentiveContraction: toBigIntValue(raw[6]),
|
|
29
|
+
protocolIncentiveContraction: toBigIntValue(raw[7]),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function parseContext(raw) {
|
|
33
|
+
return {
|
|
34
|
+
pool: raw[0],
|
|
35
|
+
reserves: {
|
|
36
|
+
reserveNum: toBigIntValue(raw[1][0]),
|
|
37
|
+
reserveDen: toBigIntValue(raw[1][1]),
|
|
38
|
+
},
|
|
39
|
+
prices: {
|
|
40
|
+
oracleNum: toBigIntValue(raw[2][0]),
|
|
41
|
+
oracleDen: toBigIntValue(raw[2][1]),
|
|
42
|
+
poolPriceAbove: raw[2][2],
|
|
43
|
+
rebalanceThreshold: toNumberValue(raw[2][3]),
|
|
44
|
+
},
|
|
45
|
+
token0: raw[3],
|
|
46
|
+
token1: raw[4],
|
|
47
|
+
token0Dec: toBigIntValue(raw[5]),
|
|
48
|
+
token1Dec: toBigIntValue(raw[6]),
|
|
49
|
+
isToken0Debt: raw[7],
|
|
50
|
+
incentives: {
|
|
51
|
+
liquiditySourceIncentiveExpansion: toBigIntValue(raw[8][0]),
|
|
52
|
+
protocolIncentiveExpansion: toBigIntValue(raw[8][1]),
|
|
53
|
+
liquiditySourceIncentiveContraction: toBigIntValue(raw[8][2]),
|
|
54
|
+
protocolIncentiveContraction: toBigIntValue(raw[8][3]),
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function parseAction(raw) {
|
|
59
|
+
return {
|
|
60
|
+
dir: parseDirection(raw[0]),
|
|
61
|
+
amount0Out: toBigIntValue(raw[1]),
|
|
62
|
+
amount1Out: toBigIntValue(raw[2]),
|
|
63
|
+
amountOwedToPool: toBigIntValue(raw[3]),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function isPreviewEligible(detail) {
|
|
67
|
+
return (detail.poolType === 'FPMM' &&
|
|
68
|
+
detail.pricing !== null &&
|
|
69
|
+
detail.rebalancing.inBand === false &&
|
|
70
|
+
!!detail.rebalancing.liquidityStrategy);
|
|
71
|
+
}
|
|
72
|
+
function buildPreview(detail, strategyAddress, config, context, action) {
|
|
73
|
+
const debtToken = context.isToken0Debt ? context.token0 : context.token1;
|
|
74
|
+
const collateralToken = context.isToken0Debt ? context.token1 : context.token0;
|
|
75
|
+
const inputToken = action.dir === 'Expand' ? debtToken : collateralToken;
|
|
76
|
+
const outputToken = action.dir === 'Expand' ? collateralToken : debtToken;
|
|
77
|
+
const amountTransferredValue = action.amount0Out > 0n ? action.amount0Out : action.amount1Out;
|
|
78
|
+
const protocolRate = action.dir === 'Expand'
|
|
79
|
+
? config.protocolIncentiveExpansion
|
|
80
|
+
: config.protocolIncentiveContraction;
|
|
81
|
+
const liquiditySourceRate = action.dir === 'Expand'
|
|
82
|
+
? config.liquiditySourceIncentiveExpansion
|
|
83
|
+
: config.liquiditySourceIncentiveContraction;
|
|
84
|
+
const protocolIncentiveAmount = (amountTransferredValue * protocolRate) / FEE_DENOMINATOR;
|
|
85
|
+
const liquiditySourceBase = amountTransferredValue > protocolIncentiveAmount
|
|
86
|
+
? amountTransferredValue - protocolIncentiveAmount
|
|
87
|
+
: 0n;
|
|
88
|
+
const liquiditySourceIncentiveAmount = (liquiditySourceBase * liquiditySourceRate) / FEE_DENOMINATOR;
|
|
89
|
+
return {
|
|
90
|
+
poolAddress: detail.poolAddr,
|
|
91
|
+
strategyAddress,
|
|
92
|
+
direction: action.dir,
|
|
93
|
+
config,
|
|
94
|
+
context,
|
|
95
|
+
action,
|
|
96
|
+
inputToken,
|
|
97
|
+
outputToken,
|
|
98
|
+
amountRequired: {
|
|
99
|
+
token: inputToken,
|
|
100
|
+
amount: action.amountOwedToPool,
|
|
101
|
+
},
|
|
102
|
+
amountTransferred: {
|
|
103
|
+
token: outputToken,
|
|
104
|
+
amount: amountTransferredValue,
|
|
105
|
+
},
|
|
106
|
+
protocolIncentive: {
|
|
107
|
+
token: outputToken,
|
|
108
|
+
amount: protocolIncentiveAmount,
|
|
109
|
+
},
|
|
110
|
+
liquiditySourceIncentive: {
|
|
111
|
+
token: outputToken,
|
|
112
|
+
amount: liquiditySourceIncentiveAmount,
|
|
113
|
+
},
|
|
114
|
+
approvalToken: inputToken,
|
|
115
|
+
approvalSpender: strategyAddress,
|
|
116
|
+
approvalAmount: action.amountOwedToPool,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export async function fetchPoolRebalancePreview(publicClient, detail) {
|
|
120
|
+
const [preview] = await fetchPoolRebalancePreviewBatch(publicClient, [detail]);
|
|
121
|
+
return preview;
|
|
122
|
+
}
|
|
123
|
+
export async function fetchPoolRebalancePreviewBatch(publicClient, details) {
|
|
124
|
+
const previews = details.map(() => null);
|
|
125
|
+
const eligibleTargets = details.flatMap((detail, index) => {
|
|
126
|
+
if (!isPreviewEligible(detail))
|
|
127
|
+
return [];
|
|
128
|
+
const strategyAddress = detail.rebalancing.liquidityStrategy;
|
|
129
|
+
if (!strategyAddress)
|
|
130
|
+
return [];
|
|
131
|
+
try {
|
|
132
|
+
return [
|
|
133
|
+
{
|
|
134
|
+
index,
|
|
135
|
+
detail,
|
|
136
|
+
strategyAddress: getAddress(strategyAddress),
|
|
137
|
+
},
|
|
138
|
+
];
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
if (eligibleTargets.length === 0) {
|
|
145
|
+
return previews;
|
|
146
|
+
}
|
|
147
|
+
const contracts = eligibleTargets.flatMap(({ detail, strategyAddress }) => [
|
|
148
|
+
{
|
|
149
|
+
address: strategyAddress,
|
|
150
|
+
abi: LIQUIDITY_STRATEGY_ABI,
|
|
151
|
+
functionName: 'poolConfigs',
|
|
152
|
+
args: [detail.poolAddr],
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
address: strategyAddress,
|
|
156
|
+
abi: LIQUIDITY_STRATEGY_ABI,
|
|
157
|
+
functionName: 'determineAction',
|
|
158
|
+
args: [detail.poolAddr],
|
|
159
|
+
},
|
|
160
|
+
]);
|
|
161
|
+
const results = await multicall(publicClient, contracts);
|
|
162
|
+
eligibleTargets.forEach((target, targetIndex) => {
|
|
163
|
+
const configResult = results[targetIndex * 2];
|
|
164
|
+
const determineActionResult = results[targetIndex * 2 + 1];
|
|
165
|
+
if (!configResult || !determineActionResult)
|
|
166
|
+
return;
|
|
167
|
+
if (configResult.status === 'failure' || determineActionResult.status === 'failure')
|
|
168
|
+
return;
|
|
169
|
+
try {
|
|
170
|
+
const config = parsePoolConfig(configResult.result);
|
|
171
|
+
const [rawContext, rawAction] = determineActionResult.result;
|
|
172
|
+
const context = parseContext(rawContext);
|
|
173
|
+
const action = parseAction(rawAction);
|
|
174
|
+
previews[target.index] = buildPreview(target.detail, target.strategyAddress, config, context, action);
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
previews[target.index] = null;
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
return previews;
|
|
181
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { BaseError, ContractFunctionRevertedError } from 'viem';
|
|
2
|
+
import { ROUTER_ABI } from '../../core/abis';
|
|
3
|
+
import { getContractAddress } from '../../core/constants';
|
|
4
|
+
import { FXMarketClosedError } from '../../core/errors';
|
|
5
|
+
import { encodeRoutePath } from '../../utils/pathEncoder';
|
|
6
|
+
import { validateAddress } from '../../utils/validation';
|
|
7
|
+
/**
|
|
8
|
+
* Service for getting swap quotes from the Mento protocol.
|
|
9
|
+
* Calculates expected output amounts for trades without executing them.
|
|
10
|
+
*/
|
|
11
|
+
export class QuoteService {
|
|
12
|
+
constructor(publicClient, chainId, routeService) {
|
|
13
|
+
this.publicClient = publicClient;
|
|
14
|
+
this.chainId = chainId;
|
|
15
|
+
this.routeService = routeService;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Calculates the expected output amount for a swap between two tokens.
|
|
19
|
+
*
|
|
20
|
+
* @param tokenIn - The address of the input token (e.g., '0x765DE816845861e75A25fCA122bb6898B8B1282a')
|
|
21
|
+
* @param tokenOut - The address of the output token (e.g., '0x471EcE3750Da237f93B8E339c536989b8978a438')
|
|
22
|
+
* @param amountIn - The amount of input tokens (in wei/smallest unit)
|
|
23
|
+
* @param route - Optional pre-fetched route. If not provided, the optimal route will be found automatically.
|
|
24
|
+
* @returns The expected output amount (in wei/smallest unit)
|
|
25
|
+
* @throws {RouteNotFoundError} If no route exists between the token pair
|
|
26
|
+
* @throws {Error} If the Router contract call fails
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* // Calculate output for 100 USDm
|
|
31
|
+
* const amountIn = BigInt(100) * BigInt(10 ** 18) // 100 USDm in wei
|
|
32
|
+
* const expectedOut = await quoteService.getAmountOut(
|
|
33
|
+
* '0x765DE816845861e75A25fCA122bb6898B8B1282a', // USDm
|
|
34
|
+
* '0x471EcE3750Da237f93B8E339c536989b8978a438', // CELO
|
|
35
|
+
* amountIn
|
|
36
|
+
* )
|
|
37
|
+
* console.log(`Expected CELO output: ${expectedOut}`)
|
|
38
|
+
*
|
|
39
|
+
* // Or provide a pre-fetched route for better performance
|
|
40
|
+
* const route = await routeService.findRoute(
|
|
41
|
+
* '0x765DE816845861e75A25fCA122bb6898B8B1282a',
|
|
42
|
+
* '0x471EcE3750Da237f93B8E339c536989b8978a438'
|
|
43
|
+
* )
|
|
44
|
+
* const expectedOut2 = await quoteService.getAmountOut(
|
|
45
|
+
* '0x765DE816845861e75A25fCA122bb6898B8B1282a',
|
|
46
|
+
* '0x471EcE3750Da237f93B8E339c536989b8978a438',
|
|
47
|
+
* amountIn,
|
|
48
|
+
* route
|
|
49
|
+
* )
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
async getAmountOut(tokenIn, tokenOut, amountIn, route) {
|
|
53
|
+
// Validate address inputs
|
|
54
|
+
validateAddress(tokenIn, 'tokenIn');
|
|
55
|
+
validateAddress(tokenOut, 'tokenOut');
|
|
56
|
+
// If the consumer does not provide a route then we find the best route.
|
|
57
|
+
if (!route) {
|
|
58
|
+
route = await this.routeService.findRoute(tokenIn, tokenOut);
|
|
59
|
+
}
|
|
60
|
+
return getAmountOutForRoute(this.publicClient, this.chainId, tokenIn, tokenOut, amountIn, route);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export async function getAmountOutForRoute(publicClient, chainId, tokenIn, tokenOut, amountIn, route) {
|
|
64
|
+
const routerRoutes = encodeRoutePath(route.path, tokenIn, tokenOut);
|
|
65
|
+
const routerAddress = getContractAddress(chainId, 'Router');
|
|
66
|
+
try {
|
|
67
|
+
const amounts = (await publicClient.readContract({
|
|
68
|
+
address: routerAddress,
|
|
69
|
+
abi: ROUTER_ABI,
|
|
70
|
+
functionName: 'getAmountsOut',
|
|
71
|
+
args: [amountIn, routerRoutes],
|
|
72
|
+
}));
|
|
73
|
+
return amounts[amounts.length - 1];
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
if (error instanceof BaseError) {
|
|
77
|
+
const revertError = error.walk((candidate) => candidate instanceof ContractFunctionRevertedError);
|
|
78
|
+
if (revertError instanceof ContractFunctionRevertedError &&
|
|
79
|
+
revertError.data?.errorName === 'FXMarketClosed') {
|
|
80
|
+
throw new FXMarketClosedError();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './QuoteService';
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { ERC20_ABI } from '../../core/abis';
|
|
2
|
+
import { RouteNotFoundError } from '../../core/errors';
|
|
3
|
+
import { buildConnectivityStructures, generateAllRoutes, selectOptimalRoutes } from '../../utils/routeUtils';
|
|
4
|
+
import { canonicalSymbolKey } from '../../utils/sortUtils';
|
|
5
|
+
import { multicall } from '../../utils/multicall';
|
|
6
|
+
/**
|
|
7
|
+
* Service for discovering and managing trading routes in the Mento protocol.
|
|
8
|
+
* Handles route discovery for both direct (single-hop) and multi-hop trading paths.
|
|
9
|
+
*
|
|
10
|
+
* Routes are identified by their token pair and include the path of pools
|
|
11
|
+
* needed to execute the trade. Multi-hop routes (up to 2 hops) are automatically
|
|
12
|
+
* discovered when no direct route exists between two tokens.
|
|
13
|
+
*/
|
|
14
|
+
export class RouteService {
|
|
15
|
+
constructor(publicClient, chainId, poolService) {
|
|
16
|
+
this.publicClient = publicClient;
|
|
17
|
+
this.chainId = chainId;
|
|
18
|
+
this.poolService = poolService;
|
|
19
|
+
this.symbolCache = new Map();
|
|
20
|
+
this.routeCache = new Map();
|
|
21
|
+
this.routeLookupCache = new Map();
|
|
22
|
+
this.routePromises = new Map();
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Generates all direct (single-hop) routes from available pools
|
|
26
|
+
* Routes are deduplicated and assets are sorted alphabetically by symbol
|
|
27
|
+
*
|
|
28
|
+
* @returns Array of direct routes with single-hop paths
|
|
29
|
+
* @throws {Error} If RPC calls fail
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const directRoutes = await routeService.getDirectRoutes()
|
|
34
|
+
* console.log(`Found ${directRoutes.length} direct routes`)
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
async getDirectRoutes() {
|
|
38
|
+
const pools = await this.poolService.getPools();
|
|
39
|
+
if (pools.length === 0) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
// Fetch all unique token addresses
|
|
43
|
+
const uniqueTokens = new Set();
|
|
44
|
+
pools.forEach((pool) => {
|
|
45
|
+
uniqueTokens.add(pool.token0);
|
|
46
|
+
uniqueTokens.add(pool.token1);
|
|
47
|
+
});
|
|
48
|
+
// Fetch symbols for all tokens in parallel. Used for the route ids
|
|
49
|
+
const tokenAddresses = Array.from(uniqueTokens);
|
|
50
|
+
await this.hydrateTokenSymbols(tokenAddresses);
|
|
51
|
+
const routes = [];
|
|
52
|
+
// Loop all pools
|
|
53
|
+
for (const pool of pools) {
|
|
54
|
+
const symbol0 = this.symbolCache.get(pool.token0);
|
|
55
|
+
const symbol1 = this.symbolCache.get(pool.token1);
|
|
56
|
+
if (!symbol0 || !symbol1) {
|
|
57
|
+
throw new Error(`Symbol not found for token ${pool.token0} or ${pool.token1}`);
|
|
58
|
+
}
|
|
59
|
+
// Create canonical route ID (alphabetically sorted symbols)
|
|
60
|
+
const routeId = canonicalSymbolKey(symbol0, symbol1);
|
|
61
|
+
// Sort tokens to match the canonical route ID order (alphabetical by symbol)
|
|
62
|
+
const sortedTokens = symbol0 < symbol1
|
|
63
|
+
? [
|
|
64
|
+
{ address: pool.token0, symbol: symbol0 },
|
|
65
|
+
{ address: pool.token1, symbol: symbol1 },
|
|
66
|
+
]
|
|
67
|
+
: [
|
|
68
|
+
{ address: pool.token1, symbol: symbol1 },
|
|
69
|
+
{ address: pool.token0, symbol: symbol0 },
|
|
70
|
+
];
|
|
71
|
+
routes.push({
|
|
72
|
+
id: routeId,
|
|
73
|
+
tokens: sortedTokens,
|
|
74
|
+
path: [pool],
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
return routes;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Discovers all tradable routes including multi-hop routes (up to 2 hops)
|
|
81
|
+
* Uses cached data by default for instant results, or generates fresh from blockchain
|
|
82
|
+
*
|
|
83
|
+
* @param options - Configuration options
|
|
84
|
+
* @param options.cached - Whether to use pre-generated cached routes (default: true)
|
|
85
|
+
* @param options.returnAllRoutes - Whether to return all possible routes or just the optimal one per pair (default: false)
|
|
86
|
+
* @returns Array of all tradable routes (direct + multi-hop routes)
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* // Fast: use pre-generated cache
|
|
91
|
+
* const cachedRoutes = await routeService.getRoutes({ cached: true })
|
|
92
|
+
*
|
|
93
|
+
* // Slower but fresh: generate from blockchain
|
|
94
|
+
* const freshRoutes = await routeService.getRoutes({ cached: false })
|
|
95
|
+
*
|
|
96
|
+
* // Get all route variants (useful for cache generation)
|
|
97
|
+
* const allRoutes = await routeService.getRoutes({ cached: false, returnAllRoutes: true })
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
async getRoutes(options) {
|
|
101
|
+
const cached = options?.cached ?? true;
|
|
102
|
+
const returnAllRoutes = options?.returnAllRoutes ?? false;
|
|
103
|
+
const cacheKey = this.getCacheKey(cached, returnAllRoutes);
|
|
104
|
+
const cachedRoutes = this.routeCache.get(cacheKey);
|
|
105
|
+
if (cachedRoutes) {
|
|
106
|
+
return cachedRoutes;
|
|
107
|
+
}
|
|
108
|
+
const inFlight = this.routePromises.get(cacheKey);
|
|
109
|
+
if (inFlight) {
|
|
110
|
+
return inFlight;
|
|
111
|
+
}
|
|
112
|
+
const promise = this.loadRoutes(cached, returnAllRoutes);
|
|
113
|
+
this.routePromises.set(cacheKey, promise);
|
|
114
|
+
try {
|
|
115
|
+
const routes = await promise;
|
|
116
|
+
this.routeCache.set(cacheKey, routes);
|
|
117
|
+
if (!returnAllRoutes) {
|
|
118
|
+
this.routeLookupCache.set(cacheKey, this.buildLookup(routes));
|
|
119
|
+
}
|
|
120
|
+
return routes;
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
this.routePromises.delete(cacheKey);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async warm(options) {
|
|
127
|
+
return this.getRoutes(options);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Looks up the tradable route between two tokens (direct or multi-hop)
|
|
131
|
+
*
|
|
132
|
+
* @param tokenIn - Input token address (direction matters for routing)
|
|
133
|
+
* @param tokenOut - Output token address (direction matters for routing)
|
|
134
|
+
* @param options - Optional configuration (e.g., cached)
|
|
135
|
+
* @returns The optimal tradable route connecting the two tokens
|
|
136
|
+
* @throws {RouteNotFoundError} If no route exists between the token pair
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```typescript
|
|
140
|
+
* const USDm = '0x765DE816845861e75A25fCA122bb6898B8B1282a'
|
|
141
|
+
* const BRLm = '0xE4D5...'
|
|
142
|
+
* const route = await routeService.findRoute(USDm, BRLm)
|
|
143
|
+
*
|
|
144
|
+
* if (route.path.length === 1) {
|
|
145
|
+
* console.log('Direct route available')
|
|
146
|
+
* } else {
|
|
147
|
+
* console.log('Two-hop route:', route.path)
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
async findRoute(tokenIn, tokenOut, options) {
|
|
152
|
+
const cached = options?.cached ?? true;
|
|
153
|
+
const cacheKey = this.getCacheKey(cached, false);
|
|
154
|
+
const routes = await this.getRoutes({ cached, returnAllRoutes: false });
|
|
155
|
+
const lookup = this.routeLookupCache.get(cacheKey) ?? this.buildLookup(routes);
|
|
156
|
+
this.routeLookupCache.set(cacheKey, lookup);
|
|
157
|
+
const matchingRoute = lookup.get(makeTokenPairKey(tokenIn, tokenOut));
|
|
158
|
+
if (!matchingRoute) {
|
|
159
|
+
throw new RouteNotFoundError(tokenIn, tokenOut);
|
|
160
|
+
}
|
|
161
|
+
return matchingRoute;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Generate fresh tradable routes from blockchain data
|
|
165
|
+
* @param returnAllRoutes - Whether to return all routes or just optimal ones per pair
|
|
166
|
+
* @private
|
|
167
|
+
*/
|
|
168
|
+
async generateFreshRoutes(returnAllRoutes = false) {
|
|
169
|
+
// Get direct routes
|
|
170
|
+
const directRoutes = await this.getDirectRoutes();
|
|
171
|
+
if (directRoutes.length === 0) {
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
// Build connectivity structures for route finding
|
|
175
|
+
const connectivity = buildConnectivityStructures(directRoutes);
|
|
176
|
+
// Generate all possible routes (direct + 2-hop)
|
|
177
|
+
const allRoutes = generateAllRoutes(connectivity);
|
|
178
|
+
// Select routes based on returnAllRoutes flag
|
|
179
|
+
const selectedRoutes = selectOptimalRoutes(allRoutes, returnAllRoutes, connectivity.addrToSymbol);
|
|
180
|
+
return selectedRoutes;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Load cached tradable routes for current chain
|
|
184
|
+
* @private
|
|
185
|
+
*/
|
|
186
|
+
async loadCachedRoutes() {
|
|
187
|
+
const { getCachedRoutes } = await import('../../utils/routes');
|
|
188
|
+
const cachedRoutes = await getCachedRoutes(this.chainId);
|
|
189
|
+
return cachedRoutes || [];
|
|
190
|
+
}
|
|
191
|
+
async loadRoutes(cached, returnAllRoutes) {
|
|
192
|
+
if (cached) {
|
|
193
|
+
try {
|
|
194
|
+
const cachedRoutes = await this.loadCachedRoutes();
|
|
195
|
+
if (cachedRoutes.length > 0) {
|
|
196
|
+
return returnAllRoutes ? cachedRoutes : cachedRoutes;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
// Cache miss or corrupt - silently fall through to fresh generation
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return this.generateFreshRoutes(returnAllRoutes);
|
|
204
|
+
}
|
|
205
|
+
getCacheKey(cached, returnAllRoutes) {
|
|
206
|
+
return `${cached ? 'cached' : 'fresh'}:${returnAllRoutes ? 'all' : 'best'}`;
|
|
207
|
+
}
|
|
208
|
+
buildLookup(routes) {
|
|
209
|
+
const lookup = new Map();
|
|
210
|
+
for (const route of routes) {
|
|
211
|
+
lookup.set(makeTokenPairKey(route.tokens[0].address, route.tokens[1].address), route);
|
|
212
|
+
}
|
|
213
|
+
return lookup;
|
|
214
|
+
}
|
|
215
|
+
async hydrateTokenSymbols(addresses) {
|
|
216
|
+
const missingAddresses = addresses.filter((address) => !this.symbolCache.has(address));
|
|
217
|
+
if (missingAddresses.length === 0) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const results = await multicall(this.publicClient, missingAddresses.map((address) => ({
|
|
221
|
+
address: address,
|
|
222
|
+
abi: ERC20_ABI,
|
|
223
|
+
functionName: 'symbol',
|
|
224
|
+
args: [],
|
|
225
|
+
})));
|
|
226
|
+
for (const [index, address] of missingAddresses.entries()) {
|
|
227
|
+
const result = results[index];
|
|
228
|
+
if (!result || result.status === 'failure') {
|
|
229
|
+
this.symbolCache.set(address, address);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
this.symbolCache.set(address, result.result);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Helper: Fetch token symbol from on-chain
|
|
237
|
+
* Results are cached to avoid redundant RPC calls
|
|
238
|
+
* Falls back to address if symbol fetch fails
|
|
239
|
+
*
|
|
240
|
+
* @private
|
|
241
|
+
*/
|
|
242
|
+
async fetchTokenSymbol(address) {
|
|
243
|
+
// Return cached symbol if available
|
|
244
|
+
if (this.symbolCache.has(address)) {
|
|
245
|
+
return this.symbolCache.get(address);
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
const symbol = (await this.publicClient.readContract({
|
|
249
|
+
address: address,
|
|
250
|
+
abi: ERC20_ABI,
|
|
251
|
+
functionName: 'symbol',
|
|
252
|
+
args: [],
|
|
253
|
+
}));
|
|
254
|
+
// Cache the symbol
|
|
255
|
+
this.symbolCache.set(address, symbol);
|
|
256
|
+
return symbol;
|
|
257
|
+
}
|
|
258
|
+
catch {
|
|
259
|
+
// Fallback to address if symbol fetch fails
|
|
260
|
+
this.symbolCache.set(address, address);
|
|
261
|
+
return address;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function makeTokenPairKey(tokenA, tokenB) {
|
|
266
|
+
const [first, second] = [tokenA.toLowerCase(), tokenB.toLowerCase()].sort();
|
|
267
|
+
return `${first}:${second}`;
|
|
268
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './RouteService';
|