Skip to content

Commit

Permalink
feat: gql wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslan-sh-r committed Jan 18, 2024
1 parent 323334e commit eb582f8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/indexer-nibi/defaultObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
GQLToken,
GQLUnbonding,
GQLUser,
GQLUserContract,
GQLValidator,
GQLValidatorStatus,
} from "./gql/generated"
Expand Down Expand Up @@ -182,6 +183,7 @@ export const defaultMarkPriceCandles: GQLMarkPriceCandle = {
volumeNotional: 0,
pair: "",
period: 0,
periodInterval: "",
periodStartTs: "",
indexPriceTwapClose: 0,
}
Expand Down Expand Up @@ -267,6 +269,7 @@ export const defaultStatsFees: GQLStatsFees = {
feesTotal: 0,
feesTotalCumulative: 0,
period: 0,
periodInterval: "",
periodStartTs: "",
}

Expand All @@ -275,6 +278,7 @@ export const defaultPerpOpenInterest: GQLStatsPerpOpenInterest = {
openInterestShort: 0,
openInterestTotal: 0,
period: 0,
periodInterval: "",
periodStartTs: "",
}

Expand All @@ -284,13 +288,15 @@ export const defaultPerpPnl: GQLStatsPerpPnl = {
netPnl: 0,
netPnlCumulative: 0,
period: 0,
periodInterval: "",
periodStartTs: "",
profit: 0,
profitCumulative: 0,
}

export const defaultTotals: GQLStatsTotals = {
period: 0,
periodInterval: "",
periodStartTs: "",
totalPerp: 0,
totalFeesPerp: 0,
Expand All @@ -303,6 +309,7 @@ export const defaultTotals: GQLStatsTotals = {

export const defaultTvl: GQLStatsTvl = {
period: 0,
periodInterval: "",
periodStartTs: "",
tvlPerp: 0,
tvlStablecoin: 0,
Expand All @@ -321,6 +328,7 @@ export const defaultUsers: GQLStatsUsers = {
newUsersTotal: 0,
newUsersTotalCumulative: 0,
period: 0,
periodInterval: "",
periodStartTs: "",
userActionsPerp: 0,
uniqueUsersLp: 0,
Expand All @@ -342,6 +350,7 @@ export const defaultVolume: GQLStatsVolume = {
volumeTotal: 0,
volumeTotalCumulative: 0,
period: 0,
periodInterval: "",
periodStartTs: "",
}

Expand Down Expand Up @@ -396,3 +405,8 @@ export const defaultIbcChannelsResponse: GQLIbcChannelsResponse = {
channels: [defaultIbcChannel],
revision_height: 0,
}

export const defaultUserContract: GQLUserContract = {
user: defaultUser,
contractAddress: "",
}
10 changes: 10 additions & 0 deletions src/indexer-nibi/heart-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
GQLUnbonding,
GQLUser,
GQLValidator,
GQLWasm,
} from "./gql/generated"
import {
GqlOutCommunityPool,
Expand Down Expand Up @@ -97,6 +98,7 @@ import {
GqlOutPerpPositions,
} from "./subscription"
import { queryBatchHandler } from "./batchHandlers/queryBatchHandler"
import { GqlOutWasm, GqlWasmFields, QueryWasmArgs, wasm } from "./query/wasm"

/** IHeartMonitor is an interface for a Heart Monitor GraphQL API.
* Each of its methods corresponds to a GQLQueryGql function. */
Expand Down Expand Up @@ -226,6 +228,11 @@ export interface IHeartMonitor {
args: GQLQueryGqlValidatorsArgs,
fields?: Partial<GQLValidator>
) => Promise<GqlOutValidators>

readonly wasm: (
args: QueryWasmArgs,
fields?: GqlWasmFields
) => Promise<GqlOutWasm>
}

/** HeartMonitor is an API for "Heart Monitor" that indexes the Nibiru blockchain
Expand Down Expand Up @@ -362,4 +369,7 @@ export class HeartMonitor implements IHeartMonitor {
args: GQLQueryGqlValidatorsArgs,
fields?: Partial<GQLValidator>
) => validators(args, this.gqlEndpt, fields)

wasm = async (args: QueryWasmArgs, fields?: GqlWasmFields) =>
wasm(args, this.gqlEndpt, fields)
}
1 change: 1 addition & 0 deletions src/indexer-nibi/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from "./stats"
export * from "./unbondings"
export * from "./users"
export * from "./validators"
export * from "./wasm"
68 changes: 68 additions & 0 deletions src/indexer-nibi/query/wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { defaultUserContract } from "../defaultObjects"
import { convertObjectToPropertiesString, doGqlQuery, gqlQuery } from "../gql"
import {
GQLQuery,
GQLUserContract,
GQLWasmGqlUserContractsArgs,
} from "../gql/generated"

export type QueryWasmArgs = {
userContracts?: GQLWasmGqlUserContractsArgs
}

export interface GqlOutWasm {
wasm?: GQLQuery["wasm"]
}

export type GqlWasmFields = Partial<{
userContracts?: Partial<GQLUserContract>
}>

export const wasmQueryString = (
args: QueryWasmArgs,
fields?: GqlWasmFields
) => {
const oracleQuery: string[] = []

if (fields?.userContracts) {
oracleQuery.push(
gqlQuery(
"userContracts",
args.userContracts ?? {},
convertObjectToPropertiesString(fields.userContracts),
true
)
)
}

// Default Objects

if (args.userContracts && !fields?.userContracts) {
oracleQuery.push(
gqlQuery(
"userContracts",
args.userContracts,
convertObjectToPropertiesString(defaultUserContract),
true
)
)
}

return `
wasm {
${oracleQuery.join("\n")}
}
`
}

export const wasm = async (
args: QueryWasmArgs,
endpt: string,
fields?: GqlWasmFields
): Promise<GqlOutWasm> =>
doGqlQuery(
`{
${wasmQueryString(args, fields)}
}`,
endpt
)

0 comments on commit eb582f8

Please sign in to comment.