|
| 1 | +import { createPublicClient, http } from 'viem' |
| 2 | +import { PoolService } from '../../../src/services/pools/PoolService' |
| 3 | +import { Pool, PoolType, FPMMPoolDetails, PoolRebalancePreview } from '../../../src/core/types' |
| 4 | +import { ChainId } from '../../../src/core/constants/chainId' |
| 5 | +import { getDefaultRpcUrl } from '../../../src/utils/chainConfig' |
| 6 | + |
| 7 | +/** |
| 8 | + * Integration tests for PoolService.getPoolRebalancePreview() |
| 9 | + * |
| 10 | + * Verifies rebalance preview retrieval from on-chain OpenLiquidityStrategy contracts: |
| 11 | + * - Eligibility filtering (FPMM, out-of-band, strategy registered) |
| 12 | + * - Preview structure and internal consistency |
| 13 | + * - Batch operations |
| 14 | + * |
| 15 | + * Note: Results depend on live market state — pools may or may not be out-of-band. |
| 16 | + * |
| 17 | + * Tests are parameterised across all deployed chains to ensure |
| 18 | + * coverage is not lost when new chains are added. |
| 19 | + * |
| 20 | + * @group integration |
| 21 | + */ |
| 22 | + |
| 23 | +interface ChainTestConfig { |
| 24 | + name: string |
| 25 | + chainId: number |
| 26 | + rpcEnvVar: string |
| 27 | +} |
| 28 | + |
| 29 | +const CHAIN_CONFIGS: ChainTestConfig[] = [ |
| 30 | + { |
| 31 | + name: 'Celo Mainnet', |
| 32 | + chainId: ChainId.CELO, |
| 33 | + rpcEnvVar: 'CELO_RPC_URL', |
| 34 | + }, |
| 35 | + { |
| 36 | + name: 'Celo Sepolia', |
| 37 | + chainId: ChainId.CELO_SEPOLIA, |
| 38 | + rpcEnvVar: 'CELO_SEPOLIA_RPC_URL', |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'Monad Testnet', |
| 42 | + chainId: ChainId.MONAD_TESTNET, |
| 43 | + rpcEnvVar: 'MONAD_TESTNET_RPC_URL', |
| 44 | + }, |
| 45 | + { |
| 46 | + name: 'Monad', |
| 47 | + chainId: ChainId.MONAD, |
| 48 | + rpcEnvVar: 'MONAD_RPC_URL', |
| 49 | + }, |
| 50 | +] |
| 51 | + |
| 52 | +describe.each(CHAIN_CONFIGS)('PoolService.getPoolRebalancePreview() Integration - $name', ({ chainId, rpcEnvVar }) => { |
| 53 | + const RPC_URL = process.env[rpcEnvVar] || getDefaultRpcUrl(chainId) |
| 54 | + |
| 55 | + const publicClient = createPublicClient({ |
| 56 | + transport: http(RPC_URL), |
| 57 | + }) |
| 58 | + |
| 59 | + const poolService = new PoolService(publicClient, chainId) |
| 60 | + |
| 61 | + let pools: Pool[] |
| 62 | + let fpmmPool: Pool | undefined |
| 63 | + let fpmmPoolWithStrategy: Pool | undefined |
| 64 | + let virtualPool: Pool | undefined |
| 65 | + |
| 66 | + beforeAll(async () => { |
| 67 | + pools = await poolService.getPools() |
| 68 | + fpmmPool = pools.find((p) => p.poolType === PoolType.FPMM) |
| 69 | + virtualPool = pools.find((p) => p.poolType === PoolType.Virtual) |
| 70 | + |
| 71 | + // Find an FPMM pool with a registered liquidity strategy |
| 72 | + for (const pool of pools.filter((p) => p.poolType === PoolType.FPMM)) { |
| 73 | + const details = (await poolService.getPoolDetails(pool.poolAddr)) as FPMMPoolDetails |
| 74 | + if (details.rebalancing.liquidityStrategy) { |
| 75 | + fpmmPoolWithStrategy = pool |
| 76 | + break |
| 77 | + } |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + describe('single pool preview', () => { |
| 82 | + it('should discover at least one FPMM pool', () => { |
| 83 | + expect(fpmmPool).toBeDefined() |
| 84 | + }) |
| 85 | + |
| 86 | + it('should return a preview or null for an FPMM pool with a strategy', async () => { |
| 87 | + if (!fpmmPoolWithStrategy) { |
| 88 | + console.log('No FPMM pool with a registered liquidity strategy found — skipping') |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + const preview = await poolService.getPoolRebalancePreview(fpmmPoolWithStrategy.poolAddr) |
| 93 | + |
| 94 | + if (preview === null) { |
| 95 | + // Pool is in-band or FX market is closed — valid result |
| 96 | + console.log('Pool is currently in-band or market closed — preview is null') |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + // Validate structure |
| 101 | + expect(preview.poolAddress).toBe(fpmmPoolWithStrategy.poolAddr) |
| 102 | + expect(preview.strategyAddress).toMatch(/^0x[a-fA-F0-9]{40}$/) |
| 103 | + expect(['Expand', 'Contract']).toContain(preview.direction) |
| 104 | + |
| 105 | + // Token assignments |
| 106 | + const poolTokens = [fpmmPoolWithStrategy.token0.toLowerCase(), fpmmPoolWithStrategy.token1.toLowerCase()] |
| 107 | + expect(poolTokens).toContain(preview.inputToken.toLowerCase()) |
| 108 | + expect(poolTokens).toContain(preview.outputToken.toLowerCase()) |
| 109 | + expect(preview.inputToken.toLowerCase()).not.toBe(preview.outputToken.toLowerCase()) |
| 110 | + |
| 111 | + // Amount fields reference correct tokens |
| 112 | + expect(preview.amountRequired.token.toLowerCase()).toBe(preview.inputToken.toLowerCase()) |
| 113 | + expect(preview.amountTransferred.token.toLowerCase()).toBe(preview.outputToken.toLowerCase()) |
| 114 | + expect(preview.protocolIncentive.token.toLowerCase()).toBe(preview.outputToken.toLowerCase()) |
| 115 | + expect(preview.liquiditySourceIncentive.token.toLowerCase()).toBe(preview.outputToken.toLowerCase()) |
| 116 | + |
| 117 | + // Approval fields |
| 118 | + expect(preview.approvalToken.toLowerCase()).toBe(preview.inputToken.toLowerCase()) |
| 119 | + expect(preview.approvalSpender).toBe(preview.strategyAddress) |
| 120 | + expect(preview.approvalAmount).toBe(preview.amountRequired.amount) |
| 121 | + |
| 122 | + // Amount sanity |
| 123 | + expect(preview.amountTransferred.amount).toBeGreaterThan(0n) |
| 124 | + expect(preview.protocolIncentive.amount).toBeLessThanOrEqual(preview.amountTransferred.amount) |
| 125 | + expect(preview.liquiditySourceIncentive.amount).toBeLessThanOrEqual(preview.amountTransferred.amount) |
| 126 | + }) |
| 127 | + |
| 128 | + it('should return valid config and context when preview is available', async () => { |
| 129 | + if (!fpmmPoolWithStrategy) return |
| 130 | + |
| 131 | + const preview = await poolService.getPoolRebalancePreview(fpmmPoolWithStrategy.poolAddr) |
| 132 | + if (!preview) return |
| 133 | + |
| 134 | + // Config validation |
| 135 | + expect(preview.config.protocolFeeRecipient).toMatch(/^0x[a-fA-F0-9]{40}$/) |
| 136 | + expect(preview.config.rebalanceCooldown).toBeGreaterThanOrEqual(0) |
| 137 | + expect(preview.config.lastRebalance).toBeGreaterThanOrEqual(0) |
| 138 | + |
| 139 | + // Context token addresses match pool |
| 140 | + expect(preview.context.token0.toLowerCase()).toBe(fpmmPoolWithStrategy.token0.toLowerCase()) |
| 141 | + expect(preview.context.token1.toLowerCase()).toBe(fpmmPoolWithStrategy.token1.toLowerCase()) |
| 142 | + expect(preview.context.pool.toLowerCase()).toBe(fpmmPoolWithStrategy.poolAddr.toLowerCase()) |
| 143 | + |
| 144 | + // Action direction matches preview direction |
| 145 | + expect(preview.action.dir).toBe(preview.direction) |
| 146 | + }) |
| 147 | + |
| 148 | + it('should return null for a Virtual pool', async () => { |
| 149 | + if (!virtualPool) { |
| 150 | + console.log('No Virtual pools found on this chain — skipping') |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + const preview = await poolService.getPoolRebalancePreview(virtualPool.poolAddr) |
| 155 | + expect(preview).toBeNull() |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + describe('batch preview', () => { |
| 160 | + it('should return array matching total pool count', async () => { |
| 161 | + const previews = await poolService.getPoolRebalancePreviewBatch() |
| 162 | + |
| 163 | + expect(previews).toHaveLength(pools.length) |
| 164 | + for (const preview of previews) { |
| 165 | + expect(preview === null || typeof preview === 'object').toBe(true) |
| 166 | + } |
| 167 | + }) |
| 168 | + |
| 169 | + it('should return valid previews for non-null entries in batch', async () => { |
| 170 | + const previews = await poolService.getPoolRebalancePreviewBatch() |
| 171 | + const nonNullPreviews = previews.filter((p): p is PoolRebalancePreview => p !== null) |
| 172 | + |
| 173 | + for (const preview of nonNullPreviews) { |
| 174 | + expect(preview.poolAddress).toMatch(/^0x[a-fA-F0-9]{40}$/) |
| 175 | + expect(preview.strategyAddress).toMatch(/^0x[a-fA-F0-9]{40}$/) |
| 176 | + expect(['Expand', 'Contract']).toContain(preview.direction) |
| 177 | + expect(preview.amountTransferred.amount).toBeGreaterThan(0n) |
| 178 | + } |
| 179 | + }) |
| 180 | + }) |
| 181 | + |
| 182 | + describe('consistency', () => { |
| 183 | + it('single and batch results should agree for the same pool', async () => { |
| 184 | + if (!fpmmPoolWithStrategy) return |
| 185 | + |
| 186 | + const single = await poolService.getPoolRebalancePreview(fpmmPoolWithStrategy.poolAddr) |
| 187 | + const [batch] = await poolService.getPoolRebalancePreviewBatch([fpmmPoolWithStrategy.poolAddr]) |
| 188 | + |
| 189 | + if (single === null) { |
| 190 | + expect(batch).toBeNull() |
| 191 | + } else { |
| 192 | + expect(batch).not.toBeNull() |
| 193 | + expect(batch!.poolAddress).toBe(single.poolAddress) |
| 194 | + expect(batch!.direction).toBe(single.direction) |
| 195 | + expect(batch!.inputToken).toBe(single.inputToken) |
| 196 | + expect(batch!.outputToken).toBe(single.outputToken) |
| 197 | + expect(batch!.strategyAddress).toBe(single.strategyAddress) |
| 198 | + } |
| 199 | + }) |
| 200 | + }) |
| 201 | +}) |
0 commit comments