diff --git a/packages/server/src/queries/complex/orderbooks/active-orders.ts b/packages/server/src/queries/complex/orderbooks/active-orders.ts index 4fb989d525..ecd0e8dc3a 100644 --- a/packages/server/src/queries/complex/orderbooks/active-orders.ts +++ b/packages/server/src/queries/complex/orderbooks/active-orders.ts @@ -1,6 +1,6 @@ import { Dec, Int } from "@keplr-wallet/unit"; import { tickToPrice } from "@osmosis-labs/math"; -import { Chain } from "@osmosis-labs/types"; +import { AssetList, Chain } from "@osmosis-labs/types"; import { getAssetFromAssetList } from "@osmosis-labs/utils"; import cachified, { CacheEntry } from "cachified"; import dayjs from "dayjs"; @@ -8,6 +8,8 @@ import { LRUCache } from "lru-cache"; import { DEFAULT_LRU_OPTIONS } from "../../../utils/cache"; import { LimitOrder, queryOrderbookActiveOrders } from "../../osmosis"; +import { getOrderbookHistoricalOrders } from "./historical-orders"; +import { getOrderbookPools } from "./pools"; import { getOrderbookTickState, getOrderbookTickUnrealizedCancels, @@ -53,6 +55,69 @@ export function getOrderbookActiveOrders({ }); } +export function getOrderbookAllActiveOrders({ + userOsmoAddress, + chainList, + assetLists, +}: { + userOsmoAddress: string; + chainList: Chain[]; + assetLists: AssetList[]; +}) { + return cachified({ + cache: activeOrdersCache, + key: `orderbookAllActiveOrders-${userOsmoAddress}`, + ttl: 1500, // 1.5 seconds + getFreshValue: async () => { + const pools = await getOrderbookPools(); + const contractAddresses = pools.map((p) => p.contractAddress); + const promises = contractAddresses.map(async (contractAddress) => { + const { baseDenom, quoteDenom } = pools.find( + (p) => p.contractAddress === contractAddress + )!; + + const baseAsset = getAssetFromAssetList({ + assetLists, + coinMinimalDenom: baseDenom, + }); + const quoteAsset = getAssetFromAssetList({ + assetLists, + coinMinimalDenom: quoteDenom, + }); + + const { + data: { orders }, + } = await queryOrderbookActiveOrders({ + orderbookAddress: contractAddress, + userAddress: userOsmoAddress, + chainList, + }); + + if (orders.length === 0) return []; + + return getTickInfoAndTransformOrders( + contractAddress, + orders, + chainList, + quoteAsset, + baseAsset + ); + }); + + promises.push( + getOrderbookHistoricalOrders({ + userOsmoAddress, + assetLists, + chainList, + }) + ); + + const ordersByContracts = await Promise.all(promises); + return ordersByContracts.flat(); + }, + }); +} + function mapOrderStatus(order: LimitOrder, percentFilled: Dec): OrderStatus { const quantInt = parseInt(order.quantity); if (quantInt === 0 || percentFilled.equals(new Dec(1))) return "filled"; diff --git a/packages/server/src/queries/osmosis/orderbooks.ts b/packages/server/src/queries/osmosis/orderbooks.ts index 7d4b6752ab..88edcbd834 100644 --- a/packages/server/src/queries/osmosis/orderbooks.ts +++ b/packages/server/src/queries/osmosis/orderbooks.ts @@ -52,6 +52,11 @@ export const queryOrderbookActiveOrders = createNodeQuery< const encodedMsg = Buffer.from(msg).toString("base64"); return `/cosmwasm/wasm/v1/contract/${orderbookAddress}/smart/${encodedMsg}`; }, + options: () => { + return { + keepalive: true, + }; + }, }); interface TickValues { total_amount_of_liquidity: string; diff --git a/packages/trpc/src/orderbook-router.ts b/packages/trpc/src/orderbook-router.ts index 5dc42e2873..855ac29593 100644 --- a/packages/trpc/src/orderbook-router.ts +++ b/packages/trpc/src/orderbook-router.ts @@ -3,6 +3,7 @@ import { tickToPrice } from "@osmosis-labs/math"; import { CursorPaginationSchema, getOrderbookActiveOrders, + getOrderbookAllActiveOrders, getOrderbookHistoricalOrders, getOrderbookMakerFee, getOrderbookPools, @@ -62,44 +63,11 @@ export const orderbookRouter = createTRPCRouter({ return maybeCachePaginatedItems({ getFreshItems: async () => { const { userOsmoAddress } = input; - const pools = await getOrderbookPools(); - 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 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; - } - ); - promises.push( - getOrderbookHistoricalOrders({ - userOsmoAddress: input.userOsmoAddress, - assetLists: ctx.assetLists, - chainList: ctx.chainList, - }) - ); - - const ordersByContracts = await Promise.all(promises); - const allOrders = ordersByContracts.flat(); + const allOrders = await getOrderbookAllActiveOrders({ + userOsmoAddress, + chainList: ctx.chainList, + assetLists: ctx.assetLists, + }); return allOrders.sort(defaultSortOrders); }, cacheKey: `all-active-orders-${input.userOsmoAddress}`,