Skip to content

Commit

Permalink
Merge pull request #175 from kizmt/feature/oo-indexer
Browse files Browse the repository at this point in the history
Feature/oo-indexer
  • Loading branch information
skrrb authored Sep 27, 2023
2 parents d16d681 + 03d3830 commit a5d9fb2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
22 changes: 17 additions & 5 deletions ts/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export type OracleConfigParams = IdlTypes<OpenbookV2>['OracleConfigParams'];
export type OracleConfig = IdlTypes<OpenbookV2>['OracleConfig'];
export type MarketAccount = IdlAccounts<OpenbookV2>['market'];
export type OpenOrdersAccount = IdlAccounts<OpenbookV2>['openOrdersAccount'];
export type OpenOrdersIndexerAccount =
IdlAccounts<OpenbookV2>['openOrdersIndexer'];
export type EventHeapAccount = IdlAccounts<OpenbookV2>['eventHeap'];
export type BookSideAccount = IdlAccounts<OpenbookV2>['bookSide'];
export type LeafNode = IdlTypes<OpenbookV2>['LeafNode'];
Expand Down Expand Up @@ -151,6 +153,16 @@ export class OpenBookV2Client {
}
}

public async getOpenOrdersIndexer(
publicKey: PublicKey,
): Promise<OpenOrdersIndexerAccount | null> {
try {
return await this.program.account.openOrdersIndexer.fetch(publicKey);
} catch {
return null;
}
}

public async getEventHeap(
publicKey: PublicKey,
): Promise<EventHeapAccount | null> {
Expand Down Expand Up @@ -196,8 +208,8 @@ export class OpenBookV2Client {
name: string,
quoteMint: PublicKey,
baseMint: PublicKey,
quoteLoteSize: BN,
baseLoteSize: BN,
quoteLotSize: BN,
baseLotSize: BN,
makerFee: BN,
takerFee: BN,
timeExpiry: BN,
Expand Down Expand Up @@ -247,8 +259,8 @@ export class OpenBookV2Client {
.createMarket(
name,
oracleConfigParams,
quoteLoteSize,
baseLoteSize,
quoteLotSize,
baseLotSize,
makerFee,
takerFee,
timeExpiry,
Expand Down Expand Up @@ -314,7 +326,7 @@ export class OpenBookV2Client {
Buffer.from('OpenOrders'),
this.walletPk.toBuffer(),
market.toBuffer(),
accountIndex.toBuffer('le', 4),
accountIndex.toArrayLike(Buffer, 'le', 4),
],
this.programId,
);
Expand Down
68 changes: 38 additions & 30 deletions ts/client/src/market.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { PublicKey, type Connection, type AccountInfo } from '@solana/web3.js';
import {
PublicKey,
type Connection,
type AccountInfo,
type Message,
} from '@solana/web3.js';
import { getFilteredProgramAccounts } from './client';
import { utils, Program, type Provider, getProvider } from '@coral-xyz/anchor';

Expand Down Expand Up @@ -27,7 +32,6 @@ export async function findAccountsByMints(
];
return await getFilteredProgramAccounts(connection, programId, filters);
}

interface Market {
market: string;
baseMint: string;
Expand Down Expand Up @@ -55,51 +59,55 @@ export async function findAllMarkets(
await connection.getSignaturesForAddress(eventAuthority)
).map((x) => x.signature);
const batchSignatures: [string[]] = [[]];
for (var i = 0; i < signatures.length; i += BATCH_TX_SIZE) {
for (let i = 0; i < signatures.length; i += BATCH_TX_SIZE) {
batchSignatures.push(signatures.slice(0, BATCH_TX_SIZE));
}
for (const batch of batchSignatures) {
const allTxs = await connection.getTransactions(batch, {
commitment: 'confirmed',
maxSupportedTransactionVersion: 0,
});
for (const tx of allTxs) {
if (
tx?.meta?.innerInstructions !== null &&
tx?.meta?.innerInstructions !== undefined
)
for (const innerIns of tx.meta.innerInstructions) {
// validate key and program key
const eventAuthorityKey = innerIns.instructions[1].accounts[0];
const programKey = innerIns.instructions[1].programIdIndex;

if (
tx.transaction.message.accountKeys[eventAuthorityKey].toString() !==
eventAuthority.toString() ||
tx.transaction.message.accountKeys[programKey].toString() !==
programId.toString()
) {
continue;
} else {
const ixData = utils.bytes.bs58.decode(
innerIns.instructions[1].data,
);
const eventData = utils.bytes.base64.encode(ixData.slice(8));
const event = program.coder.events.decode(eventData);
if (innerIns.instructions?.[1]?.accounts?.[0] !== undefined) {
console.log('err');
// validate key and program key
const eventAuthorityKey = innerIns.instructions[1].accounts[0];
const programKey = innerIns.instructions[1].programIdIndex;
if (
(tx.transaction.message as Message).staticAccountKeys[
eventAuthorityKey
].toString() !== eventAuthority.toString() ||
(tx.transaction.message as Message).staticAccountKeys[
programKey
].toString() !== programId.toString()
) {
continue;
} else {
const ixData = utils.bytes.bs58.decode(
innerIns.instructions[1].data,
);
const eventData = utils.bytes.base64.encode(ixData.slice(8));
const event = program.coder.events.decode(eventData);

if (event != null) {
const market: Market = {
market: (event.data.market as PublicKey).toString(),
baseMint: (event.data.baseMint as PublicKey).toString(),
quoteMint: (event.data.quoteMint as PublicKey).toString(),
name: event.data.name as string,
timestamp: tx.blockTime,
};
marketsAll.push(market);
if (event != null) {
const market: Market = {
market: (event.data.market as PublicKey).toString(),
baseMint: (event.data.baseMint as PublicKey).toString(),
quoteMint: (event.data.quoteMint as PublicKey).toString(),
name: event.data.name as string,
timestamp: tx.blockTime,
};
marketsAll.push(market);
}
}
}
}
}
}

return marketsAll;
}

0 comments on commit a5d9fb2

Please sign in to comment.