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

/anchors endpoint #389

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
"devDependencies": {
"@defichain/jellyfish-crypto": ">=0.44.0",
"@defichain/jellyfish-testing": ">=0.44.0",
"@defichain/testcontainers": ">=0.44.0",
"@defichain/testing": ">=0.44.0",
"@nestjs/cli": "^8.1.1",
Expand Down
208 changes: 208 additions & 0 deletions packages/whale-api-client/__tests__/api/anchor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import { GenesisKeys, MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { StubService } from '../stub.service'
import { StubWhaleApiClient } from '../stub.client'
import { WhaleApiClient } from '../../src'
import { TestingGroup } from '@defichain/jellyfish-testing'

let container: MasterNodeRegTestContainer
let service: StubService
let client: WhaleApiClient
let tGroup: TestingGroup

beforeAll(async () => {
tGroup = TestingGroup.create(3)
container = tGroup.group.get(0)
service = new StubService(container)
client = new StubWhaleApiClient(service)

await tGroup.group.start()
await service.start()
await setup()
})

async function setMockTime (offsetHour: number): Promise<void> {
await tGroup.exec(async (testing: any) => {
await testing.misc.offsetTimeHourly(offsetHour)
})
}

async function setup (): Promise<void> {
{
const auths = await tGroup.get(0).container.call('spv_listanchorauths')
expect(auths.length).toStrictEqual(0)
}

const initOffsetHour = -12
await setMockTime(initOffsetHour)

for (let i = 0; i < 15; i += 1) {
const { container } = tGroup.get(i % tGroup.length())
await container.generate(1)
await tGroup.waitForSync()
}

await tGroup.get(0).container.waitForAnchorTeams(tGroup.length())

for (let i = 0; i < tGroup.length(); i += 1) {
const { container } = tGroup.get(i % tGroup.length())
const team = await container.call('getanchorteams')
expect(team.auth.length).toStrictEqual(tGroup.length())
expect(team.confirm.length).toStrictEqual(tGroup.length())
expect(team.auth.includes(GenesisKeys[0].operator.address))
expect(team.auth.includes(GenesisKeys[1].operator.address))
expect(team.auth.includes(GenesisKeys[2].operator.address))
expect(team.confirm.includes(GenesisKeys[0].operator.address))
expect(team.confirm.includes(GenesisKeys[1].operator.address))
expect(team.confirm.includes(GenesisKeys[2].operator.address))
}

await tGroup.anchor.generateAnchorAuths(2, initOffsetHour)

await tGroup.get(0).container.waitForAnchorAuths(tGroup.length())

for (let i = 0; i < tGroup.length(); i += 1) {
const { container } = tGroup.get(i % tGroup.length())
const auths = await container.call('spv_listanchorauths')
expect(auths.length).toStrictEqual(2)
expect(auths[0].signers).toStrictEqual(tGroup.length())
}

await tGroup.get(0).container.call('spv_setlastheight', [1])
const anchor1 = await createAnchor()
await tGroup.get(0).generate(1)
await tGroup.waitForSync()

await tGroup.get(0).container.call('spv_setlastheight', [2])
const anchor2 = await createAnchor()
await tGroup.get(0).generate(1)
await tGroup.waitForSync()

await tGroup.get(0).container.call('spv_setlastheight', [3])
const anchor3 = await createAnchor()
await tGroup.get(0).generate(1)
await tGroup.waitForSync()

await tGroup.get(0).container.call('spv_setlastheight', [4])
const anchor4 = await createAnchor()
await tGroup.get(0).generate(1)
await tGroup.waitForSync()

await tGroup.get(1).container.call('spv_sendrawtx', [anchor1.txHex])
await tGroup.get(1).container.call('spv_sendrawtx', [anchor2.txHex])
await tGroup.get(1).container.call('spv_sendrawtx', [anchor3.txHex])
await tGroup.get(1).container.call('spv_sendrawtx', [anchor4.txHex])
await tGroup.get(1).generate(1)
await tGroup.waitForSync()

await tGroup.get(0).container.call('spv_setlastheight', [6])
}

async function createAnchor (): Promise<any> {
const rewardAddress = await tGroup.get(0).rpc.spv.getNewAddress()
return await tGroup.get(0).rpc.spv.createAnchor([{
txid: '11a276bb25585f6973a4dd68373cffff41dbcaddf12bbc1c2b489d1dc84564ee',
vout: 2,
amount: 15800,
privkey: 'b0528d87cfdb09f72c9d10b7b3cc00727062d93537a3e8abcf1fde821d08b59d'
}], rewardAddress)
}

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

describe('list', () => {
it('should list anchors', async () => {
const result = await client.anchors.list(4)
expect(result[0]).toStrictEqual({
id: '1',
btc: {
block: {
height: 1,
hash: '0000000000000001000000000000000100000000000000010000000000000001'
},
txn: {
hash: expect.any(String)
},
confirmations: 6
},
dfi: {
block: {
height: 30,
hash: expect.any(String)
}
},
previousAnchor: '0000000000000000000000000000000000000000000000000000000000000000',
rewardAddress: expect.any(String),
signatures: 2,
active: true,
anchorCreationHeight: 75
})
})

it('should test pagination', async () => {
const first = await client.anchors.list(2)

expect(first.length).toStrictEqual(2)
expect(first.hasNext).toStrictEqual(true)
expect(first.nextToken).toStrictEqual('3')

expect(first[0]).toStrictEqual({
id: '1',
btc: {
block: {
height: 1,
hash: '0000000000000001000000000000000100000000000000010000000000000001'
},
txn: {
hash: expect.any(String)
},
confirmations: 6
},
dfi: {
block: {
height: 30,
hash: expect.any(String)
}
},
previousAnchor: '0000000000000000000000000000000000000000000000000000000000000000',
rewardAddress: expect.any(String),
signatures: 2,
active: true,
anchorCreationHeight: 75
})

const last = await client.anchors.list(3, first.nextToken)
expect(last.length).toStrictEqual(2)
expect(last.nextToken).toStrictEqual(undefined)

expect(last[0]).toStrictEqual({
id: '3',
btc: {
block: {
height: 3,
hash: '0000000000000001000000000000000100000000000000010000000000000001'
},
txn: {
hash: expect.any(String)
},
confirmations: 4
},
dfi: {
block: {
height: 30,
hash: expect.any(String)
}
},
previousAnchor: '0000000000000000000000000000000000000000000000000000000000000000',
rewardAddress: expect.any(String),
signatures: 2,
active: false,
anchorCreationHeight: 75
})
})
})
46 changes: 46 additions & 0 deletions packages/whale-api-client/src/api/anchors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { WhaleApiClient } from '../whale.api.client'
import { ApiPagedResponse } from '../whale.api.response'

/**
* DeFi whale endpoint for anchors related services.
*/

export class Anchors {
constructor (private readonly client: WhaleApiClient) {}

/**
* Paginate query anchors.
*
* @param {number} size of anchors to query
* @param {string} next set of anchors
* @return {Promise<ApiPagedResponse<AnchorData>>}
*/
async list (size: number = 30, next?: string): Promise<ApiPagedResponse<AnchorData>> {
return await this.client.requestList('GET', 'anchors', size, next)
}
}

export interface AnchorData {
id: string /* ------------ ID is height of the btc block */
btc: {
block: {
height: number
hash: string
}
txn: {
hash: string
}
confirmations: number
}
dfi: {
block: {
height: number
hash: string
}
}
previousAnchor: string
rewardAddress: string
signatures: number
active?: boolean
anchorCreationHeight?: number
}
1 change: 1 addition & 0 deletions packages/whale-api-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * as prices from './api/prices'
export * as stats from './api/stats'
export * as rawtx from './api/rawtx'
export * as fee from './api/fee'
export * as anchors from './api/anchors'

export * from './whale.api.client'
export * from './whale.api.response'
Expand Down
3 changes: 2 additions & 1 deletion packages/whale-api-client/src/whale.api.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Prices } from './api/prices'
import { Stats } from './api/stats'
import { Rawtx } from './api/rawtx'
import { Fee } from './api/fee'
import { Anchors } from './api/anchors'

/**
* WhaleApiClient Options
Expand Down Expand Up @@ -74,7 +75,7 @@ export class WhaleApiClient {
public readonly stats = new Stats(this)
public readonly rawtx = new Rawtx(this)
public readonly fee = new Fee(this)

public readonly anchors = new Anchors(this)
constructor (
protected readonly options: WhaleApiClientOptions
) {
Expand Down
4 changes: 3 additions & 1 deletion src/module.api/_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { PriceController } from '@src/module.api/price.controller'
import { StatsController } from '@src/module.api/stats.controller'
import { FeeController } from '@src/module.api/fee.controller'
import { RawtxController } from '@src/module.api/rawtx.controller'
import { AnchorsController } from '@src/module.api/anchors.controller'

/**
* Exposed ApiModule for public interfacing
Expand All @@ -41,7 +42,8 @@ import { RawtxController } from '@src/module.api/rawtx.controller'
PriceController,
StatsController,
FeeController,
RawtxController
RawtxController,
AnchorsController
],
providers: [
{ provide: APP_PIPE, useClass: ApiValidationPipe },
Expand Down
Loading