Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: gql wasm #280

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: "",
}
40 changes: 40 additions & 0 deletions src/indexer-nibi/heart-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
GQLStatsFields,
communityPoolQueryString,
delegationsQueryString,
QueryWasmArgs,
GqlWasmFields,
} from "./query"
import {
defaultDelegations,
Expand Down Expand Up @@ -43,6 +45,7 @@ import {
defaultTvl,
defaultUnbondings,
defaultUser,
defaultUserContract,
defaultUsers,
defaultValidator,
defaultVolume,
Expand Down Expand Up @@ -999,6 +1002,43 @@ test("stats", async () => {
)
})

const testWasm = async (args: QueryWasmArgs, fields?: GqlWasmFields) => {
const resp = await heartMonitor.wasm(args, fields)
expect(resp).toHaveProperty("wasm")

if (resp.wasm) {
const { wasm } = resp

checkFields([wasm], ["userContracts"])
}
}

test("wasm", async () => {
await testWasm({
userContracts: {
limit: 1,
},
})

await testWasm(
{
userContracts: {
limit: 1,
},
},
{
userContracts: defaultUserContract,
}
)

await testWasm(
{},
{
userContracts: defaultUserContract,
}
)
})

const testUnbondings = async (
args: GQLQueryGqlUnbondingsArgs,
fields?: GQLUnbonding
Expand Down
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 = (
ruslan-sh-r marked this conversation as resolved.
Show resolved Hide resolved
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")}
}
`
ruslan-sh-r marked this conversation as resolved.
Show resolved Hide resolved
}

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