-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/carlos/liquidity-manager' into E…
…D-4570-retry-unfeasable
- Loading branch information
Showing
12 changed files
with
255 additions
and
1,332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/liquidity-manager/services/liquidity-providers/LiFi/lifi-provider.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
jest.mock('@lifi/sdk') | ||
|
||
import { FlowProducer, Queue } from 'bullmq' | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { BullModule, getFlowProducerToken, getQueueToken } from '@nestjs/bullmq' | ||
import { createMock, DeepMocked } from '@golevelup/ts-jest' | ||
import * as LiFi from '@lifi/sdk' | ||
import { EcoConfigService } from '@/eco-configs/eco-config.service' | ||
import { LiquidityManagerQueue } from '@/liquidity-manager/queues/liquidity-manager.queue' | ||
import { LiFiProviderService } from '@/liquidity-manager/services/liquidity-providers/LiFi/lifi-provider.service' | ||
import { KernelAccountClientV2Service } from '@/transaction/smart-wallets/kernel/kernel-account-client-v2.service' | ||
|
||
describe('LiFiProviderService', () => { | ||
let lifiProviderService: LiFiProviderService | ||
let kernelAccountClientService: KernelAccountClientV2Service | ||
let ecoConfigService: DeepMocked<EcoConfigService> | ||
let queue: DeepMocked<Queue> | ||
let flowProducer: DeepMocked<FlowProducer> | ||
|
||
beforeEach(async () => { | ||
const chainMod: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
LiFiProviderService, | ||
{ provide: EcoConfigService, useValue: createMock<EcoConfigService>() }, | ||
{ | ||
provide: KernelAccountClientV2Service, | ||
useValue: createMock<KernelAccountClientV2Service>(), | ||
}, | ||
], | ||
imports: [ | ||
BullModule.registerQueue({ name: LiquidityManagerQueue.queueName }), | ||
BullModule.registerFlowProducerAsync({ name: LiquidityManagerQueue.flowName }), | ||
], | ||
}) | ||
.overrideProvider(getQueueToken(LiquidityManagerQueue.queueName)) | ||
.useValue(createMock<Queue>()) | ||
.overrideProvider(getFlowProducerToken(LiquidityManagerQueue.flowName)) | ||
.useValue(createMock<FlowProducer>()) | ||
.compile() | ||
|
||
ecoConfigService = chainMod.get(EcoConfigService) | ||
lifiProviderService = chainMod.get(LiFiProviderService) | ||
kernelAccountClientService = chainMod.get(KernelAccountClientV2Service) | ||
|
||
queue = chainMod.get(getQueueToken(LiquidityManagerQueue.queueName)) | ||
flowProducer = chainMod.get(getFlowProducerToken(LiquidityManagerQueue.flowName)) | ||
}) | ||
|
||
const mockConfig = { | ||
targetSlippage: 0.02, | ||
intervalDuration: 1000, | ||
thresholds: { surplus: 0.1, deficit: 0.2 }, | ||
} | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
describe('OnModuleInit', () => { | ||
it('should configure LiFi SDK on init', async () => { | ||
const mockGetClient = jest.spyOn(kernelAccountClientService, 'getClient') | ||
mockGetClient.mockReturnValue({ account: { address: '0x123' } } as any) | ||
|
||
jest.spyOn(ecoConfigService, 'getIntentSources').mockReturnValue([{ chainID: 10 }] as any) | ||
|
||
const rpcUrls = { '10': 'http://op.rpc.com' } | ||
jest.spyOn(ecoConfigService, 'getChainRPCs').mockReturnValue(rpcUrls) | ||
|
||
await lifiProviderService.onModuleInit() | ||
|
||
expect(mockGetClient).toHaveBeenCalled() | ||
expect(lifiProviderService['walletAddress']).toEqual('0x123') | ||
expect(LiFi.createConfig).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
integrator: 'Eco', | ||
rpcUrls: { '10': [rpcUrls['10']] }, | ||
}), | ||
) | ||
}) | ||
}) | ||
|
||
describe('getQuote', () => { | ||
it('should return a quote', async () => { | ||
const mockTokenIn = { | ||
chainId: 1, | ||
config: { address: '0xTokenIn' }, | ||
balance: { decimals: 18 }, | ||
} | ||
const mockTokenOut = { | ||
chainId: 1, | ||
config: { address: '0xTokenOut' }, | ||
balance: { decimals: 18 }, | ||
} | ||
const mockRoute = { | ||
fromAmount: '1000000000000000000', | ||
toAmount: '2000000000000000000', | ||
toAmountMin: '1900000000000000000', | ||
steps: [], | ||
} | ||
jest.spyOn(LiFi, 'getRoutes').mockResolvedValue({ routes: [mockRoute] } as any) | ||
|
||
const result = await lifiProviderService.getQuote(mockTokenIn as any, mockTokenOut as any, 1) | ||
|
||
expect(result.amountIn).toEqual(BigInt(mockRoute.fromAmount)) | ||
expect(result.amountOut).toEqual(BigInt(mockRoute.toAmount)) | ||
expect(result.slippage).toBeCloseTo(0.05) | ||
expect(result.tokenIn).toEqual(mockTokenIn) | ||
expect(result.tokenOut).toEqual(mockTokenOut) | ||
expect(result.strategy).toEqual('LiFi') | ||
expect(result.context).toEqual(mockRoute) | ||
}) | ||
}) | ||
|
||
describe('execute', () => { | ||
it('should execute a quote', async () => { | ||
const mockQuote = { | ||
tokenIn: { config: { address: '0xTokenIn', chainId: 1 } }, | ||
tokenOut: { config: { address: '0xTokenOut', chainId: 1 } }, | ||
amountIn: BigInt(1000000000000000000), | ||
amountOut: BigInt(2000000000000000000), | ||
slippage: 0.05, | ||
context: { gasCostUSD: 10, steps: [] }, | ||
} | ||
const mockExecuteRoute = jest.spyOn(LiFi, 'executeRoute') | ||
|
||
await lifiProviderService.execute(mockQuote as any) | ||
|
||
expect(mockExecuteRoute).toHaveBeenCalledWith(mockQuote.context, expect.any(Object)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.