Skip to content

Commit

Permalink
added poolpair stats priceRatio and totalLiquidityUsd (#202)
Browse files Browse the repository at this point in the history
* fix

* wip

* add poolpair.service

* added price ratio and totalLiqudityUsd

* add poolpair get service test

* revert config

* revert mapTokenData

* stub testPoolSwap on test

* remove unuse comment

* fix poolpair.test.ts

* fix poolpair.e2e, refine PoolPairData

* use static address for testpoolswap

* get usdtdfi conv price by getpoolpair, remove testpoolswap

* remove log

* tight logic on checking poolpair token id

* added DFI-token poolpair test

* prchanges: rename tokenA/tokenB to ab

* pr changes: remove mock, use created fixture

* refactor poolpair.service.ts to sync instead of live fetching

Co-authored-by: Fuxing Loh <[email protected]>
  • Loading branch information
canonbrother and fuxingloh authored Jul 23, 2021
1 parent fd277b2 commit e8a1ffd
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 56 deletions.
147 changes: 92 additions & 55 deletions packages/whale-api-client/__tests__/api/poolpair.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { StubWhaleApiClient } from '../stub.client'
import { StubService } from '../stub.service'
import { WhaleApiClient, WhaleApiException } from '../../src'
import { createPoolPair, createToken, addPoolLiquidity, getNewAddress, mintTokens } from '@defichain/testing'
import { ApiPagedResponse, WhaleApiClient, WhaleApiException } from '../../src'
import { addPoolLiquidity, createPoolPair, createToken, getNewAddress, mintTokens } from '@defichain/testing'
import { PoolPairService } from '@src/module.api/poolpair.service'
import { PoolPairData } from '@whale-api-client/api/poolpair'
import waitForExpect from 'wait-for-expect'

let container: MasterNodeRegTestContainer
let service: StubService
Expand All @@ -18,81 +21,107 @@ beforeAll(async () => {
await container.waitForWalletCoinbaseMaturity()
await service.start()

const tokens = ['A', 'B', 'C', 'D', 'E', 'F']
await setup()

await waitForExpect(() => {
// @ts-expect-error
expect(service.app?.get(PoolPairService).USDT_PER_DFI).toBeDefined()
}, 61000) // 60 seconds interval
})

afterAll(async () => {
try {
await service.stop()
} finally {
await container.stop()
}
})

async function setup (): Promise<void> {
const tokens = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']

for (const token of tokens) {
await container.waitForWalletBalanceGTE(110)
await createToken(container, token)
await mintTokens(container, token)
}
await createPoolPair(container, 'A', 'B')
await createPoolPair(container, 'A', 'C')
await createPoolPair(container, 'A', 'D')
await createPoolPair(container, 'A', 'E')
await createPoolPair(container, 'A', 'F')
await createPoolPair(container, 'B', 'C')
await createPoolPair(container, 'B', 'D')
await createPoolPair(container, 'B', 'E')
await container.generate(1)
await createPoolPair(container, 'A', 'DFI')
await createPoolPair(container, 'B', 'DFI')
await createPoolPair(container, 'C', 'DFI')
await createPoolPair(container, 'D', 'DFI')
await createPoolPair(container, 'E', 'DFI')
await createPoolPair(container, 'F', 'DFI')
await createPoolPair(container, 'G', 'DFI')
await createPoolPair(container, 'H', 'DFI')

await addPoolLiquidity(container, {
tokenA: 'A',
amountA: 100,
tokenB: 'B',
tokenB: 'DFI',
amountB: 200,
shareAddress: await getNewAddress(container)
})
await addPoolLiquidity(container, {
tokenA: 'A',
tokenA: 'B',
amountA: 50,
tokenB: 'C',
tokenB: 'DFI',
amountB: 300,
shareAddress: await getNewAddress(container)
})
await addPoolLiquidity(container, {
tokenA: 'A',
tokenA: 'C',
amountA: 90,
tokenB: 'D',
tokenB: 'DFI',
amountB: 360,
shareAddress: await getNewAddress(container)
})
await container.generate(1)
})

afterAll(async () => {
try {
await service.stop()
} finally {
await container.stop()
}
})
// dexUsdtDfi setup
await createToken(container, 'USDT')
await createPoolPair(container, 'USDT', 'DFI')
await mintTokens(container, 'USDT')
await addPoolLiquidity(container, {
tokenA: 'USDT',
amountA: 1000,
tokenB: 'DFI',
amountB: 431.51288,
shareAddress: await getNewAddress(container)
})
}

