Skip to content

Commit

Permalink
feat: sdk compatibility and twap oracle data
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasDeco committed Aug 29, 2024
1 parent 718f73c commit 14f4a02
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 61 deletions.
2 changes: 1 addition & 1 deletion lib/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./client";
export * from "./client.js";
export * from "./rpc/rpcClient";
export * from "./indexer/indexerClient";
142 changes: 112 additions & 30 deletions lib/client/indexer/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import {
generateSubscriptionOp,
Client as IndexerGraphQLClient,
order_by,
orders_bool_exp,
orders_order_by,
orders_select_column,
Expand Down Expand Up @@ -134,16 +135,19 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
return market;
}

watchCurrentTwapPrice(marketKey: PublicKey): Observable<TwapObservation[]> {
const { query, variables } = generateSubscriptionOp({
watchCurrentTwapPrice(
marketKey: PublicKey,
includeOracleData?: boolean
): Observable<TwapObservation[]> {
const queryForGenerate = {
twap_chart_data: {
__args: {
where: {
market_acct: { _eq: marketKey.toString() }
},
order_by: [
{
interv: "desc"
interv: "desc" as order_by
}
],
limit: 1
Expand All @@ -158,8 +162,31 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
}
},
interv: true
}
});
},
twaps: undefined as any
};

if (includeOracleData) {
queryForGenerate.twaps = {
__args: {
where: {
market_acct: { _eq: marketKey.toString() }
},
order_by: [
{
created_at: "desc" as order_by
}
],
limit: 1
},
last_observation: true,
last_price: true,
observation_agg: true,
updated_slot: true
};
}

const { query, variables } = generateSubscriptionOp(queryForGenerate);

return new Observable((subscriber) => {
const subscriptionCleanup = this.graphqlWSClient.subscribe<{
Expand All @@ -177,20 +204,38 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
};
};
}[];
twaps: {
last_observation: number;
last_price: number;
observation_agg: number;
updated_slot: number;
}[];
}>(
{ query, variables },
{
next: (data) => {
next: ({ data }) => {
const twaps = data?.twaps;
const twapObservations =
data.data?.twap_chart_data?.map<TwapObservation>((d) => ({
priceUi: PriceMath.getHumanPrice(
new BN(d.token_amount),
d.market?.tokenByBaseMintAcct.decimals!!,
d.market?.tokenByQuoteMintAcct.decimals!!
),
priceRaw: d.token_amount,
createdAt: new Date(d.interv)
}));
data?.twap_chart_data?.map<TwapObservation>((d, i) => {
const latestTwaps = twaps?.[i];
return {
priceUi: PriceMath.getHumanPrice(
new BN(d.token_amount),
d.market?.tokenByBaseMintAcct?.decimals!!,
d.market?.tokenByQuoteMintAcct.decimals!!
),
priceRaw: d.token_amount,
createdAt: new Date(d.interv),
oracleData: latestTwaps
? {
lastObservation: latestTwaps.last_observation,
lastPrice: latestTwaps.last_price,
observationAgg: latestTwaps.observation_agg,
updatedSlot: latestTwaps.updated_slot
}
: null
};
});
subscriber.next(twapObservations);
},
error: (error) => subscriber.error(error),
Expand All @@ -203,17 +248,18 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
}

async fetchCurrentTwapPrice(
marketKey: PublicKey
marketKey: PublicKey,
includeOracleData?: boolean
): Promise<TwapObservation[]> {
const { twap_chart_data } = await this.graphqlClient.query({
const query = {
twap_chart_data: {
__args: {
where: {
market_acct: { _eq: marketKey.toString() }
},
order_by: [
{
interv: "desc"
interv: "desc" as order_by
}
],
limit: 1
Expand All @@ -228,19 +274,55 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
}
},
interv: true
}
});
},
twaps: undefined as any
};

const twapObservations = twap_chart_data?.map<TwapObservation>((d) => ({
priceUi: PriceMath.getHumanPrice(
new BN(d.token_amount),
d.market?.tokenByBaseMintAcct?.decimals!!,
d.market?.tokenByQuoteMintAcct.decimals!!
),
priceRaw: d.token_amount,
createdAt: new Date(d.interv)
}));
return twapObservations;
if (includeOracleData) {
query.twaps = {
__args: {
where: {
market_acct: { _eq: marketKey.toString() }
},
order_by: [
{
created_at: "desc" as order_by
}
],
limit: 1
},
last_observation: true,
last_price: true,
observation_agg: true,
updated_slot: true
};
}

const { twap_chart_data, twaps } = await this.graphqlClient.query(query);
const latestTwapChartData = twap_chart_data?.[0];
const latestTwaps = twaps?.[0];

if (!latestTwapChartData) return [];

return [
{
priceUi: PriceMath.getHumanPrice(
new BN(latestTwapChartData.token_amount),
latestTwapChartData.market?.tokenByBaseMintAcct?.decimals!!,
latestTwapChartData.market?.tokenByQuoteMintAcct.decimals!!
),
priceRaw: latestTwapChartData.token_amount,
createdAt: new Date(latestTwapChartData.interv),
oracleData: latestTwaps
? {
lastObservation: latestTwaps.last_observation,
lastPrice: latestTwaps.last_price,
observationAgg: latestTwaps.observation_agg,
updatedSlot: latestTwaps.updated_slot
}
: null
}
];
}

