Skip to content

Commit

Permalink
feat(apps/whale-api): revert "feat(apps/whale-api): always return the…
Browse files Browse the repository at this point in the history
… highest yield" (#1990)
  • Loading branch information
thedoublejay authored Jan 19, 2023
1 parent 3d0e633 commit 8fb4248
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
32 changes: 32 additions & 0 deletions apps/whale-api/src/module.api/poolpair.controller.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,38 @@ 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({
Expand Down
14 changes: 14 additions & 0 deletions apps/whale-api/src/module.api/poolswap.pathfinding.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ 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)
Expand Down

0 comments on commit 8fb4248

Please sign in to comment.