From 9f6f833ce1f42dceebf39250590fe801401d9c2d Mon Sep 17 00:00:00 2001 From: advaith101 Date: Tue, 19 Nov 2024 13:03:18 -0500 Subject: [PATCH 1/2] feat: enrich token from db not rpc --- packages/indexer/src/subscriber.ts | 6 +-- .../indexer/src/v3_indexer/builders/swaps.ts | 2 +- .../v3_indexer/indexers/amm-market/utils.ts | 38 +++++++++++++++---- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/packages/indexer/src/subscriber.ts b/packages/indexer/src/subscriber.ts index 68709e6..50d805d 100644 --- a/packages/indexer/src/subscriber.ts +++ b/packages/indexer/src/subscriber.ts @@ -43,9 +43,9 @@ export async function subscribeAll() { V4_AMM_PROGRAM_ID, V4_AUTOCRAT_PROGRAM_ID, V4_CONDITIONAL_VAULT_PROGRAM_ID, - V3_AMM_PROGRAM_ID, - V3_AUTOCRAT_PROGRAM_ID, - V3_CONDITIONAL_VAULT_PROGRAM_ID + // V3_AMM_PROGRAM_ID, + // V3_AUTOCRAT_PROGRAM_ID, + // V3_CONDITIONAL_VAULT_PROGRAM_ID ]; console.log("Subscribing to logs"); Promise.all(programIds.map(async (programId) => subscribe(programId))); diff --git a/packages/indexer/src/v3_indexer/builders/swaps.ts b/packages/indexer/src/v3_indexer/builders/swaps.ts index 59c21e3..b9f2822 100644 --- a/packages/indexer/src/v3_indexer/builders/swaps.ts +++ b/packages/indexer/src/v3_indexer/builders/swaps.ts @@ -283,7 +283,7 @@ export class SwapBuilder { const marketAcct = swapIx.accountsWithData.find((a) => a.name === "amm"); if (!marketAcct) return Err({ type: "missing data" }); - const marketAcctPubKey = new PublicKey(marketAcct.pubkey); + // const marketAcctPubKey = new PublicKey(marketAcct.pubkey); // this.indexPriceAndTWAPForAccount(marketAcctPubKey); const userAcct = swapIx.accountsWithData.find((a) => a.name === "user"); if (!userAcct) return Err({ type: "missing data" }); 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 e89a09d..ee3ee9b 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,7 @@ import { BN } from "@coral-xyz/anchor"; import { BN_0, enrichTokenMetadata } from "@metadaoproject/futarchy-sdk"; import { PriceMath } from "@metadaoproject/futarchy/v0.4"; -import { schema, usingDb, eq } from "@metadaoproject/indexer-db"; +import { schema, usingDb, eq, inArray } from "@metadaoproject/indexer-db"; import { PricesType } from "@metadaoproject/indexer-db/lib/schema"; import { TwapRecord, @@ -29,15 +29,37 @@ export async function indexAmmMarketAccountWithContext( const ammMarketAccount = await rpcReadClient.markets.amm.decodeMarket( accountInfo ); - const baseToken = await enrichTokenMetadata( - ammMarketAccount.baseMint, - provider - ); - const quoteToken = await enrichTokenMetadata( - ammMarketAccount.quoteMint, - provider + + // TODO: prob need to type these lol + let baseToken; + let quoteToken; + + //get base and quote decimals from db + console.log("utils::indexAmmMarketAccountWithContext::getting tokens from db", ammMarketAccount.baseMint.toString(), ammMarketAccount.quoteMint.toString()); + const tokens = await usingDb((db) => + db + .select() + .from(schema.tokens) + .where(inArray(schema.tokens.mintAcct, [ammMarketAccount.baseMint.toString(), ammMarketAccount.quoteMint.toString()])) + .execute() ); + if (!tokens || tokens.length < 2) { + // fallback if we don't have the tokens in the db for some reason + console.log("utils::indexAmmMarketAccountWithContext::no tokens in db, fetching from rpc"); + baseToken = await enrichTokenMetadata( + ammMarketAccount.baseMint, + provider + ); + quoteToken = await enrichTokenMetadata( + ammMarketAccount.quoteMint, + provider + ) + } 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 From 9df10484e545db9d7c6967eb0c1b72b3ef55f1d4 Mon Sep 17 00:00:00 2001 From: advaith101 Date: Tue, 19 Nov 2024 14:15:27 -0500 Subject: [PATCH 2/2] feat: one listener for amms/prices/twaps/orders --- .../indexer/src/v3_indexer/builders/swaps.ts | 24 +++++++++++++++++-- .../src/v3_indexer/cli/txw/populate.ts | 13 +++++----- .../indexers/start-account-info-indexers.ts | 21 ++++++++-------- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/packages/indexer/src/v3_indexer/builders/swaps.ts b/packages/indexer/src/v3_indexer/builders/swaps.ts index b9f2822..4910ad9 100644 --- a/packages/indexer/src/v3_indexer/builders/swaps.ts +++ b/packages/indexer/src/v3_indexer/builders/swaps.ts @@ -230,6 +230,22 @@ export class SwapBuilder { // } return Ok(new SwapPersistable(swapOrder, swapTake, transactionRecord)); // priceRecord + } else { + // handle non-swap transactions (add/remove liquidity, crank, etc) + // find market account from instructions + console.log("builder::buildOrderFromSwapIx::looking for market account in non swap txn"); + let marketAcct: PublicKey | undefined; + for (const ix of tx.instructions) { + const candidate = ix.accountsWithData.find((a) => a.name === "amm"); + if (candidate) { + marketAcct = new PublicKey(candidate.pubkey); + break; + } + } + if (marketAcct) { + console.log("builder::buildOrderFromSwapIx::market found for non swap txn, indexing price and twap", marketAcct); + this.indexPriceAndTWAPForAccount(marketAcct); + } } return Err({ type: SwapPersistableError.NonSwapTransaction }); } catch (e: any) { @@ -283,8 +299,12 @@ export class SwapBuilder { const marketAcct = swapIx.accountsWithData.find((a) => a.name === "amm"); if (!marketAcct) return Err({ type: "missing data" }); - // const marketAcctPubKey = new PublicKey(marketAcct.pubkey); - // this.indexPriceAndTWAPForAccount(marketAcctPubKey); + + //get market account and index price and twap async + console.log("builder::buildOrderFromSwapIx::indexing price and twap for market", marketAcct.pubkey); + const marketAcctPubKey = new PublicKey(marketAcct.pubkey); + this.indexPriceAndTWAPForAccount(marketAcctPubKey); + const userAcct = swapIx.accountsWithData.find((a) => a.name === "user"); if (!userAcct) return Err({ type: "missing data" }); const userAcctPubKey = new PublicKey(userAcct.pubkey); diff --git a/packages/indexer/src/v3_indexer/cli/txw/populate.ts b/packages/indexer/src/v3_indexer/cli/txw/populate.ts index adca2d5..fa2f360 100644 --- a/packages/indexer/src/v3_indexer/cli/txw/populate.ts +++ b/packages/indexer/src/v3_indexer/cli/txw/populate.ts @@ -90,11 +90,12 @@ async function populateAmmMarketIndexerAccountDependencies() { )) ?? []; for (const ammMarket of ammMarkets) { - const newAmmIndexerDep: IndexerAccountDependency = { - acct: ammMarket.marketAcct.toString(), - name: "amm-market-accounts", - latestTxSigProcessed: null, - }; + // TODO: we no longer need an account info indexer market accounts.. leaving this here for now + // const newAmmIndexerDep: IndexerAccountDependency = { + // acct: ammMarket.marketAcct.toString(), + // name: "amm-market-accounts", + // latestTxSigProcessed: null, + // }; const newAmmIntervalIndexerDep: IndexerAccountDependency = { acct: ammMarket.marketAcct.toString(), name: "amm-market-accounts-fetch", @@ -111,7 +112,7 @@ async function populateAmmMarketIndexerAccountDependencies() { db .insert(schema.indexerAccountDependencies) .values([ - newAmmIndexerDep, + // newAmmIndexerDep, //TODO: leaving this here for now newAmmIntervalIndexerDep, newAmmLogsSubscribeIndexerDep, ]) diff --git a/packages/indexer/src/v3_indexer/indexers/start-account-info-indexers.ts b/packages/indexer/src/v3_indexer/indexers/start-account-info-indexers.ts index a9eb65f..4974883 100644 --- a/packages/indexer/src/v3_indexer/indexers/start-account-info-indexers.ts +++ b/packages/indexer/src/v3_indexer/indexers/start-account-info-indexers.ts @@ -38,16 +38,17 @@ export async function startAccountInfoIndexer( } } - connection.onAccountChange(accountPubKey, async (accountInfo, context) => { - const res = await implementation.index( - accountInfo, - accountPubKey, - context - ); - if (!res.success) { - logger.error("error indexing account update", accountPubKey.toString()); - } - }); + // // TODO: re-enable this or delete this whole thing if not needed + // connection.onAccountChange(accountPubKey, async (accountInfo, context) => { + // const res = await implementation.index( + // accountInfo, + // accountPubKey, + // context + // ); + // if (!res.success) { + // logger.error("error indexing account update", accountPubKey.toString()); + // } + // }); } } function getAccountInfoIndexerImplementation(