Skip to content

Commit

Permalink
Merge pull request #235 from metaDAOproject/feat/twap-price-improvements
Browse files Browse the repository at this point in the history
feat/twap price improvements
  • Loading branch information
LukasDeco authored Sep 7, 2024
2 parents a2d9d44 + cd0bdb8 commit c8eb970
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 12 deletions.
117 changes: 107 additions & 10 deletions lib/client/indexer/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { Observable } from "rxjs";
import {
ProposalMarketPricesAggregate,
SpotObservation,
TwapObservation
TwapObservation,
TwapOracleData
} from "@/types/prices";
import {
generateSubscriptionOp,
Expand Down Expand Up @@ -131,7 +132,7 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
twap_chart_data: {
__args: {
where: {
market_acct: { _eq: marketKey.toString() }
market_acct: { _eq: marketKey.toBase58() }
},
order_by: [
{
Expand Down Expand Up @@ -200,6 +201,102 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
});
}

watchCurrentTwapOracle(marketKey: PublicKey): Observable<TwapOracleData> {
const queryForGenerate = {
twaps: {
__args: {
where: {
market_acct: { _eq: marketKey.toBase58() }
},
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<TwapOracleData>((subscriber) => {
const subscriptionCleanup = this.graphqlWSClient.subscribe<{
twaps: {
last_observation: number;
last_price: number;
observation_agg: number;
updated_slot: number;
}[];
}>(
{ query, variables },
{
next: ({ data }) => {
const twapObservations = data?.twaps?.map<TwapOracleData>((d) => {
return {
lastObservation: d.last_observation,
lastPrice: d.last_price,
observationAgg: d.observation_agg,
updatedSlot: d.updated_slot
};
});
if (!twapObservations?.[0]) return;
subscriber.next(twapObservations[0]);
},
error: (error) => subscriber.error(error),
complete: () => subscriber.complete()
}
);

return () => subscriptionCleanup();
});
}

async fetchCurrentTwapOracle(
marketKey: PublicKey
): Promise<TwapOracleData | null> {
const query = {
twaps: {
__args: {
where: {
market_acct: { _eq: marketKey.toBase58() }
},
order_by: [
{
created_at: "desc" as order_by
}
],
limit: 1
},
last_observation: true,
last_price: true,
observation_agg: true,
updated_slot: true
}
};

try {
const { twaps } = await this.graphqlClient.query(query);
const latestTwap = twaps?.[0];

if (!latestTwap) return null;

return {
lastObservation: latestTwap.last_observation,
lastPrice: latestTwap.last_price,
observationAgg: latestTwap.observation_agg,
updatedSlot: latestTwap.updated_slot
};
} catch (error) {
console.error("Error fetching current TWAP oracle:", error);
throw error;
}
}

async fetchCurrentTwapPrice(
marketKey: PublicKey,
includeOracleData?: boolean
Expand All @@ -208,7 +305,7 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
twap_chart_data: {
__args: {
where: {
market_acct: { _eq: marketKey.toString() }
market_acct: { _eq: marketKey.toBase58() }
},
order_by: [
{
Expand All @@ -235,7 +332,7 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
query.twaps = {
__args: {
where: {
market_acct: { _eq: marketKey.toString() }
market_acct: { _eq: marketKey.toBase58() }
},
order_by: [
{
Expand Down Expand Up @@ -291,7 +388,7 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
}
],
where: {
market_acct: { _eq: marketKey.toString() }
market_acct: { _eq: marketKey.toBase58() }
}
},
token_amount: true,
Expand Down Expand Up @@ -363,7 +460,7 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
twap_chart_data: {
__args: {
where: {
market_acct: { _eq: marketKey.toString() },
market_acct: { _eq: marketKey.toBase58() },
interv
},
order_by: [
Expand Down Expand Up @@ -846,7 +943,7 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
prices_chart_data: {
__args: {
where: {
market_acct: { _eq: marketKey.toString() },
market_acct: { _eq: marketKey.toBase58() },
prices_type: {
_in: ["spot", "conditional"]
},
Expand Down Expand Up @@ -902,7 +999,7 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
prices_chart_data: {
__args: {
where: {
market_acct: { _eq: marketKey.toString() },
market_acct: { _eq: marketKey.toBase58() },
prices_type: {
_in: ["spot", "conditional"]
}
Expand Down Expand Up @@ -938,7 +1035,7 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
prices_chart_data: {
__args: {
where: {
market_acct: { _eq: marketKey.toString() },
market_acct: { _eq: marketKey.toBase58() },
prices_type: {
_in: ["spot", "conditional"]
},
Expand Down Expand Up @@ -977,7 +1074,7 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
}
],
where: {
market_acct: { _eq: marketKey.toString() },
market_acct: { _eq: marketKey.toBase58() },
prices_type: {
_in: ["spot", "conditional"]
}
Expand Down
11 changes: 9 additions & 2 deletions lib/transactions/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import {
SendTransactionOptions
} from "../types/transactions";
import { AnchorProvider } from "@coral-xyz/anchor";
import { Observable, Subscriber, shareReplay } from "rxjs";
import {
Observable,
Subscriber,
distinctUntilKeyChanged,
shareReplay
} from "rxjs";
import { Bundler } from "./bundles";

type SingleOrArray<T> = T | T[];
Expand Down Expand Up @@ -238,7 +243,9 @@ export class TransactionSender {
};
});
});
return obs.pipe(shareReplay());

// preventing duplicate status from coming through
return obs.pipe(shareReplay()).pipe(distinctUntilKeyChanged("status"));
}

private async handleSignatureResult(
Expand Down

0 comments on commit c8eb970

Please sign in to comment.