Skip to content

Commit

Permalink
fix(add): oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
cgilbe27 committed Oct 2, 2023
1 parent 01ca15c commit 1794ff8
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/indexer-nibi/src/defaultObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
GovVote,
Governance,
MarkPriceCandle,
OracleEntry,
OraclePrice,
PerpLeaderboard,
PerpMarket,
PerpPosition,
Expand Down Expand Up @@ -340,3 +342,16 @@ export const defaultUnbondings: Unbonding = {
delegator: defaultUser,
validator: defaultValidator,
}

export const defaultOraclePrice: OraclePrice = {
block: defaultBlock,
eventSeqNo: 0,
pair: "",
price: 0,
txSeqNo: 0,
}

export const defaultOracleEntry: OracleEntry = {
numVotes: 0,
validator: defaultValidator,
}
70 changes: 69 additions & 1 deletion packages/indexer-nibi/src/gql/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,64 @@ export enum MarkPriceCandlesOrder {
PeriodStartTs = "period_start_ts",
}

export type Oracle = {
readonly __typename?: "Oracle"
readonly oraclePrices: ReadonlyArray<OraclePrice>
readonly oracles: ReadonlyArray<OracleEntry>
}

export type OracleOraclePricesArgs = {
limit?: InputMaybe<Scalars["Int"]["input"]>
offset?: InputMaybe<Scalars["Int"]["input"]>
order_by?: InputMaybe<OraclePricesOrder>
order_desc?: InputMaybe<Scalars["Boolean"]["input"]>
where?: InputMaybe<OraclePricesFilter>
}

export type OracleOraclesArgs = {
limit?: InputMaybe<Scalars["Int"]["input"]>
order_by?: InputMaybe<OraclesOrder>
order_desc?: InputMaybe<Scalars["Boolean"]["input"]>
where?: InputMaybe<OraclesFilter>
}

export type OracleEntry = {
readonly __typename?: "OracleEntry"
readonly numVotes: Scalars["Int"]["output"]
readonly validator: Validator
}

export type OraclePrice = {
readonly __typename?: "OraclePrice"
readonly block: Block
readonly eventSeqNo: Scalars["Int"]["output"]
readonly pair: Scalars["String"]["output"]
readonly price: Scalars["Float"]["output"]
readonly txSeqNo: Scalars["Int"]["output"]
}

export type OraclePricesFilter = {
readonly block?: InputMaybe<IntFilter>
readonly blockTs?: InputMaybe<TimeFilter>
readonly pair?: InputMaybe<StringFilter>
}

export enum OraclePricesOrder {
Pair = "pair",
Price = "price",
Sequence = "sequence",
}

export type OraclesFilter = {
readonly numVotes?: InputMaybe<IntFilter>
readonly validatorAddressEq?: InputMaybe<Scalars["String"]["input"]>
}

export enum OraclesOrder {
NumVotes = "num_votes",
ValidatorAddress = "validator_address",
}

