From 5c44e0effc7089b4077ca5944aa372a47b81715c Mon Sep 17 00:00:00 2001 From: JJ Adonis Date: Tue, 17 Jan 2023 23:29:47 +0800 Subject: [PATCH] feat(pathfinding): always return the highest yield (#1978) #### What this PR does / why we need it: Initially, the behavior is always to return the highest yield when doing swaps. We have updated it to use direct path first (if available) before considering other options. Even if direct path is more expensive. This will revert it to its original behavior as some pairs are cheaper via composite swaps. (e.g, DFI -> DUSD). Will fix this issue - https://github.com/DeFiCh/wallet/issues/3885 #### Which issue(s) does this PR fixes?: Fixes # #### Additional comments?: --- .../src/module.api/poolpair.controller.e2e.ts | 32 ------------------- .../poolswap.pathfinding.service.ts | 14 -------- 2 files changed, 46 deletions(-) diff --git a/apps/whale-api/src/module.api/poolpair.controller.e2e.ts b/apps/whale-api/src/module.api/poolpair.controller.e2e.ts index 4c8410bc75..56cd291053 100644 --- a/apps/whale-api/src/module.api/poolpair.controller.e2e.ts +++ b/apps/whale-api/src/module.api/poolpair.controller.e2e.ts @@ -512,38 +512,6 @@ describe('get best path', () => { }) }) - it('should return direct path even if composite swap paths has greater return', async () => { - // 1 J = 7 K - // 1 J = 2 L = 8 K - const response = await controller.getBestPath('10', '11') - expect(response).toStrictEqual({ - fromToken: { - id: '10', - name: 'J', - symbol: 'J', - displaySymbol: 'dJ' - }, - toToken: { - id: '11', - name: 'K', - symbol: 'K', - displaySymbol: 'dK' - }, - bestPath: [ - { - symbol: 'J-K', - poolPairId: '23', - priceRatio: { ab: '0.14285714', ba: '7.00000000' }, - tokenA: { id: '10', name: 'J', symbol: 'J', displaySymbol: 'dJ' }, - tokenB: { id: '11', name: 'K', symbol: 'K', displaySymbol: 'dK' }, - commissionFeeInPct: '0.25000000' - } - ], - estimatedReturn: '7.00000000', - estimatedReturnLessDexFees: '5.25000000' - }) - }) - it('should deduct commission fee - 1 leg', async () => { const response = await controller.getBestPath('10', '12') expect(response).toStrictEqual({ diff --git a/apps/whale-api/src/module.api/poolswap.pathfinding.service.ts b/apps/whale-api/src/module.api/poolswap.pathfinding.service.ts index 60ad38437a..091644c8e2 100644 --- a/apps/whale-api/src/module.api/poolswap.pathfinding.service.ts +++ b/apps/whale-api/src/module.api/poolswap.pathfinding.service.ts @@ -52,20 +52,6 @@ export class PoolSwapPathFindingService { paths } = await this.getAllSwapPaths(fromTokenId, toTokenId) - // always use direct path if available - for (const path of paths) { - const { estimatedReturn, estimatedReturnLessDexFees } = computeReturnLessDexFeesInDestinationToken(path, fromTokenId) - if (path.length === 1) { - return { - fromToken: fromToken, - toToken: toToken, - bestPath: path, - estimatedReturn: formatNumber(estimatedReturn), // denoted in toToken - estimatedReturnLessDexFees: formatNumber(estimatedReturnLessDexFees) // denoted in toToken - } - } - } - // otherwise, search for the best path based on return let bestPath: SwapPathPoolPair[] = [] let bestReturnLessDexFees = new BigNumber(0)