Skip to content

Commit

Permalink
add prices.active.defid.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Nov 19, 2024
1 parent 5846c36 commit 683e75f
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ describe('getAccountHistory', () => {
try {
await controller.getAccountHistory(addr, -1, 1)
} catch (err: any) {
console.log('err1: ', err)
expect(err.error).toStrictEqual({
at: expect.any(Number),
code: 400,
Expand Down
155 changes: 155 additions & 0 deletions apps/whale-api/src/module.api/__defid__/prices.active.defid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { JsonRpcClient } from '@defichain/jellyfish-api-jsonrpc'
import { DPriceController, DefidBin, DefidRpc } from '../../e2e.defid.module'

let container: DefidRpc
let app: DefidBin
let controller: DPriceController
let client: JsonRpcClient

beforeAll(async () => {
app = new DefidBin()
await app.start()
controller = app.ocean.priceController
container = app.rpc
await app.waitForWalletCoinbaseMaturity()
client = new JsonRpcClient(app.rpcUrl)

const height = await app.getBlockCount()
await container.generate(1)
await app.waitForBlockHeight(height)
})

afterAll(async () => {
await app.stop()
})

const now = Math.floor(Date.now() / 1000)

it('should get active price with 2 active oracles (exact values)', async () => {
const address = await app.getNewAddress()
const oracles = []

for (let i = 0; i < 2; i++) {
oracles.push(await client.oracle.appointOracle(address, [
{ token: 'S1', currency: 'USD' }
], {
weightage: 1
}))
await container.generate(1)
}

{
const height = await app.getBlockCount()
await container.generate(1)
await app.waitForBlockHeight(height)
}

await app.generate(1)
const { data: beforeActivePrice } = await controller.getFeedActive('S1-USD')
expect(beforeActivePrice.length).toStrictEqual(0)

const oneMinute = 60
const timestamp = now
for (let i = 0; i < oracles.length; i++) {
await client.oracle.setOracleData(oracles[i], timestamp + i * oneMinute, {
prices: [
{ tokenAmount: '10.0@S1', currency: 'USD' }
]
})
}
await app.generate(1)

await client.loan.setLoanToken({
symbol: 'S1',
fixedIntervalPriceId: 'S1/USD'
})
await app.generate(1)

for (let i = 0; i <= 6; i++) {
const mockTime = now + i * oneMinute
await client.misc.setMockTime(mockTime)
const price = i > 3 ? '12.0' : '10.0'
console.log('price: ', price)
for (const oracle of oracles) {
await client.oracle.setOracleData(oracle, mockTime, {
prices: [
{ tokenAmount: `${price}@S1`, currency: 'USD' }
]
})
}
await app.generate(1)
}

{
const height = await app.getBlockCount()
await app.generate(1)
await app.waitForBlockHeight(height)
}

const { data: activePrice } = await controller.getFeedActive('S1-USD')
expect(activePrice[0]).toStrictEqual({
block: {
hash: expect.any(String),
height: expect.any(Number),
medianTime: expect.any(Number),
time: expect.any(Number)
},
id: expect.any(String),
key: 'S1-USD',
active: {
amount: '10.00000000',
oracles: {
active: 2,
total: 2
},
weightage: 2
},
next: {
amount: '12.00000000',
oracles: {
active: 2,
total: 2
},
weightage: 2
},
sort: expect.any(String),
isLive: true
})

{
await app.generate(1)
const height = await app.getBlockCount()
await app.generate(1)
await app.waitForBlockHeight(height)
}

const { data: nextActivePrice } = await controller.getFeedActive('S1-USD')
expect(nextActivePrice[0]).toStrictEqual({
active: {
amount: '10.00000000',
oracles: {
active: 2,
total: 2
},
weightage: 2
},
block: {
hash: expect.any(String),
height: expect.any(Number),
medianTime: expect.any(Number),
time: expect.any(Number)
},
id: expect.any(String),
key: 'S1-USD',
next: {
amount: '12.00000000',
oracles: {
active: 2,
total: 2
},
weightage: 2
},
sort: expect.any(String),
isLive: true
})
})
3 changes: 1 addition & 2 deletions apps/whale-api/src/module.api/__defid__/prices.defid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,5 @@ it('should get oracles', async () => {
})
})

// NOTE(canonbrother):
// `getFeedActive` and `getFeedWithInterval` are skipped on origin test suite due to flaky
// NOTE(canonbrother): `getFeedWithInterval` are skipped on origin test suite due to flaky
// to do while needed

0 comments on commit 683e75f

Please sign in to comment.