From ef8f5d7f23bde20f3e8f010f5e63c7f324a69272 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Mon, 9 May 2022 16:13:11 +0800 Subject: [PATCH] api url rename and store addr as script --- packages/whale-api-client/src/api/address.ts | 2 +- src/module.api/address.controller.ts | 11 ++++++++--- .../model/dftx/set.future.swap.spec.ts | 17 +++++++++++++---- .../model/dftx/set.future.swap.ts | 4 ++-- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/packages/whale-api-client/src/api/address.ts b/packages/whale-api-client/src/api/address.ts index 0cc6ec96..9467e10b 100644 --- a/packages/whale-api-client/src/api/address.ts +++ b/packages/whale-api-client/src/api/address.ts @@ -112,7 +112,7 @@ export class Address { * @return {Promise>} */ async listFutureSwap (address: string, height: number, size: number = 30, next?: string): Promise> { - return await this.client.requestList('GET', `address/${address}/future-swaps/${height}`, size, next) + return await this.client.requestList('GET', `address/${address}/future/swaps/${height}`, size, next) } } diff --git a/src/module.api/address.controller.ts b/src/module.api/address.controller.ts index 268df88c..8f11d8a4 100644 --- a/src/module.api/address.controller.ts +++ b/src/module.api/address.controller.ts @@ -10,7 +10,7 @@ import { ScriptActivity, ScriptActivityMapper } from '@src/module.model/script.a import { ScriptAggregation, ScriptAggregationMapper } from '@src/module.model/script.aggregation' import { ScriptUnspent, ScriptUnspentMapper } from '@src/module.model/script.unspent' import { FutureSwapMapper } from '@src/module.model/future.swap' -import { DeFiAddress } from '@defichain/jellyfish-address' +import { DeFiAddress, fromAddress } from '@defichain/jellyfish-address' import { NetworkName } from '@defichain/jellyfish-network' import { HexEncoder } from '@src/module.model/_hex.encoder' import { toBuffer } from '@defichain/jellyfish-transaction/dist/script/_buffer' @@ -19,6 +19,8 @@ import { LoanVaultService } from '@src/module.api/loan.vault.service' import { parseDisplaySymbol } from '@src/module.api/token.controller' import { AccountHistory } from '@defichain/jellyfish-api-core/dist/category/account' +/* eslint-disable @typescript-eslint/no-non-null-assertion */ + @Controller('/address/:address') export class AddressController { constructor ( @@ -189,7 +191,7 @@ export class AddressController { }) } - @Get('/future-swaps/:height') + @Get('/future/swaps/:height') async listFutureSwap ( @Param('address') address: string, @Param('height', ParseIntPipe) height: number, @@ -201,7 +203,10 @@ export class AddressController { const lt = query.next ?? `${HexEncoder.encodeHeight(nextSettleBlock)}-${'f'.repeat(64)}` const gt = `${HexEncoder.encodeHeight(height)}-${'0'.repeat(64)}` - const list = await this.futureSwapMapper.query(address, size, lt, gt) + const script = fromAddress(address, this.network)!.script + const haddr = toBuffer(script.stack).toString('hex') + + const list = await this.futureSwapMapper.query(haddr, size, lt, gt) return ApiPagedResponse.of(list, size, item => { return item.sort diff --git a/src/module.indexer/model/dftx/set.future.swap.spec.ts b/src/module.indexer/model/dftx/set.future.swap.spec.ts index af32af77..05eed2d4 100644 --- a/src/module.indexer/model/dftx/set.future.swap.spec.ts +++ b/src/module.indexer/model/dftx/set.future.swap.spec.ts @@ -5,6 +5,8 @@ import { FutureSwap } from '@defichain/jellyfish-api-core/dist/category/account' import BigNumber from 'bignumber.js' import { Testing } from '@defichain/jellyfish-testing' import { FutureSwapMapper } from '@src/module.model/future.swap' +import { fromAddress } from '@defichain/jellyfish-address' +import { toBuffer } from '@defichain/jellyfish-transaction/dist/script/_buffer' const testing = Testing.create(new MasterNodeRegTestContainer()) let app: NestFastifyApplication @@ -200,11 +202,12 @@ it('should index future swap', async () => { const futureSwapMapper = app.get(FutureSwapMapper) { - const res = await futureSwapMapper.query(fromAddr, 30) + const hex = addressToHex(fromAddr) + const res = await futureSwapMapper.query(hex, 30) expect(res.length).toStrictEqual(7) expect(res[3]).toStrictEqual({ id: expect.any(String), - key: fromAddr, + key: hex, sort: expect.any(String), source: { token: 3, amount: '5.67751' @@ -216,11 +219,12 @@ it('should index future swap', async () => { } { - const res = await futureSwapMapper.query(fromAddr1, 30) + const hex = addressToHex(fromAddr1) + const res = await futureSwapMapper.query(hex, 30) expect(res.length).toStrictEqual(4) expect(res[0]).toStrictEqual({ id: expect.any(String), - key: fromAddr1, + key: hex, sort: expect.any(String), source: { token: 2, amount: '5.78' @@ -231,3 +235,8 @@ it('should index future swap', async () => { }) } }) + +function addressToHex (addr: string): string { + const script = fromAddress(addr, 'regtest')!.script + return toBuffer(script.stack).toString('hex') +} diff --git a/src/module.indexer/model/dftx/set.future.swap.ts b/src/module.indexer/model/dftx/set.future.swap.ts index 41b1dca5..49f9a133 100644 --- a/src/module.indexer/model/dftx/set.future.swap.ts +++ b/src/module.indexer/model/dftx/set.future.swap.ts @@ -5,7 +5,7 @@ import { Inject, Injectable, Logger } from '@nestjs/common' import { HexEncoder } from '@src/module.model/_hex.encoder' import { FutureSwapMapper } from '@src/module.model/future.swap' import { NetworkName } from '@defichain/jellyfish-network' -import { fromScript } from '@defichain/jellyfish-address' +import { toBuffer } from '@defichain/jellyfish-transaction/dist/script/_buffer' /* eslint-disable @typescript-eslint/no-non-null-assertion */ @@ -27,7 +27,7 @@ export class SetFutureSwapIndexer extends DfTxIndexer { await this.futureSwapMapper.put({ id: txid, - key: fromScript(data.owner, this.network)!.address, + key: toBuffer(data.owner.stack).toString('hex'), sort: `${HexEncoder.encodeHeight(block.height)}-${txid}`, source: { token: data.source.token,