Skip to content

Commit

Permalink
feat: order history queyr changes
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Aug 12, 2024
1 parent 10724d1 commit b4e6c18
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 39 deletions.
67 changes: 66 additions & 1 deletion packages/server/src/queries/complex/orderbooks/active-orders.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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";
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,
Expand Down Expand Up @@ -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";
Expand Down
5 changes: 5 additions & 0 deletions packages/server/src/queries/osmosis/orderbooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 6 additions & 38 deletions packages/trpc/src/orderbook-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { tickToPrice } from "@osmosis-labs/math";
import {
CursorPaginationSchema,
getOrderbookActiveOrders,
getOrderbookAllActiveOrders,
getOrderbookHistoricalOrders,
getOrderbookMakerFee,
getOrderbookPools,
Expand Down Expand Up @@ -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}`,
Expand Down

0 comments on commit b4e6c18

Please sign in to comment.