watchTwapPrices(marketKey: PublicKey): Observable<TwapObservation[]> {
Expand Down
8 changes: 4 additions & 4 deletions lib/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export * from "./reactions"
import { PublicKey } from "@solana/web3.js";
import { BN } from "@coral-xyz/anchor";
import { AutocratProgram, ProgramVersion } from "@/types";
import AUTOCRAT_V0_IDL from "@/idl/autocrat_v0.json";
import AUTOCRAT_V0_1_IDL from "@/idl/autocrat_v0.1.json";
import AUTOCRAT_V0_2_IDL from "@/idl/autocrat_v0.2.json";
import AUTOCRAT_V0_3_IDL from "@/idl/autocrat_v0.3.json";
import AUTOCRAT_V0_IDL from "@/idl/autocrat_v0.json" assert {type: "json"};
import AUTOCRAT_V0_1_IDL from "@/idl/autocrat_v0.1.json" assert {type: "json"};
import AUTOCRAT_V0_2_IDL from "@/idl/autocrat_v0.2.json" assert {type: "json"};
import AUTOCRAT_V0_3_IDL from "@/idl/autocrat_v0.3.json" assert {type: "json"};

export const SYSTEM_PROGRAM: PublicKey = new PublicKey(
"11111111111111111111111111111111"
Expand Down
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./client";
export * from "./constants";
export * from "./client/index.js";
export * from "./constants/index.js";
export * from "./openbookTwap";
export * from "./nonce";
export * from "./transactions/";
Expand Down
8 changes: 8 additions & 0 deletions lib/types/prices.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
export type TwapObservation = {
priceRaw?: number | null;
priceUi: number | null;
oracleData?: TwapOracleData | null;
createdAt: Date;
};

export type TwapOracleData = {
lastObservation: number;
lastPrice: number;
observationAgg: number;
updatedSlot: number;
};

/**
* Quote and Base amounts generally available on market spot prices
*/
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@metadaoproject/futarchy-sdk",
"version": "4.0.0-alpha.47",
"version": "4.0.0-alpha.48",
"type": "module",
"main": "dist",
"scripts": {
"preinstall": "npx only-allow pnpm",
"build": "tsc --project tsconfig.build.json && tscpaths -p tsconfig.json -s ./lib -o ./dist",
"build": "tsc --project tsconfig.build.json && tscpaths -p tsconfig.json -s ./lib -o ./dist && tsc-alias -p tsconfig.build.json",
"build:tests": "tsc && tscpaths -p tsconfig.json -s ./tests -o ./dist"
},
"keywords": [
Expand Down Expand Up @@ -36,7 +37,8 @@
"dayjs": "^1.11.11",
"graphql-ws": "^5.16.0",
"numeral": "^2.0.6",
"rxjs": "^7.8.1"
"rxjs": "^7.8.1",
"tsc-alias": "^1.8.10"
},
"devDependencies": {
"semantic-release": "^24.0.0",
Expand Down
Loading

0 comments on commit 14f4a02

Please sign in to comment.