describe('list', () => {
it('should list', async () => {
const response = await client.poolpair.list(30)
const response: ApiPagedResponse<PoolPairData> = await client.poolpair.list(30)

expect(response.length).toStrictEqual(8)
expect(response.length).toStrictEqual(9)
expect(response.hasNext).toStrictEqual(false)

expect(response[1]).toStrictEqual({
id: '8',
symbol: 'A-C',
name: 'A-C',
id: '10',
symbol: 'B-DFI',
name: 'B-Default Defi token',
status: true,
tokenA: {
id: '1',
id: '2',
reserve: '50',
blockCommission: '0'
},
tokenB: {
id: '3',
id: '0',
reserve: '300',
blockCommission: '0'
},
commission: '0',
totalLiquidity: '122.47448713',
totalLiquidity: {
token: '122.47448713',
usd: '1390.456752'
},
tradeEnabled: true,
ownerAddress: expect.any(String),
priceRatio: {
ab: '0.16666666',
ba: '6'
},
rewardPct: '0',
creation: {
tx: expect.any(String),
Expand All @@ -102,42 +131,43 @@ describe('list', () => {
})

it('should list with pagination', async () => {
const first = await client.poolpair.list(3)
expect(first.length).toStrictEqual(3)
const first = await client.poolpair.list(4)
expect(first.length).toStrictEqual(4)
expect(first.hasNext).toStrictEqual(true)
expect(first.nextToken).toStrictEqual('9')
expect(first.nextToken).toStrictEqual('12')

expect(first[0].symbol).toStrictEqual('A-B')
expect(first[1].symbol).toStrictEqual('A-C')
expect(first[2].symbol).toStrictEqual('A-D')
expect(first[0].symbol).toStrictEqual('A-DFI')
expect(first[1].symbol).toStrictEqual('B-DFI')
expect(first[2].symbol).toStrictEqual('C-DFI')
expect(first[3].symbol).toStrictEqual('D-DFI')

const next = await client.paginate(first)
expect(next.length).toStrictEqual(3)
expect(next.length).toStrictEqual(4)
expect(next.hasNext).toStrictEqual(true)
expect(next.nextToken).toStrictEqual('12')
expect(next.nextToken).toStrictEqual('16')

expect(next[0].symbol).toStrictEqual('A-E')
expect(next[1].symbol).toStrictEqual('A-F')
expect(next[2].symbol).toStrictEqual('B-C')
expect(next[0].symbol).toStrictEqual('E-DFI')
expect(next[1].symbol).toStrictEqual('F-DFI')
expect(next[2].symbol).toStrictEqual('G-DFI')
expect(next[3].symbol).toStrictEqual('H-DFI')

const last = await client.paginate(next)
expect(last.length).toStrictEqual(2)
expect(last.length).toStrictEqual(1)
expect(last.hasNext).toStrictEqual(false)
expect(last.nextToken).toBeUndefined()

expect(last[0].symbol).toStrictEqual('B-D')
expect(last[1].symbol).toStrictEqual('B-E')
expect(last[0].symbol).toStrictEqual('USDT-DFI')
})
})

describe('get', () => {
it('should get', async () => {
const response = await client.poolpair.get('7')
const response: PoolPairData = await client.poolpair.get('9')

expect(response).toStrictEqual({
id: '7',
symbol: 'A-B',
name: 'A-B',
id: '9',
symbol: 'A-DFI',
name: 'A-Default Defi token',
status: true,
tokenA: {
id: expect.any(String),
Expand All @@ -150,9 +180,16 @@ describe('get', () => {
blockCommission: '0'
},
commission: '0',
totalLiquidity: '141.42135623',
totalLiquidity: {
token: '141.42135623',
usd: '926.971168'
},
tradeEnabled: true,
ownerAddress: expect.any(String),
priceRatio: {
ab: '0.5',
ba: '2'
},
rewardPct: '0',
creation: {
tx: expect.any(String),
Expand All @@ -164,15 +201,15 @@ describe('get', () => {
it('should throw error as numeric string is expected', async () => {
expect.assertions(2)
try {
await client.poolpair.get('A-B')
await client.poolpair.get('A-DFI')
} catch (err) {
expect(err).toBeInstanceOf(WhaleApiException)
expect(err.error).toStrictEqual({
code: 400,
type: 'BadRequest',
at: expect.any(Number),
message: 'Validation failed (numeric string is expected)',
url: '/v0/regtest/poolpairs/A-B'
url: '/v0/regtest/poolpairs/A-DFI'
})
}
})
Expand Down
9 changes: 8 additions & 1 deletion packages/whale-api-client/src/api/poolpair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@ export interface PoolPairData {
reserve: string // BigNumber
blockCommission: string // BigNumber
}
priceRatio: {
ab: string // BigNumber
ba: string // BigNumber
}
commission: string // BigNumber
totalLiquidity: string // BigNumber
totalLiquidity: {
token: string // BigNumber
usd?: string // BigNumber
}
tradeEnabled: boolean
ownerAddress: string
rewardPct: string // BigNumber
Expand Down

0 comments on commit e8a1ffd

Please sign in to comment.