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

Commit

Permalink
chore(poolpair.controller): allow to to be optional when history ca…
Browse files Browse the repository at this point in the history
…nnot be resolved (#848)

* poolpair.service.ts to check undefined for history so that to amount can fail with optional

* add composite swap transaction to poolpairs.test.ts
  • Loading branch information
fuxingloh authored Mar 14, 2022
1 parent ee5372a commit cf4f2a5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
70 changes: 59 additions & 11 deletions packages/whale-api-client/__tests__/api/poolpairs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@ describe('poolswap', () => {
tokenTo: 'DUSD'
})

await testing.rpc.poolpair.compositeSwap({
from: await testing.address('swap'),
tokenFrom: 'A',
amountFrom: 123,
to: await testing.address('swap'),
tokenTo: 'C'
})

const height = await container.getBlockCount()
await container.generate(1)
await service.waitForIndexedHeight(height)
Expand All @@ -377,7 +385,22 @@ describe('poolswap', () => {
{
id: expect.any(String),
txid: expect.stringMatching(/[0-f]{64}/),
txno: 1,
txno: expect.any(Number),
poolPairId: '9',
sort: expect.any(String),
fromAmount: '123.00000000',
fromTokenId: 1,
block: {
hash: expect.stringMatching(/[0-f]{64}/),
height: expect.any(Number),
time: expect.any(Number),
medianTime: expect.any(Number)
}
},
{
id: expect.any(String),
txid: expect.stringMatching(/[0-f]{64}/),
txno: expect.any(Number),
poolPairId: '9',
sort: expect.any(String),
fromAmount: '50.00000000',
Expand All @@ -392,7 +415,7 @@ describe('poolswap', () => {
{
id: expect.any(String),
txid: expect.stringMatching(/[0-f]{64}/),
txno: 3,
txno: expect.any(Number),
poolPairId: '9',
sort: expect.any(String),
fromAmount: '25.00000000',
Expand All @@ -412,7 +435,32 @@ describe('poolswap', () => {
{
id: expect.any(String),
txid: expect.stringMatching(/[0-f]{64}/),
txno: 1,
txno: expect.any(Number),
poolPairId: '9',
sort: expect.any(String),
fromAmount: '123.00000000',
fromTokenId: 1,
block: {
hash: expect.stringMatching(/[0-f]{64}/),
height: expect.any(Number),
time: expect.any(Number),
medianTime: expect.any(Number)
},
from: {
address: expect.any(String),
symbol: 'A',
amount: '123.00000000'
},
to: {
address: expect.any(String),
amount: '10.42667420',
symbol: 'C'
}
},
{
id: expect.any(String),
txid: expect.stringMatching(/[0-f]{64}/),
txno: expect.any(Number),
poolPairId: '9',
sort: expect.any(String),
fromAmount: '50.00000000',
Expand All @@ -437,7 +485,7 @@ describe('poolswap', () => {
{
id: expect.any(String),
txid: expect.stringMatching(/[0-f]{64}/),
txno: 3,
txno: expect.any(Number),
poolPairId: '9',
sort: expect.any(String),
fromAmount: '25.00000000',
Expand Down Expand Up @@ -471,14 +519,14 @@ describe('poolswap', () => {
tokenA: {
id: expect.any(String),
symbol: 'A',
reserve: '175',
reserve: '298',
blockCommission: '0',
displaySymbol: 'dA'
},
tokenB: {
id: '0',
symbol: 'DFI',
reserve: '114.2857143',
reserve: '67.11409397',
blockCommission: '0',
displaySymbol: 'DFI'
},
Expand All @@ -490,22 +538,22 @@ describe('poolswap', () => {
commission: '0',
totalLiquidity: {
token: '141.42135623',
usd: '529.6978124963500510109100852'
usd: '311.06415164247241009334543708'
},
tradeEnabled: true,
ownerAddress: expect.any(String),
priceRatio: {
ab: '1.53124999',
ba: '0.65306122'
ab: '4.44019999',
ba: '0.22521508'
},
rewardPct: '0',
creation: {
tx: expect.any(String),
height: expect.any(Number)
},
volume: {
d30: 113.50667410636073,
h24: 113.50667410636073
d30: 103.34010406914354,
h24: 103.34010406914354
}
})

Expand Down
9 changes: 5 additions & 4 deletions src/module.api/poolpair.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,6 @@ export class PoolPairService {
}

const history = await this.getAccountHistory(toAddress, height, txno)
if (history === undefined) {
return undefined
}

return {
from: {
Expand Down Expand Up @@ -401,7 +398,11 @@ function findPoolSwapDfTx (vouts: TransactionVout[]): PoolSwapDfTx | undefined {
}
}

function findPoolSwapFromTo (history: AccountHistory, from: boolean): PoolSwapFromToData | undefined {
function findPoolSwapFromTo (history: AccountHistory | undefined, from: boolean): PoolSwapFromToData | undefined {
if (history?.amounts === undefined) {
return undefined
}

for (const amount of history.amounts) {
const [value, symbol] = amount.split('@')
const isNegative = value.startsWith('-')
Expand Down

0 comments on commit cf4f2a5

Please sign in to comment.