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

Commit

Permalink
api url rename and store addr as script
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed May 9, 2022
1 parent 1a61b6a commit ef8f5d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/whale-api-client/src/api/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class Address {
* @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)
return await this.client.requestList('GET', `address/${address}/future/swaps/${height}`, size, next)
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/module.api/address.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 (
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
17 changes: 13 additions & 4 deletions src/module.indexer/model/dftx/set.future.swap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand All @@ -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')
}
4 changes: 2 additions & 2 deletions src/module.indexer/model/dftx/set.future.swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -27,7 +27,7 @@ export class SetFutureSwapIndexer extends DfTxIndexer<SetFutureSwap> {

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,
Expand Down

0 comments on commit ef8f5d7

Please sign in to comment.