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

Commit

Permalink
rm height params, use block_period govvar
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed May 18, 2022
1 parent dc6cdc1 commit f53b8b9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,37 +209,38 @@ afterAll(async () => {
})

it('should listFutureSwap', async () => {
const list = await client.address.listFutureSwap(fromAddr, 1, 30)
const list = await client.address.listFutureSwap(fromAddr, 30)

{
const first = await client.address.listFutureSwap(fromAddr, 1, 3)
const first = await client.address.listFutureSwap(fromAddr, 3)
expect(first.length).toStrictEqual(3)
expect(first.hasNext).toStrictEqual(true)
expect(first[0].id).toStrictEqual(list[0].id)
expect(first[1].id).toStrictEqual(list[1].id)
expect(first[2].id).toStrictEqual(list[2].id)

const next = await client.address.listFutureSwap(fromAddr, 1, 3, first.nextToken)
const next = await client.address.listFutureSwap(fromAddr, 3, first.nextToken)
expect(next.length).toStrictEqual(3)
expect(next.hasNext).toStrictEqual(true)
expect(next[0].id).toStrictEqual(list[3].id)
expect(next[1].id).toStrictEqual(list[4].id)
expect(next[2].id).toStrictEqual(list[5].id)

const last = await client.address.listFutureSwap(fromAddr, 1, 3, next.nextToken)
const last = await client.address.listFutureSwap(fromAddr, 3, next.nextToken)
expect(last.length).toStrictEqual(1)
expect(last.hasNext).toStrictEqual(false)
expect(last[0].id).toStrictEqual(list[6].id)
}

{
const res = await client.address.listFutureSwap(fromAddr1, 1, 30)
const res = await client.address.listFutureSwap(fromAddr1, 30)
expect(res.length).toStrictEqual(4)
expect(res.hasNext).toStrictEqual(false)
}
})

it('should get empty as out of range', async () => {
const empty = await client.address.listFutureSwap(fromAddr, 999, 30)
it('should get empty as non-existent', async () => {
const addr = await testing.generateAddress()
const empty = await client.address.listFutureSwap(addr, 30)
expect(empty.length).toStrictEqual(0)
})
5 changes: 2 additions & 3 deletions packages/whale-api-client/src/api/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,12 @@ export class Address {
* List future swap
*
* @param {string} address to list future swap
* @param {number} height to set range of future swap
* @param {string} [size=30] to query
* @param {string} [next] token for next slice of FutureSwap
* @return {Promise<ApiPagedResponse<FutureSwap>>}
*/
async listFutureSwap (address: string, height: number, size: number = 30, next?: string): Promise<ApiPagedResponse<FutureSwap>> {
return await this.client.requestList('GET', `address/${address}/future/swaps/${height}`, size, next)
async listFutureSwap (address: string, size: number = 30, next?: string): Promise<ApiPagedResponse<FutureSwap>> {
return await this.client.requestList('GET', `address/${address}/future/swaps`, size, next)
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/module.api/address.controller.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1155,12 +1155,12 @@ describe('listFutureSwap', () => {
})

it('should listFutureSwap', async () => {
const list = await controller.listFutureSwap(fromAddr, 1, {
const list = await controller.listFutureSwap(fromAddr, {
size: 30
})

{
const first = await controller.listFutureSwap(fromAddr, 1, {
const first = await controller.listFutureSwap(fromAddr, {
size: 3
})
expect(first.data.length).toStrictEqual(3)
Expand All @@ -1169,7 +1169,7 @@ describe('listFutureSwap', () => {
expect(first.data[1].id).toStrictEqual(list.data[1].id)
expect(first.data[2].id).toStrictEqual(list.data[2].id)

const next = await controller.listFutureSwap(fromAddr, 1, {
const next = await controller.listFutureSwap(fromAddr, {
size: 3,
next: first.page?.next
})
Expand All @@ -1179,7 +1179,7 @@ describe('listFutureSwap', () => {
expect(next.data[1].id).toStrictEqual(list.data[4].id)
expect(next.data[2].id).toStrictEqual(list.data[5].id)

const last = await controller.listFutureSwap(fromAddr, 1, {
const last = await controller.listFutureSwap(fromAddr, {
size: 3,
next: next.page?.next
})
Expand All @@ -1189,16 +1189,17 @@ describe('listFutureSwap', () => {
}

{
const res = await controller.listFutureSwap(fromAddr1, 1, {
const res = await controller.listFutureSwap(fromAddr1, {
size: 30
})
expect(res.data.length).toStrictEqual(4)
expect(res.page?.next).toBeUndefined()
}
})

it('should get empty as out of range', async () => {
const empty = await controller.listFutureSwap(fromAddr, 999, {
it('should get empty as non-existent', async () => {
const addr = await testing.generateAddress()
const empty = await controller.listFutureSwap(addr, {
size: 30
})
expect(empty.data.length).toStrictEqual(0)
Expand Down
12 changes: 7 additions & 5 deletions src/module.api/address.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,19 @@ export class AddressController {
})
}

@Get('/future/swaps/:height')
@Get('/future/swaps')
async listFutureSwap (
@Param('address') address: string,
@Param('height', ParseIntPipe) height: number,
@Query() query: PaginationQuery
): Promise<ApiPagedResponse<FutureSwap>> {
const size = query.size > 30 ? 30 : query.size
const nextSettleBlock = await this.rpcClient.oracle.getFutureSwapBlock()
const end = await this.rpcClient.oracle.getFutureSwapBlock() // next settle block height
// mapper is sorted DESC
const lt = query.next ?? `${HexEncoder.encodeHeight(nextSettleBlock)}-${'f'.repeat(64)}`
const gt = `${HexEncoder.encodeHeight(height)}-${'0'.repeat(64)}`
const lt = query.next ?? `${HexEncoder.encodeHeight(end)}-${'f'.repeat(64)}`
const attributes = await this.rpcClient.masternode.getGov('ATTRIBUTES')
const interval = attributes.ATTRIBUTES['v0/params/dfip2203/block_period']
const start = end - interval
const gt = `${HexEncoder.encodeHeight(start)}-${'0'.repeat(64)}`

const script = fromAddress(address, this.network)!.script
const haddr = toBuffer(script.stack).toString('hex')
Expand Down

0 comments on commit f53b8b9

Please sign in to comment.