From 21209b553f6429871274137a0db14367a5b41e91 Mon Sep 17 00:00:00 2001 From: Kollan House Date: Sun, 4 Aug 2024 13:23:47 -0600 Subject: [PATCH] feat: token fetch --- lib/client/client.ts | 7 +++- lib/client/indexer/indexerClient.ts | 6 +++ lib/client/indexer/tokens.ts | 64 +++++++++++++++++++++++++++++ lib/types/autocrats.ts | 19 +++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 lib/client/indexer/tokens.ts diff --git a/lib/client/client.ts b/lib/client/client.ts index a40bafb..ff961dd 100644 --- a/lib/client/client.ts +++ b/lib/client/client.ts @@ -20,7 +20,8 @@ import { ProposalAccounts, ProgramVersionLabel, ProposalWithFullData, - Dao + Dao, + TokenCache } from "@/types"; import { SwapType } from "@metadaoproject/futarchy"; import { Observable } from "rxjs"; @@ -220,3 +221,7 @@ export interface CreateProposal { proposalDetails: ProposalDetails ): void; } + +export interface FutarchyTokenClient { + fetchFutarchyTokens(): Promise; +} \ No newline at end of file diff --git a/lib/client/indexer/indexerClient.ts b/lib/client/indexer/indexerClient.ts index 891d047..be94068 100644 --- a/lib/client/indexer/indexerClient.ts +++ b/lib/client/indexer/indexerClient.ts @@ -8,6 +8,7 @@ import { ClientOptions, createClient as createWsClient } from "graphql-ws"; import { FutarchyProtocol } from "@/types"; import { Observable } from "rxjs"; import { FutarchyIndexerUserClient } from "./user"; +import { FutarchyIndexerTokenClient } from "./tokens"; export class FutarchyIndexerClient implements FutarchyClient { public daos: FutarchyIndexerDaoClient; @@ -15,6 +16,7 @@ export class FutarchyIndexerClient implements FutarchyClient { public balances: FutarchyIndexerBalancesClient; public markets: FutarchyIndexerMarketsClient; public user: FutarchyIndexerUserClient; + public tokens: FutarchyIndexerTokenClient; private protocolMap: Map; private wsClient; @@ -75,6 +77,10 @@ export class FutarchyIndexerClient implements FutarchyClient { graphqlClient, this.protocolMap, ) + this.tokens = new FutarchyIndexerTokenClient( + rpcClient.daos, + graphqlClient + ) } watchSlot() { diff --git a/lib/client/indexer/tokens.ts b/lib/client/indexer/tokens.ts new file mode 100644 index 0000000..c35fabe --- /dev/null +++ b/lib/client/indexer/tokens.ts @@ -0,0 +1,64 @@ +import { + Tokens__GQL, + TokenCache +} from "@/types"; +import { FutarchyTokenClient } from "@/client"; +import { FutarchyRPCDaoClient } from "../rpc"; +import { + Client as IndexerGraphQLClient, +} from "./__generated__"; + +export class FutarchyIndexerTokenClient implements FutarchyTokenClient { + private rpcDaoClient: FutarchyRPCDaoClient; + private graphqlClient: IndexerGraphQLClient; + constructor( + rpcDaoClient: FutarchyRPCDaoClient, + graphqlClient: IndexerGraphQLClient, + ) { + this.rpcDaoClient = rpcDaoClient; + this.graphqlClient = graphqlClient; + } + async fetchFutarchyTokens(): Promise { + try { + + const { tokens } = await this.graphqlClient.query?.({ + tokens: { + __args: { + where: {} + }, + // pass arguments to the query + decimals: true, + symbol: true, + mint_acct: true, + name: true, + image_url: true, + supply: true, + updated_at: true, + } + }); + + const tokenDetails = this.getTokenCacheFromQuery(tokens) ?? [] as TokenCache[] + + return tokenDetails; + } catch (e) { + console.error(e); + //return this.rpcDaoClient.fetchFutarchyTokens(); + return [] + } + } + + private getTokenCacheFromQuery( + tokens: Tokens__GQL + ): TokenCache[] { + return tokens.map((t) => { + return { + symbol: t.symbol, + decimals: t.decimals, + name: t.name ?? null, + mint_acct: t.mint_acct, + url: t.image_url, + supply: t.supply ?? null + } as TokenCache + }) as TokenCache[] + }; +} diff --git a/lib/types/autocrats.ts b/lib/types/autocrats.ts index c276c38..8182284 100644 --- a/lib/types/autocrats.ts +++ b/lib/types/autocrats.ts @@ -82,6 +82,16 @@ export type Dao = { }; }; +// Tokens Cache +export type TokenCache = { + symbol: string; + decimals: number; + name: string | null; + mint_acct: string; + url: string; + supply: number | null; +} + // we might want to consider changing this to be DaoDetails as the main component and export type DaoAggregate = { name: string; @@ -142,3 +152,12 @@ export type DaoDetails__GQL = { }; }>; }; + +export type Tokens__GQL = { + symbol: string; + decimals: number; + name: string | null; + mint_acct: string; + image_url: string | null; + supply: number | null; +}[]