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

feat(controller): anchors API service #719

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
157 changes: 157 additions & 0 deletions packages/whale-api-client/__tests__/api/anchors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { RegTestFoundationKeys } from '@defichain/jellyfish-network'
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(RegTestFoundationKeys[0].operator.address))
expect(team.auth.includes(RegTestFoundationKeys[1].operator.address))
expect(team.auth.includes(RegTestFoundationKeys[2].operator.address))
expect(team.confirm.includes(RegTestFoundationKeys[0].operator.address))
expect(team.confirm.includes(RegTestFoundationKeys[1].operator.address))
expect(team.confirm.includes(RegTestFoundationKeys[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()
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: 90
})
})

// it('should test pagination', async () => {
// const firstRequest = await client.anchors.list(2)
//
// expect(firstRequest.length).toStrictEqual(2)
// expect(firstRequest.hasNext).toStrictEqual(true)
//
// const lastRequest = await client.paginate(firstRequest)

// })
})
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 (startBtcHeight)
* @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 @@ -13,6 +13,7 @@ export * as stats from './api/stats'
export * as rawtx from './api/rawtx'
export * as fee from './api/fee'
export * as loan from './api/loan'
export * as anchors from './api/anchors'

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

/**
* WhaleApiClient Options
Expand Down Expand Up @@ -76,6 +77,7 @@ export class WhaleApiClient {
public readonly rawtx = new Rawtx(this)
public readonly fee = new Fee(this)
public readonly loan = new Loan(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 @@ -24,6 +24,7 @@ import { FeeController } from '@src/module.api/fee.controller'
import { RawtxController } from '@src/module.api/rawtx.controller'
import { LoanController } from '@src/module.api/loan.controller'
import { LoanVaultService } from '@src/module.api/loan.vault.service'
import { AnchorsController } from '@src/module.api/anchors.controller'

/**
* Exposed ApiModule for public interfacing
Expand All @@ -44,7 +45,8 @@ import { LoanVaultService } from '@src/module.api/loan.vault.service'
StatsController,
FeeController,
RawtxController,
LoanController
LoanController,
AnchorsController
],
providers: [
{ provide: APP_PIPE, useClass: ApiValidationPipe },
Expand Down
Loading