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 4f7e83e
Showing 1 changed file with 155 additions and 0 deletions.
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
})
})

0 comments on commit 4f7e83e

Please sign in to comment.