export type PeriodFilter = {
readonly periodEq?: InputMaybe<Scalars["Int"]["input"]>
readonly periodGt?: InputMaybe<Scalars["Int"]["input"]>
Expand Down Expand Up @@ -337,7 +395,7 @@ export type PerpMarket = {
readonly maintenance_margin_ratio: Scalars["Float"]["output"]
readonly mark_price: Scalars["Float"]["output"]
readonly mark_price_twap: Scalars["Float"]["output"]
readonly max_funding_rate: Scalars["Float"]["output"]
readonly max_funding_rate?: Maybe<Scalars["Float"]["output"]>
readonly max_leverage: Scalars["Float"]["output"]
readonly pair: Scalars["String"]["output"]
readonly partial_liquidation_ratio: Scalars["Float"]["output"]
Expand Down Expand Up @@ -444,6 +502,7 @@ export type Query = {
readonly distributionCommissions: ReadonlyArray<DistributionCommission>
readonly governance: Governance
readonly markPriceCandles: ReadonlyArray<MarkPriceCandle>
readonly oracle: Oracle
readonly perp: Perp
/** @deprecated Moved to perp sub schema */
readonly perpLeaderboard: ReadonlyArray<PerpLeaderboard>
Expand Down Expand Up @@ -980,6 +1039,10 @@ export type StringFilter = {
readonly like?: InputMaybe<Scalars["String"]["input"]>
}

export type SubOraclePricesFilter = {
readonly pair: Scalars["String"]["input"]
}

export type SubPerpMarketFilter = {
readonly pair: Scalars["String"]["input"]
}
Expand All @@ -992,6 +1055,7 @@ export type SubPerpPositionFilter = {
export type Subscription = {
readonly __typename?: "Subscription"
readonly markPriceCandles: ReadonlyArray<MarkPriceCandle>
readonly oraclePrices: ReadonlyArray<OraclePrice>
readonly perpMarket: PerpMarket
readonly perpPositions: ReadonlyArray<PerpPosition>
}
Expand All @@ -1001,6 +1065,10 @@ export type SubscriptionMarkPriceCandlesArgs = {
where?: InputMaybe<MarkPriceCandlesFilter>
}

export type SubscriptionOraclePricesArgs = {
where?: InputMaybe<SubOraclePricesFilter>
}

export type SubscriptionPerpMarketArgs = {
where: SubPerpMarketFilter
}
Expand Down
59 changes: 58 additions & 1 deletion packages/indexer-nibi/src/gql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,57 @@ enum MarkPriceCandlesOrder {
period_start_ts
}

type Oracle {
oraclePrices(
limit: Int
offset: Int
order_by: OraclePricesOrder
order_desc: Boolean
where: OraclePricesFilter
): [OraclePrice!]!
oracles(
limit: Int
order_by: OraclesOrder
order_desc: Boolean
where: OraclesFilter
): [OracleEntry!]!
}

type OracleEntry {
numVotes: Int!
validator: Validator!
}

type OraclePrice {
block: Block!
eventSeqNo: Int!
pair: String!
price: Float!
txSeqNo: Int!
}

input OraclePricesFilter {
block: IntFilter
blockTs: TimeFilter
pair: StringFilter
}

enum OraclePricesOrder {
pair
price
sequence
}

input OraclesFilter {
numVotes: IntFilter
validatorAddressEq: String
}

enum OraclesOrder {
num_votes
validator_address
}

input PeriodFilter {
periodEq: Int
periodGt: Int
Expand Down Expand Up @@ -279,7 +330,7 @@ type PerpMarket {
maintenance_margin_ratio: Float!
mark_price: Float!
mark_price_twap: Float!
max_funding_rate: Float!
max_funding_rate: Float
max_leverage: Float!
pair: String!
partial_liquidation_ratio: Float!
Expand Down Expand Up @@ -403,6 +454,7 @@ type Query {
order_desc: Boolean
where: MarkPriceCandlesFilter
): [MarkPriceCandle!]!
oracle: Oracle!
perp: Perp!
perpLeaderboard(
limit: Int
Expand Down Expand Up @@ -843,6 +895,10 @@ input StringFilter {
like: String
}

input SubOraclePricesFilter {
pair: String!
}

input SubPerpMarketFilter {
pair: String!
}
Expand All @@ -857,6 +913,7 @@ type Subscription {
limit: Int
where: MarkPriceCandlesFilter
): [MarkPriceCandle!]!
oraclePrices(where: SubOraclePricesFilter): [OraclePrice!]!
perpMarket(where: SubPerpMarketFilter!): PerpMarket!
perpPositions(where: SubPerpPositionFilter!): [PerpPosition!]!
}
Expand Down
51 changes: 51 additions & 0 deletions packages/indexer-nibi/src/heart-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { cleanResponse, gqlEndptFromTmRpc } from "./gql"
import { communityPoolQueryString, delegationsQueryString } from "./query"
import {
defaultMarkPriceCandles,
defaultOraclePrice,
defaultPerpMarket,
defaultPerpPosition,
} from "./defaultObjects"
Expand Down Expand Up @@ -204,6 +205,56 @@ test("markPriceCandlesSubscription", async () => {
}
})

test("oracle", async () => {
const resp = await heartMonitor.oracle({
oraclePrices: {
limit: 1,
},
oracles: {
limit: 1,
},
})
expect(resp).toHaveProperty("oracle")

if (resp.oracle) {
const { oracle } = resp
const fields = ["oraclePrices", "oracles"]
fields.forEach((field: string) => {
expect(oracle).toHaveProperty(field)
})
}
})

test("oraclePricesSubscription", async () => {
const hm = {
oraclePricesSubscription: jest.fn().mockResolvedValue({
next: async () => ({
value: {
data: {
oraclePrices: [defaultOraclePrice],
},
},
}),
}),
}

const resp = await hm.oraclePricesSubscription({
where: { pair: "ubtc:unusd" },
})

const event = await resp.next()

expect(event.value.data).toHaveProperty("oraclePrices")

if ((event.value.data.oraclePrices.length ?? 0) > 0) {
const [oraclePrices] = event.value.data.oraclePrices ?? []
const fields = ["block", "eventSeqNo", "pair", "price", "txSeqNo"]
fields.forEach((field: string) => {
expect(oraclePrices).toHaveProperty(field)
})
}
})

test("perp", async () => {
const resp = await heartMonitor.perp({
leaderboard: {
Expand Down
26 changes: 26 additions & 0 deletions packages/indexer-nibi/src/heart-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DistributionCommission,
Governance,
MarkPriceCandle,
OraclePrice,
PerpMarket,
PerpPosition,
QueryCommunityPoolArgs,
Expand All @@ -30,6 +31,7 @@ import {
SpotPoolJoined,
SpotPoolSwap,
SubscriptionMarkPriceCandlesArgs,
SubscriptionOraclePricesArgs,
SubscriptionPerpMarketArgs,
SubscriptionPerpPositionsArgs,
Token,
Expand Down Expand Up @@ -77,12 +79,18 @@ import {
governance,
GqlOutMarkPriceCandles,
markPriceCandles,
QueryOracleArgs,
OracleFields,
GqlOutOracle,
oracle,
} from "./query"
import {
markPriceCandlesSubscription,
GqlOutPerpMarket,
perpMarketSubscription,
perpPositionsSubscription,
oraclePricesSubscription,
GqlOutOraclePrices,
} from "./subscription"
import { queryBatchHandler } from "./batchHandlers/queryBatchHandler"

Expand Down Expand Up @@ -119,6 +127,16 @@ export interface IHeartMonitor {
fields?: Partial<MarkPriceCandle>
) => Promise<AsyncIterableIterator<ExecutionResult<GqlOutMarkPriceCandles>>>

readonly oracle: (
args: QueryOracleArgs,
fields?: OracleFields
) => Promise<GqlOutOracle>

readonly oraclePricesSubscription: (
args: SubscriptionOraclePricesArgs,
fields?: Partial<OraclePrice>
) => Promise<AsyncIterableIterator<ExecutionResult<GqlOutOraclePrices>>>

readonly perp: (
args: QueryPerpArgs,
fields?: PerpFields
Expand Down Expand Up @@ -244,6 +262,14 @@ export class HeartMonitor implements IHeartMonitor {
fields?: Partial<MarkPriceCandle>
) => markPriceCandlesSubscription(args, this.subscriptionClient, fields)

oracle = async (args: QueryOracleArgs, fields?: OracleFields) =>
oracle(args, this.gqlEndpt, fields)

oraclePricesSubscription = async (
args: SubscriptionOraclePricesArgs,
fields?: Partial<OraclePrice>
) => oraclePricesSubscription(args, this.subscriptionClient, fields)

perp = async (args: QueryPerpArgs, fields?: PerpFields) =>
perp(args, this.gqlEndpt, fields)

Expand Down
1 change: 1 addition & 0 deletions packages/indexer-nibi/src/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from "./stats"
export * from "./unbondings"
export * from "./users"
export * from "./validators"
export * from "./oracle"
Loading

0 comments on commit 1794ff8

Please sign in to comment.