Skip to content

Commit

Permalink
fix: WIP batch query implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Aug 23, 2024
1 parent dd73ae2 commit 6434758
Showing 1 changed file with 56 additions and 31 deletions.
87 changes: 56 additions & 31 deletions packages/trpc/src/orderbook-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,40 +66,65 @@ export const orderbookRouter = createTRPCRouter({
const contractAddresses = pools.map((p) => p.contractAddress);
if (contractAddresses.length === 0 || userOsmoAddress.length === 0)
return [];
const promises = contractAddresses.map(
async (contractOsmoAddress: string) => {
const { baseDenom, quoteDenom } = pools.find(
(p) => p.contractAddress === contractOsmoAddress
)!;
const batchSize = 5; // Define the batch size
const batches = contractAddresses.reduce((acc: any[], _, index) => {
const batchIndex = Math.floor(index / batchSize);
if (!acc[batchIndex]) {
acc[batchIndex] = [];
}
acc[batchIndex].push(contractAddresses[index]);
return acc;
}, []);

const allOrders = [];
for (let i = 0; i < batches.length; i++) {
const promises = batches[i].map(
async (contractOsmoAddress: string) => {
const { baseDenom, quoteDenom } = pools.find(
(p) => p.contractAddress === contractOsmoAddress
)!;

const quoteAsset = getAssetFromAssetList({
coinMinimalDenom: quoteDenom,
assetLists: ctx.assetLists,
});
const baseAsset = getAssetFromAssetList({
coinMinimalDenom: baseDenom,
assetLists: ctx.assetLists,
});
const orders = await getOrderbookActiveOrders({
orderbookAddress: contractOsmoAddress,
userOsmoAddress: userOsmoAddress,
chainList: ctx.chainList,
baseAsset,
quoteAsset,
});
return orders;
const quoteAsset = getAssetFromAssetList({
coinMinimalDenom: quoteDenom,
assetLists: ctx.assetLists,
});
const baseAsset = getAssetFromAssetList({
coinMinimalDenom: baseDenom,
assetLists: ctx.assetLists,
});
const orders = await getOrderbookActiveOrders({
orderbookAddress: contractOsmoAddress,
userOsmoAddress: userOsmoAddress,
chainList: ctx.chainList,
baseAsset,
quoteAsset,
});
return orders;
}
);
if (i === batches.length - 1) {
promises.push(
getOrderbookHistoricalOrders({
userOsmoAddress: input.userOsmoAddress,
assetLists: ctx.assetLists,
chainList: ctx.chainList,
})
);
}
);
promises.push(
getOrderbookHistoricalOrders({
userOsmoAddress: input.userOsmoAddress,
assetLists: ctx.assetLists,
chainList: ctx.chainList,
})
);
const batchOrders = await Promise.all(promises);
allOrders.push(...batchOrders.flat());
await new Promise((resolve) => setTimeout(resolve, 150));
}
// promises.push(
// getOrderbookHistoricalOrders({
// userOsmoAddress: input.userOsmoAddress,
// assetLists: ctx.assetLists,
// chainList: ctx.chainList,
// })
// );

const ordersByContracts = await Promise.all(promises);
const allOrders = ordersByContracts.flat();
// const ordersByContracts = await Promise.all(promises);
// const allOrders = ordersByContracts.flat();

return allOrders.sort(defaultSortOrders);
},
Expand Down

0 comments on commit 6434758

Please sign in to comment.