Skip to content

Commit

Permalink
Merge pull request #321 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 02273f7 + f250cb1 commit ed312b5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
43 changes: 41 additions & 2 deletions packages/indexer/src/v3_indexer/indexers/amm-market/utils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand Down
41 changes: 21 additions & 20 deletions packages/indexer/src/v4_indexer/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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],
Expand All @@ -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
Expand Down

0 comments on commit ed312b5

Please sign in to comment.