diff --git a/packages/indexer/src/v3_indexer/indexers/amm-market/utils.ts b/packages/indexer/src/v3_indexer/indexers/amm-market/utils.ts index ee3ee9b..36d537c 100644 --- a/packages/indexer/src/v3_indexer/indexers/amm-market/utils.ts +++ b/packages/indexer/src/v3_indexer/indexers/amm-market/utils.ts @@ -1,7 +1,8 @@ import { BN } from "@coral-xyz/anchor"; -import { BN_0, enrichTokenMetadata } from "@metadaoproject/futarchy-sdk"; +import { enrichTokenMetadata } from "@metadaoproject/futarchy-sdk"; import { PriceMath } from "@metadaoproject/futarchy/v0.4"; import { schema, usingDb, eq, inArray } from "@metadaoproject/indexer-db"; +import { TokenRecord } from "@metadaoproject/indexer-db/lib/schema"; import { PricesType } from "@metadaoproject/indexer-db/lib/schema"; import { TwapRecord, @@ -12,6 +13,8 @@ import { provider, rpcReadClient } from "../../connection"; import { Err, Ok, Result, TaggedUnion } from "../../match"; import { logger } from "../../../logger"; import { getHumanPrice } from "../../usecases/math"; +import { getMint } from "@solana/spl-token"; +import { connection } from "../../../connection"; export enum AmmMarketAccountIndexingErrors { AmmTwapIndexError = "AmmTwapIndexError", @@ -55,11 +58,47 @@ export async function indexAmmMarketAccountWithContext( ammMarketAccount.quoteMint, provider ) + + // get token mints from rpc (needed for fetching supply) + const baseMintPubKey = new PublicKey(baseToken.publicKey ?? ""); + const baseTokenMint = await getMint( + connection, + baseMintPubKey + ); + const quoteMintPubKey = new PublicKey(quoteToken.publicKey ?? ""); + const quoteTokenMint = await getMint( + connection, + quoteMintPubKey + ); + const baseTokenRecord: TokenRecord = { + symbol: baseToken.symbol, + name: baseToken.name ? baseToken.name : baseToken.symbol, + decimals: baseToken.decimals, + mintAcct: baseToken.publicKey ?? "", + supply: baseTokenMint.supply.toString(), + updatedAt: new Date(), + } + const quoteTokenRecord: TokenRecord = { + symbol: quoteToken.symbol, + name: quoteToken.name ? quoteToken.name : quoteToken.symbol, + decimals: quoteToken.decimals, + mintAcct: quoteToken.publicKey ?? "", + supply: quoteTokenMint.supply.toString(), + updatedAt: new Date(), + } + const tokensToInsert = [baseTokenRecord, quoteTokenRecord]; + //upsert tokens to db + await usingDb((db) => + db + .insert(schema.tokens) + .values(tokensToInsert) + .onConflictDoNothing() //TODO: probably better to update instead of do nothing on conflict, since supply/name/ticker could've changed + .execute() + ); } else { [baseToken, quoteToken] = tokens; } - // if we don't have an oracle.aggregator of 0 let's run this mf if (!ammMarketAccount.oracle.aggregator.isZero()) { // indexing the twap diff --git a/packages/indexer/src/v4_indexer/indexer.ts b/packages/indexer/src/v4_indexer/indexer.ts index c42bb40..b809f9e 100644 --- a/packages/indexer/src/v4_indexer/indexer.ts +++ b/packages/indexer/src/v4_indexer/indexer.ts @@ -83,7 +83,7 @@ const parseEvents = (transactionResponse: VersionedTransactionResponse | Transac //indexes signature export async function index(signature: string, programId: PublicKey) { - // try { + try { if (!programId.equals(AMM_PROGRAM_ID) && !programId.equals(CONDITIONAL_VAULT_PROGRAM_ID)) { //autocrat program id, we aren't indexing these for now console.log("Unknown program id: ", programId.toBase58()); @@ -96,18 +96,7 @@ export async function index(signature: string, programId: PublicKey) { return; } - const events = parseEvents(transactionResponse); - const ammEvents = events.ammEvents; - const vaultEvents = events.vaultEvents; - - Promise.all(ammEvents.map(async (event) => { - await processAmmEvent(event, signature, transactionResponse); - })); - - Promise.all(vaultEvents.map(async (event) => { - await processVaultEvent(event, signature, transactionResponse); - })); - + //insert signature to db await usingDb(async (db: DBConnection) => { await db.insert(schema.signatures).values({ signature: transactionResponse.transaction.signatures[0], @@ -121,14 +110,26 @@ export async function index(signature: string, programId: PublicKey) { account: programId.toString() }).onConflictDoNothing().execute(); }); + + const events = parseEvents(transactionResponse); + const ammEvents = events.ammEvents; + const vaultEvents = events.vaultEvents; + + Promise.all(ammEvents.map(async (event) => { + await processAmmEvent(event, signature, transactionResponse); + })); + + Promise.all(vaultEvents.map(async (event) => { + await processVaultEvent(event, signature, transactionResponse); + })); - // } catch (error) { - // logger.errorWithChatBotAlert([ - // error instanceof Error - // ? `Error processing signature: ${error.message}` - // : "Unknown error processing signature" - // ]); - // } + } catch (error) { + logger.errorWithChatBotAlert([ + error instanceof Error + ? `Error processing signature: ${error.message}` + : "Unknown error processing signature" + ]); + } } //indexes signature from logs