|
| 1 | +import { NATIVE_CURRENCY_ADDRESS, SupportedChainId, TokenInfo } from '@cowprotocol/sdk-config' |
| 2 | +import { determineIntermediateToken } from './determineIntermediateToken' |
| 3 | +import { BridgeProviderQuoteError } from '../errors' |
| 4 | + |
| 5 | +describe('determineIntermediateToken', () => { |
| 6 | + // Sample tokens for testing |
| 7 | + const usdcMainnet: TokenInfo = { |
| 8 | + chainId: SupportedChainId.MAINNET, |
| 9 | + address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC on Mainnet |
| 10 | + decimals: 6, |
| 11 | + name: 'USDC', |
| 12 | + symbol: 'USDC', |
| 13 | + } |
| 14 | + |
| 15 | + const usdtMainnet: TokenInfo = { |
| 16 | + chainId: SupportedChainId.MAINNET, |
| 17 | + address: '0xdAC17F958D2ee523a2206206994597C13D831ec7', // USDT on Mainnet |
| 18 | + decimals: 6, |
| 19 | + name: 'USDT', |
| 20 | + symbol: 'USDT', |
| 21 | + } |
| 22 | + |
| 23 | + const wethMainnet: TokenInfo = { |
| 24 | + chainId: SupportedChainId.MAINNET, |
| 25 | + address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', // WETH on Mainnet |
| 26 | + decimals: 18, |
| 27 | + name: 'WETH', |
| 28 | + symbol: 'WETH', |
| 29 | + } |
| 30 | + |
| 31 | + const nativeEth: TokenInfo = { |
| 32 | + chainId: SupportedChainId.MAINNET, |
| 33 | + address: NATIVE_CURRENCY_ADDRESS, |
| 34 | + decimals: 18, |
| 35 | + name: 'Ether', |
| 36 | + symbol: 'ETH', |
| 37 | + } |
| 38 | + |
| 39 | + const randomToken: TokenInfo = { |
| 40 | + chainId: SupportedChainId.MAINNET, |
| 41 | + address: '0x1111111111111111111111111111111111111111', |
| 42 | + decimals: 18, |
| 43 | + name: 'Random', |
| 44 | + symbol: 'RND', |
| 45 | + } |
| 46 | + |
| 47 | + const usdcArbitrum: TokenInfo = { |
| 48 | + chainId: SupportedChainId.ARBITRUM_ONE, |
| 49 | + address: '0xaf88d065e77c8cc2239327c5edb3a432268e5831', // USDC on Arbitrum |
| 50 | + decimals: 6, |
| 51 | + name: 'USDC', |
| 52 | + symbol: 'USDC', |
| 53 | + } |
| 54 | + |
| 55 | + describe('error handling', () => { |
| 56 | + it('should throw error when no intermediate tokens provided', async () => { |
| 57 | + await expect(determineIntermediateToken(SupportedChainId.MAINNET, [])).rejects.toThrow(BridgeProviderQuoteError) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should throw error when intermediateTokens is empty array', async () => { |
| 61 | + await expect(determineIntermediateToken(SupportedChainId.MAINNET, [])).rejects.toThrow(BridgeProviderQuoteError) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + describe('priority level: HIGHEST (stablecoins)', () => { |
| 66 | + it('should prioritize USDC over other tokens', async () => { |
| 67 | + const result = await determineIntermediateToken(SupportedChainId.MAINNET, [randomToken, wethMainnet, usdcMainnet]) |
| 68 | + |
| 69 | + expect(result).toBe(usdcMainnet) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should prioritize USDT over other tokens', async () => { |
| 73 | + const result = await determineIntermediateToken(SupportedChainId.MAINNET, [randomToken, wethMainnet, usdtMainnet]) |
| 74 | + |
| 75 | + expect(result).toBe(usdtMainnet) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should prioritize first stablecoin when both USDC and USDT present', async () => { |
| 79 | + const result = await determineIntermediateToken(SupportedChainId.MAINNET, [usdcMainnet, usdtMainnet]) |
| 80 | + |
| 81 | + expect(result).toBe(usdcMainnet) |
| 82 | + |
| 83 | + // Order matters - maintain stable sort |
| 84 | + const result2 = await determineIntermediateToken(SupportedChainId.MAINNET, [usdtMainnet, usdcMainnet]) |
| 85 | + |
| 86 | + expect(result2).toBe(usdtMainnet) |
| 87 | + }) |
| 88 | + |
| 89 | + it('should prioritize stablecoins on different chains', async () => { |
| 90 | + const result = await determineIntermediateToken(SupportedChainId.ARBITRUM_ONE, [randomToken, usdcArbitrum]) |
| 91 | + |
| 92 | + expect(result).toBe(usdcArbitrum) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + describe('priority level: HIGH (correlated tokens)', () => { |
| 97 | + it('should prioritize correlated tokens over non-correlated', async () => { |
| 98 | + const getCorrelatedTokens = async () => [wethMainnet] |
| 99 | + |
| 100 | + const result = await determineIntermediateToken( |
| 101 | + SupportedChainId.MAINNET, |
| 102 | + [randomToken, wethMainnet], |
| 103 | + getCorrelatedTokens, |
| 104 | + ) |
| 105 | + |
| 106 | + expect(result).toBe(wethMainnet) |
| 107 | + }) |
| 108 | + |
| 109 | + it('should prioritize stablecoins over correlated tokens', async () => { |
| 110 | + const getCorrelatedTokens = async () => [wethMainnet] |
| 111 | + |
| 112 | + const result = await determineIntermediateToken( |
| 113 | + SupportedChainId.MAINNET, |
| 114 | + [wethMainnet, usdcMainnet], |
| 115 | + getCorrelatedTokens, |
| 116 | + ) |
| 117 | + |
| 118 | + expect(result).toBe(usdcMainnet) |
| 119 | + }) |
| 120 | + |
| 121 | + it('should handle multiple correlated tokens with stable sort', async () => { |
| 122 | + const daiToken: TokenInfo = { |
| 123 | + chainId: SupportedChainId.MAINNET, |
| 124 | + address: '0x6B175474E89094C44Da98b954EedeAC495271d0F', |
| 125 | + decimals: 18, |
| 126 | + name: 'DAI', |
| 127 | + symbol: 'DAI', |
| 128 | + } |
| 129 | + |
| 130 | + const getCorrelatedTokens = async () => [wethMainnet, daiToken] |
| 131 | + |
| 132 | + const result = await determineIntermediateToken( |
| 133 | + SupportedChainId.MAINNET, |
| 134 | + [randomToken, wethMainnet, daiToken], |
| 135 | + getCorrelatedTokens, |
| 136 | + ) |
| 137 | + |
| 138 | + // Should return first correlated token in original order |
| 139 | + expect(result).toBe(wethMainnet) |
| 140 | + }) |
| 141 | + |
| 142 | + it('should gracefully handle getCorrelatedTokens failure', async () => { |
| 143 | + const getCorrelatedTokens = async () => { |
| 144 | + throw new Error('API error') |
| 145 | + } |
| 146 | + |
| 147 | + const result = await determineIntermediateToken( |
| 148 | + SupportedChainId.MAINNET, |
| 149 | + [randomToken, wethMainnet], |
| 150 | + getCorrelatedTokens, |
| 151 | + ) |
| 152 | + |
| 153 | + // Should fallback and still work |
| 154 | + expect(result).toBe(randomToken) |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + describe('priority level: MEDIUM (native tokens)', () => { |
| 159 | + it('should prioritize native token over random tokens', async () => { |
| 160 | + const result = await determineIntermediateToken(SupportedChainId.MAINNET, [randomToken, nativeEth]) |
| 161 | + |
| 162 | + expect(result).toBe(nativeEth) |
| 163 | + }) |
| 164 | + |
| 165 | + it('should prioritize stablecoins over native token', async () => { |
| 166 | + const result = await determineIntermediateToken(SupportedChainId.MAINNET, [nativeEth, usdcMainnet]) |
| 167 | + |
| 168 | + expect(result).toBe(usdcMainnet) |
| 169 | + }) |
| 170 | + |
| 171 | + it('should prioritize correlated tokens over native token', async () => { |
| 172 | + const getCorrelatedTokens = async () => [wethMainnet] |
| 173 | + |
| 174 | + const result = await determineIntermediateToken( |
| 175 | + SupportedChainId.MAINNET, |
| 176 | + [nativeEth, wethMainnet], |
| 177 | + getCorrelatedTokens, |
| 178 | + ) |
| 179 | + |
| 180 | + expect(result).toBe(wethMainnet) |
| 181 | + }) |
| 182 | + }) |
| 183 | + |
| 184 | + describe('priority level: LOW (other tokens)', () => { |
| 185 | + it('should return first token when all have low priority', async () => { |
| 186 | + const token1: TokenInfo = { |
| 187 | + chainId: SupportedChainId.MAINNET, |
| 188 | + address: '0x1111111111111111111111111111111111111111', |
| 189 | + decimals: 18, |
| 190 | + name: 'Token1', |
| 191 | + } |
| 192 | + |
| 193 | + const token2: TokenInfo = { |
| 194 | + chainId: SupportedChainId.MAINNET, |
| 195 | + address: '0x2222222222222222222222222222222222222222', |
| 196 | + decimals: 18, |
| 197 | + name: 'Token2', |
| 198 | + } |
| 199 | + |
| 200 | + const result = await determineIntermediateToken(SupportedChainId.MAINNET, [token1, token2]) |
| 201 | + |
| 202 | + expect(result).toBe(token1) |
| 203 | + }) |
| 204 | + }) |
| 205 | +}) |
0 commit comments