Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Commit

Permalink
Add to listPoolSwapsVerbose to return displaySymbol of swap (#847)
Browse files Browse the repository at this point in the history
* Add to listPoolSwapsVerbose to return displaySymbol of swap

* Refactor displaySymbol into to and from

* Add to listPoolSwapsVerbose to return displaySymbol of swap

* Refactor displaySymbol into to and from

* Revise logic to determine from and to token display symbols

* Refactor code to use function directly instead of parsing as argument
  • Loading branch information
nichellekoh authored Mar 14, 2022
1 parent 15a7a98 commit 9242603
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
18 changes: 12 additions & 6 deletions packages/whale-api-client/__tests__/api/poolpairs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,14 @@ describe('poolswap', () => {
from: {
address: expect.any(String),
symbol: 'A',
amount: '123.00000000'
amount: '123.00000000',
displaySymbol: 'dA'
},
to: {
address: expect.any(String),
amount: '10.42667420',
symbol: 'C'
symbol: 'C',
displaySymbol: 'dC'
}
},
{
Expand All @@ -474,12 +476,14 @@ describe('poolswap', () => {
from: {
address: expect.any(String),
symbol: 'A',
amount: '50.00000000'
amount: '50.00000000',
displaySymbol: 'dA'
},
to: {
address: expect.any(String),
amount: '45.71428571',
symbol: 'DFI'
symbol: 'DFI',
displaySymbol: 'DFI'
}
},
{
Expand All @@ -499,12 +503,14 @@ describe('poolswap', () => {
from: {
address: expect.any(String),
symbol: 'A',
amount: '25.00000000'
amount: '25.00000000',
displaySymbol: 'dA'
},
to: {
address: expect.any(String),
amount: '39.99999999',
symbol: 'DFI'
symbol: 'DFI',
displaySymbol: 'DFI'
}
}
])
Expand Down
1 change: 1 addition & 0 deletions packages/whale-api-client/src/api/poolpairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export interface PoolSwapFromToData {
address: string
amount: string
symbol: string
displaySymbol: string
}

export interface PoolSwapAggregatedData {
Expand Down
18 changes: 12 additions & 6 deletions src/module.api/poolpair.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { fromScript } from '@defichain/jellyfish-address'
import { NetworkName } from '@defichain/jellyfish-network'
import { AccountHistory } from '@defichain/jellyfish-api-core/dist/category/account'
import { DeFiDCache } from '@src/module.api/cache/defid.cache'
import { parseDisplaySymbol } from '@src/module.api/token.controller'
import { UndirectedGraph } from 'graphology'
import { PoolPairToken, PoolPairTokenMapper } from '@src/module.model/pool.pair.token'
import { Interval } from '@nestjs/schedule'
Expand Down Expand Up @@ -234,7 +235,9 @@ export class PoolPairService {
const fromToken = await this.deFiDCache.getTokenInfo(dftx.fromTokenId.toString())

const toAddress = fromScript(dftx.toScript, this.network)?.address
if (fromAddress === undefined || toAddress === undefined || fromToken === undefined) {
const toToken = await this.deFiDCache.getTokenInfo(dftx.toTokenId.toString())

if (fromAddress === undefined || toAddress === undefined || fromToken === undefined || toToken === undefined) {
return undefined
}

Expand All @@ -244,9 +247,10 @@ export class PoolPairService {
from: {
address: fromAddress,
symbol: fromToken.symbol,
amount: dftx.fromAmount.toFixed(8)
amount: dftx.fromAmount.toFixed(8),
displaySymbol: parseDisplaySymbol(fromToken)
},
to: findPoolSwapFromTo(history, false)
to: findPoolSwapFromTo(history, false, parseDisplaySymbol(toToken))
}
}

Expand Down Expand Up @@ -402,7 +406,7 @@ function findPoolSwapDfTx (vouts: TransactionVout[]): PoolSwapDfTx | undefined {
}
}

function findPoolSwapFromTo (history: AccountHistory | undefined, from: boolean): PoolSwapFromToData | undefined {
function findPoolSwapFromTo (history: AccountHistory | undefined, from: boolean, displaySymbol: string): PoolSwapFromToData | undefined {
if (history?.amounts === undefined) {
return undefined
}
Expand All @@ -415,15 +419,17 @@ function findPoolSwapFromTo (history: AccountHistory | undefined, from: boolean)
return {
address: history.owner,
amount: new BigNumber(value).absoluteValue().toFixed(8),
symbol: symbol
symbol: symbol,
displaySymbol: displaySymbol
}
}

if (!isNegative && !from) {
return {
address: history.owner,
amount: new BigNumber(value).absoluteValue().toFixed(8),
symbol: symbol
symbol: symbol,
displaySymbol: displaySymbol
}
}
}
Expand Down

0 comments on commit 9242603

Please sign in to comment.