Skip to content

Commit

Permalink
Merge pull request #319 from metaDAOproject/feat/performance
Browse files Browse the repository at this point in the history
feat/performance
  • Loading branch information
R-K-H authored Nov 19, 2024
2 parents 569114e + 9df1048 commit 02273f7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 28 deletions.
6 changes: 3 additions & 3 deletions packages/indexer/src/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
22 changes: 21 additions & 1 deletion packages/indexer/src/v3_indexer/builders/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -283,8 +299,12 @@ export class SwapBuilder {

const marketAcct = swapIx.accountsWithData.find((a) => a.name === "amm");
if (!marketAcct) return Err({ type: "missing data" });

//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);
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);
Expand Down
13 changes: 7 additions & 6 deletions packages/indexer/src/v3_indexer/cli/txw/populate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -111,7 +112,7 @@ async function populateAmmMarketIndexerAccountDependencies() {
db
.insert(schema.indexerAccountDependencies)
.values([
newAmmIndexerDep,
// newAmmIndexerDep, //TODO: leaving this here for now
newAmmIntervalIndexerDep,
newAmmLogsSubscribeIndexerDep,
])
Expand Down
38 changes: 30 additions & 8 deletions packages/indexer/src/v3_indexer/indexers/amm-market/utils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 02273f7

Please sign in to comment.