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

Commit

Permalink
feat(indexer): deprecated SetOracleDataIntervalIndexer Indexer & API (#…
Browse files Browse the repository at this point in the history
…749)

* feat(indexer): deprecated SetOracleDataIntervalIndexer Indexer & API

* updated deprecation notice with PR url

* fix invalid import
  • Loading branch information
fuxingloh authored Feb 3, 2022
1 parent 9a1b5dc commit c04ff9b
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 470 deletions.
89 changes: 0 additions & 89 deletions packages/whale-api-client/__tests__/api/prices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { StubService } from '../stub.service'
import { WhaleApiClient } from '../../src'
import { StubWhaleApiClient } from '../stub.client'
import { JsonRpcClient } from '@defichain/jellyfish-api-jsonrpc'
import { PriceFeedTimeInterval } from '@whale-api-client/api/prices'
import { Testing } from '@defichain/jellyfish-testing'

describe('oracles', () => {
Expand Down Expand Up @@ -239,94 +238,6 @@ describe('oracles', () => {
})
})

describe('pricefeed with interval', () => {
const container = new MasterNodeRegTestContainer()
const service = new StubService(container)
const apiClient = new StubWhaleApiClient(service)
let client: JsonRpcClient

beforeAll(async () => {
await container.start()
await container.waitForWalletCoinbaseMaturity()
await service.start()

client = new JsonRpcClient(await container.getCachedRpcUrl())
})

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

it('should get interval', async () => {
const address = await container.getNewAddress()
const oracleId = await client.oracle.appointOracle(address, [
{ token: 'S1', currency: 'USD' }
], {
weightage: 1
})
await container.generate(1)

const oneMinute = 60
const timeNow = Math.floor(new Date().getTime() / 1000)
for (let i = 0; i < 60; i++) {
const mockTime = timeNow + i * oneMinute
const price = (i + 1).toFixed(2)
await client.oracle.setOracleData(oracleId, timeNow + 5 * 60 - 1, {
prices: [
{ tokenAmount: `${price}@S1`, currency: 'USD' }
]
})
await client.misc.setMockTime(mockTime)
await container.generate(1)
}

{
const height = await container.getBlockCount()
await container.generate(1)
await service.waitForIndexedHeight(height)
}

const noInterval = await apiClient.prices.getFeed('S1', 'USD', 60)
expect(noInterval.length).toStrictEqual(60)

const interval5Minutes = await apiClient.prices.getFeedWithInterval('S1', 'USD', PriceFeedTimeInterval.FIVE_MINUTES, 60)
expect(interval5Minutes.length).toStrictEqual(11)
expect(interval5Minutes.map(x => x.aggregated.amount)).toStrictEqual(
[
'58.50000000',
'53.50000000',
'47.50000000',
'41.50000000',
'35.50000000',
'29.50000000',
'23.50000000',
'17.50000000',
'11.50000000',
'5.00000000',
'1.00000000'
]
)

const interval10Minutes = await apiClient.prices.getFeedWithInterval('S1', 'USD', PriceFeedTimeInterval.TEN_MINUTES, 60)
expect(interval10Minutes.length).toStrictEqual(7)
expect(interval10Minutes.map(x => x.aggregated.amount)).toStrictEqual(
[
'59.00000000',
'52.00000000',
'41.00000000',
'30.00000000',
'19.00000000',
'7.50000000',
'1.00000000'
]
)
})
})

