Skip to content

Commit

Permalink
feat: enrich token from db not rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
calmdentist committed Nov 19, 2024
1 parent 569114e commit 9f6f833
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 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
2 changes: 1 addition & 1 deletion packages/indexer/src/v3_indexer/builders/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
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

0 comments on commit 9f6f833

Please sign in to comment.