|
| 1 | +import { createMockSdkInstance } from '@/test/helpers/sdkInstance' |
| 2 | +import { getPosition } from '@/utils/getPosition' |
| 3 | +import { Token } from '@uniswap/sdk-core' |
| 4 | +import { Pool, Position, V4PositionManager } from '@uniswap/v4-sdk' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const instance = createMockSdkInstance() |
| 8 | + |
| 9 | +vi.mock('@/utils/getPosition', () => ({ |
| 10 | + getPosition: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +const token0 = new Token(1, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 6, 'USDC', 'USD Coin') |
| 14 | +const token1 = new Token( |
| 15 | + 1, |
| 16 | + '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', |
| 17 | + 18, |
| 18 | + 'WETH', |
| 19 | + 'Wrapped Ether', |
| 20 | +) |
| 21 | +const pool = new Pool( |
| 22 | + token0, |
| 23 | + token1, |
| 24 | + 3000, |
| 25 | + 60, |
| 26 | + '0x1111111111111111111111111111111111111111', |
| 27 | + '79228162514264337593543950336', |
| 28 | + '1000000', // liquidity as string |
| 29 | + 0, |
| 30 | +) |
| 31 | +const position = new Position({ pool, liquidity: '1000000', tickLower: -60, tickUpper: 60 }) // liquidity as string |
| 32 | + |
| 33 | +const mockPosition = { |
| 34 | + position, |
| 35 | + pool, |
| 36 | + token0, |
| 37 | + token1, |
| 38 | + poolId: '0x1111111111111111111111111111111111111111' as `0x${string}`, |
| 39 | + tokenId: '1', |
| 40 | +} |
| 41 | + |
| 42 | +describe('buildRemoveLiquidityCallData', () => { |
| 43 | + beforeEach(() => { |
| 44 | + vi.resetAllMocks() |
| 45 | + }) |
| 46 | + |
| 47 | + it('should build calldata for removing 100% liquidity', async () => { |
| 48 | + vi.mock('@/utils/getDefaultDeadline', () => ({ |
| 49 | + getDefaultDeadline: vi.fn().mockResolvedValue('1234567890'), |
| 50 | + })) |
| 51 | + vi.mocked(getPosition).mockReturnValueOnce(Promise.resolve(mockPosition)) |
| 52 | + vi.spyOn(V4PositionManager, 'removeCallParameters').mockReturnValueOnce({ |
| 53 | + calldata: '0x123', |
| 54 | + value: '0', |
| 55 | + }) |
| 56 | + const { buildRemoveLiquidityCallData } = await import('@/utils/buildRemoveLiquidityCallData') |
| 57 | + const result = await buildRemoveLiquidityCallData( |
| 58 | + { |
| 59 | + liquidityPercentage: 10_000, |
| 60 | + tokenId: '1', |
| 61 | + deadline: '123', |
| 62 | + }, |
| 63 | + instance, |
| 64 | + ) |
| 65 | + expect(result.calldata).toBe('0x123') |
| 66 | + expect(result.value).toBe('0') |
| 67 | + }) |
| 68 | + |
| 69 | + it('should use custom slippageTolerance', async () => { |
| 70 | + vi.mock('@/utils/getDefaultDeadline', () => ({ |
| 71 | + getDefaultDeadline: vi.fn().mockResolvedValue('1234567890'), |
| 72 | + })) |
| 73 | + vi.mocked(getPosition).mockReturnValueOnce(Promise.resolve(mockPosition)) |
| 74 | + const spy = vi.spyOn(V4PositionManager, 'removeCallParameters').mockReturnValueOnce({ |
| 75 | + calldata: '0xabc', |
| 76 | + value: '1', |
| 77 | + }) |
| 78 | + const { buildRemoveLiquidityCallData } = await import('@/utils/buildRemoveLiquidityCallData') |
| 79 | + await buildRemoveLiquidityCallData( |
| 80 | + { |
| 81 | + liquidityPercentage: 5000, |
| 82 | + tokenId: '1', |
| 83 | + slippageTolerance: 123, |
| 84 | + deadline: '123', |
| 85 | + }, |
| 86 | + instance, |
| 87 | + ) |
| 88 | + expect(spy).toHaveBeenCalledWith( |
| 89 | + mockPosition.position, |
| 90 | + expect.objectContaining({ slippageTolerance: expect.any(Object) }), |
| 91 | + ) |
| 92 | + }) |
| 93 | + |
| 94 | + it('should throw if position not found', async () => { |
| 95 | + vi.mock('@/utils/getDefaultDeadline', () => ({ |
| 96 | + getDefaultDeadline: vi.fn().mockResolvedValue('1234567890'), |
| 97 | + })) |
| 98 | + vi.mocked(getPosition).mockReturnValueOnce( |
| 99 | + Promise.resolve(undefined as unknown as ReturnType<typeof getPosition>), |
| 100 | + ) |
| 101 | + const { buildRemoveLiquidityCallData } = await import('@/utils/buildRemoveLiquidityCallData') |
| 102 | + await expect( |
| 103 | + buildRemoveLiquidityCallData({ liquidityPercentage: 10_000, tokenId: '404' }, instance), |
| 104 | + ).rejects.toThrow('Position not found') |
| 105 | + }) |
| 106 | + |
| 107 | + it('should throw if V4PositionManager throws', async () => { |
| 108 | + vi.mock('@/utils/getDefaultDeadline', () => ({ |
| 109 | + getDefaultDeadline: vi.fn().mockResolvedValue('1234567890'), |
| 110 | + })) |
| 111 | + vi.mocked(getPosition).mockReturnValueOnce(Promise.resolve(mockPosition)) |
| 112 | + vi.spyOn(V4PositionManager, 'removeCallParameters').mockImplementationOnce(() => { |
| 113 | + throw new Error('fail') |
| 114 | + }) |
| 115 | + const { buildRemoveLiquidityCallData } = await import('@/utils/buildRemoveLiquidityCallData') |
| 116 | + await expect( |
| 117 | + buildRemoveLiquidityCallData( |
| 118 | + { liquidityPercentage: 10_000, tokenId: '1', deadline: '123' }, |
| 119 | + instance, |
| 120 | + ), |
| 121 | + ).rejects.toThrow('fail') |
| 122 | + }) |
| 123 | +}) |
0 commit comments