describe('active price', () => {
const container = new MasterNodeRegTestContainer()
const testing = Testing.create(container)
Expand Down
41 changes: 0 additions & 41 deletions packages/whale-api-client/src/api/prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@ import { WhaleApiClient } from '../whale.api.client'
import { ApiPagedResponse } from '../whale.api.response'
import { OraclePriceFeed } from './oracles'

/**
* Time interval for graphing
*/
export enum PriceFeedTimeInterval {
FIVE_MINUTES = 5 * 60,
TEN_MINUTES = 10 * 60,
ONE_HOUR = 60 * 60,
ONE_DAY = 24 * 60 * 60
}

/**
* DeFi whale endpoint for price related services.
*/
Expand Down Expand Up @@ -61,11 +51,6 @@ export class Prices {
return await this.client.requestList('GET', `prices/${key}/feed`, size, next)
}

async getFeedWithInterval (token: string, currency: string, interval: PriceFeedTimeInterval, size: number = 30, next?: string): Promise<ApiPagedResponse<PriceFeedInterval>> {
const key = `${token}-${currency}`
return await this.client.requestList('GET', `prices/${key}/feed/interval/${interval}`, size, next)
}

/**
* Get a list of Oracles
*
Expand Down Expand Up @@ -112,32 +97,6 @@ export interface PriceFeed {
}
}

export interface PriceFeedInterval {
id: string
key: string
sort: string

token: string
currency: string

aggregated: {
amount: string
weightage: number
count: number
oracles: {
active: number
total: number
}
}

block: {
hash: string
height: number
time: number
medianTime: number
}
}

export interface PriceOracle {
id: string
key: string
Expand Down
2 changes: 1 addition & 1 deletion src/module.api/_core/api.paged.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class ApiPagedResponse<T> implements ApiResponse {
data: T[]
page?: ApiPage

private constructor (data: T[], next?: string) {
protected constructor (data: T[], next?: string) {
this.data = data
this.page = next !== undefined ? { next } : undefined
}
Expand Down
31 changes: 19 additions & 12 deletions src/module.api/price.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { PriceTicker, PriceTickerMapper } from '@src/module.model/price.ticker'
import { PriceOracle } from '@whale-api-client/api/prices'
import { OraclePriceFeedMapper } from '@src/module.model/oracle.price.feed'
import { OraclePriceActive, OraclePriceActiveMapper } from '@src/module.model/oracle.price.active'
import {
OraclePriceAggregatedInterval,
OraclePriceAggregatedIntervalMapper
} from '@src/module.model/oracle.price.aggregated.interval'
import { ApiError } from '@src/module.api/_core/api.error'

@Controller('/prices')
export class PriceController {
Expand All @@ -19,8 +16,7 @@ export class PriceController {
protected readonly oracleTokenCurrencyMapper: OracleTokenCurrencyMapper,
protected readonly priceTickerMapper: PriceTickerMapper,
protected readonly priceFeedMapper: OraclePriceFeedMapper,
protected readonly oraclePriceActiveMapper: OraclePriceActiveMapper,
protected readonly oraclePriceAggregatedIntervalMapper: OraclePriceAggregatedIntervalMapper
protected readonly oraclePriceActiveMapper: OraclePriceActiveMapper
) {
}

Expand Down Expand Up @@ -68,12 +64,8 @@ export class PriceController {
@Param('key') key: string,
@Param('interval', ParseIntPipe) interval: number,
@Query() query: PaginationQuery
): Promise<ApiPagedResponse<OraclePriceAggregatedInterval>> {
const priceKey = `${key}-${interval}`
const items = await this.oraclePriceAggregatedIntervalMapper.query(priceKey, query.size, query.next)
return ApiPagedResponse.of(items, query.size, item => {
return item.sort
})
): Promise<ApiPagedResponse<any>> {
return new DeprecatedIntervalApiPagedResponse()
}

@Get('/:key/oracles')
Expand All @@ -94,3 +86,18 @@ export class PriceController {
})
}
}

class DeprecatedIntervalApiPagedResponse<T> extends ApiPagedResponse<T> {
error?: ApiError

constructor () {
super([])
this.error = {
at: Date.now(),
code: 410,
type: 'Gone',
message: 'Oracle feed interval data has been deprecated with immediate effect. See https://github.com/DeFiCh/whale/pull/749 for more information.',
url: '/:key/feed/interval/:interval'
}
}
}
3 changes: 0 additions & 3 deletions src/module.indexer/model/dftx.indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { AppointOracleIndexer } from '@src/module.indexer/model/dftx/appoint.ora
import { RemoveOracleIndexer } from '@src/module.indexer/model/dftx/remove.oracle'
import { UpdateOracleIndexer } from '@src/module.indexer/model/dftx/update.oracle'
import { SetOracleDataIndexer } from '@src/module.indexer/model/dftx/set.oracle.data'
import { SetOracleDataIntervalIndexer } from '@src/module.indexer/model/dftx/set.oracle.data.interval'
import { CreateMasternodeIndexer } from '@src/module.indexer/model/dftx/create.masternode'
import { ResignMasternodeIndexer } from '@src/module.indexer/model/dftx/resign.masternode'
import { Injectable, Logger } from '@nestjs/common'
Expand All @@ -24,7 +23,6 @@ export class MainDfTxIndexer extends Indexer {
private readonly removeOracle: RemoveOracleIndexer,
private readonly updateOracle: UpdateOracleIndexer,
private readonly setOracleData: SetOracleDataIndexer,
private readonly setOracleDataInterval: SetOracleDataIntervalIndexer,
private readonly createMasternode: CreateMasternodeIndexer,
private readonly resignMasternode: ResignMasternodeIndexer,
private readonly activePriceIndexer: ActivePriceIndexer,
Expand All @@ -38,7 +36,6 @@ export class MainDfTxIndexer extends Indexer {
setOracleData,
createMasternode,
resignMasternode,
setOracleDataInterval,
activePriceIndexer,
placeAuctionBidIndexer
]
Expand Down
2 changes: 0 additions & 2 deletions src/module.indexer/model/dftx/_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AppointOracleIndexer } from '@src/module.indexer/model/dftx/appoint.ora
import { RemoveOracleIndexer } from '@src/module.indexer/model/dftx/remove.oracle'
import { UpdateOracleIndexer } from '@src/module.indexer/model/dftx/update.oracle'
import { SetOracleDataIndexer } from '@src/module.indexer/model/dftx/set.oracle.data'
import { SetOracleDataIntervalIndexer } from '@src/module.indexer/model/dftx/set.oracle.data.interval'
import { CreateMasternodeIndexer } from '@src/module.indexer/model/dftx/create.masternode'
import { ResignMasternodeIndexer } from '@src/module.indexer/model/dftx/resign.masternode'
import { NetworkName } from '@defichain/jellyfish-network'
Expand All @@ -18,7 +17,6 @@ const indexers = [
UpdateOracleIndexer,
CreateMasternodeIndexer,
ResignMasternodeIndexer,
SetOracleDataIntervalIndexer,
ActivePriceIndexer,
PlaceAuctionBidIndexer
]
Expand Down
85 changes: 0 additions & 85 deletions src/module.indexer/model/dftx/oracle.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import { NestFastifyApplication } from '@nestjs/platform-fastify'
import { createTestingApp, invalidateFromHeight, stopTestingApp, waitForIndexedHeight } from '@src/e2e.module'
import { OraclePriceFeedMapper } from '@src/module.model/oracle.price.feed'
import { OraclePriceAggregatedMapper } from '@src/module.model/oracle.price.aggregated'
import {
OracleIntervalSeconds,
OraclePriceAggregatedIntervalMapper
} from '@src/module.model/oracle.price.aggregated.interval'

describe('invalidate appoint/remove/update oracle', () => {
const container = new MasterNodeRegTestContainer()
Expand Down Expand Up @@ -370,84 +366,3 @@ describe('invalidate set oracle data', () => {
}
})
})

describe('interval set oracle data', () => {
const container = new MasterNodeRegTestContainer()
let app: NestFastifyApplication
let client: JsonRpcClient

beforeAll(async () => {
await container.start()
await container.waitForWalletCoinbaseMaturity()

app = await createTestingApp(container)
client = new JsonRpcClient(await container.getCachedRpcUrl())
})

afterAll(async () => {
await stopTestingApp(container, app)
})

it('should get interval', async () => {
const address = await container.getNewAddress()
const oracleId = await client.oracle.appointOracle(address, [
{ token: 'S1', currency: 'USD' }
], {
weightage: 1
})
await container.generate(1)

const oneMinute = 60
const timeNow = Math.floor(new Date().getTime() / 1000)
for (let i = 0; i < 60; i++) {
const mockTime = timeNow + i * oneMinute
const price = (i + 1).toFixed(2)
await client.oracle.setOracleData(oracleId, timeNow + 5 * 60 - 1, {
prices: [
{ tokenAmount: `${price}@S1`, currency: 'USD' }
]
})
await client.misc.setMockTime(mockTime)
await container.generate(1)
}

const height = await container.getBlockCount()
await container.generate(1)
await waitForIndexedHeight(app, height)

const noInterval = await app.get(OraclePriceAggregatedMapper).query('S1-USD', Number.MAX_SAFE_INTEGER)
expect(noInterval.length).toStrictEqual(60)

const interval5Minutes = await app.get(OraclePriceAggregatedIntervalMapper).query(`S1-USD-${OracleIntervalSeconds.FIVE_MINUTES}`, Number.MAX_SAFE_INTEGER)
expect(interval5Minutes.length).toStrictEqual(11)
expect(interval5Minutes.map(x => x.aggregated.amount)).toStrictEqual(
[
'58.50000000',
'53.50000000',
'47.50000000',
'41.50000000',
'35.50000000',
'29.50000000',
'23.50000000',
'17.50000000',
'11.50000000',
'5.00000000',
'1.00000000'
]
)

const interval10Minutes = await app.get(OraclePriceAggregatedIntervalMapper).query(`S1-USD-${OracleIntervalSeconds.TEN_MINUTES}`, Number.MAX_SAFE_INTEGER)
expect(interval10Minutes.length).toStrictEqual(7)
expect(interval10Minutes.map(x => x.aggregated.amount)).toStrictEqual(
[
'59.00000000',
'52.00000000',
'41.00000000',
'30.00000000',
'19.00000000',
'7.50000000',
'1.00000000'
]
)
})
})
Loading

0 comments on commit c04ff9b

Please sign in to comment.