Skip to content

Commit

Permalink
Fix: Twap and Price Bugs (#90)
Browse files Browse the repository at this point in the history
* fix: some bugs with prices and twaps

* chore: ver bump
  • Loading branch information
LukasDeco authored May 16, 2024
1 parent db94ef6 commit f207082
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 284 deletions.
18 changes: 18 additions & 0 deletions lib/client/indexer/__generated__/schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions lib/client/indexer/__generated__/schema.ts

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions lib/client/indexer/__generated__/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 57 additions & 9 deletions lib/client/indexer/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
} from "./__generated__";
import { Client as GQLWebSocketClient } from "graphql-ws";
import { FutarchyMarketsRPCClient } from "../rpc/markets";
import { PriceMath } from "@metadaoproject/futarchy-ts";
import { BN } from "@coral-xyz/anchor";

export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
public openbook: FutarchyIndexerOpenbookMarketsClient;
Expand Down Expand Up @@ -66,9 +68,18 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
{
created_at: "asc"
}
]
],
distinct_on: ["created_at"]
},
token_amount: true,
market: {
tokenByQuoteMintAcct: {
decimals: true
},
tokenByBaseMintAcct: {
decimals: true
}
},
updated_slot: true,
created_at: true
}
Expand All @@ -80,14 +91,28 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
token_amount: number;
updated_slot: number;
created_at: Date;
// TODO this query might be a bit slow... hasura warned about caching directive, we need to watch for this
market: {
tokenByQuoteMintAcct: {
decimals: number;
};
tokenByBaseMintAcct: {
decimals: number;
};
};
}[];
}>(
{ query, variables },
{
next: (data) => {
const twapObservations = data.data?.twaps?.map<TwapObservation>(
(d) => ({
price: d.token_amount,
priceUi: PriceMath.getHumanPrice(
new BN(d.token_amount),
d.market?.tokenByBaseMintAcct.decimals!!,
d.market?.tokenByQuoteMintAcct.decimals!!
),
priceRaw: d.token_amount,
slot: d.updated_slot,
createdAt: d.created_at
})
Expand Down Expand Up @@ -281,29 +306,52 @@ export class FutarchyIndexerMarketsClient implements FutarchyMarketsClient {
},
order_by: [
{
created_at: "desc"
created_at: "asc"
}
]
],
distinct_on: ["created_at"]
},
created_at: true,
price: true
price: true,
quote_amount: true,
base_amount: true,
market: {
tokenByQuoteMintAcct: {
decimals: true
},
tokenByBaseMintAcct: {
decimals: true
}
}
}
});

return new Observable((subscriber) => {
const subscriptionCleanup = this.graphqlWSClient.subscribe<{
takes: {
prices: {
created_at: Date;
price: number;
quote_amount: number;
base_amount: number;
market: {
tokenByQuoteMintAcct: {
decimals: number;
};
tokenByBaseMintAcct: {
decimals: number;
};
};
}[];
}>(
{ query, variables },
{
next: (data) => {
const spotObservations = data.data?.takes?.map<SpotObservation>(
const spotObservations = data.data?.prices?.map<SpotObservation>(
(d) => ({
price: d.price,
createdAt: d.created_at
priceUi: d.price,
createdAt: d.created_at,
quoteAmount: d.quote_amount,
baseAmount: d.base_amount
})
);
subscriber.next(spotObservations ?? []);
Expand Down
26 changes: 24 additions & 2 deletions lib/client/indexer/proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,18 @@ export class FutarchyIndexerProposalsClient implements FutarchyProposalsClient {
dao_acct: true,
treasury_acct: true,
tokenByBaseAcct: {
mint_acct: true
mint_acct: true,
image_url: true,
decimals: true,
symbol: true,
name: true
},
tokenByQuoteAcct: {
mint_acct: true
mint_acct: true,
image_url: true,
decimals: true,
symbol: true,
name: true
},
program: {
program_acct: true,
Expand Down Expand Up @@ -247,6 +255,20 @@ export class FutarchyIndexerProposalsClient implements FutarchyProposalsClient {
tokenMint: d.tokenByBaseAcct
? new PublicKey(d.tokenByBaseAcct.mint_acct)
: undefined
},
baseToken: {
decimals: d.tokenByBaseAcct?.decimals ?? 0,
name: d.tokenByBaseAcct?.name ?? "",
symbol: d.tokenByBaseAcct?.symbol ?? "",
url: d.tokenByBaseAcct?.image_url ?? "",
publicKey: d.tokenByBaseAcct?.mint_acct ?? ""
},
quoteToken: {
decimals: d.tokenByQuoteAcct?.decimals ?? 0,
name: d.tokenByQuoteAcct?.name ?? "",
symbol: d.tokenByQuoteAcct?.symbol ?? "",
url: d.tokenByQuoteAcct?.image_url ?? "",
publicKey: d.tokenByQuoteAcct?.mint_acct ?? ""
}
},
participants: [],
Expand Down
Loading

0 comments on commit f207082

Please sign in to comment.