Skip to content

Commit

Permalink
fix: wired in sqs query for active orders
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Aug 23, 2024
1 parent 7e0aac3 commit 082c86c
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 4 deletions.
45 changes: 44 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,14 @@
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 { queryActiveOrdersSQS } from "../../sidecar/orderbooks";
import {
getOrderbookTickState,
getOrderbookTickUnrealizedCancels,
Expand All @@ -16,6 +17,48 @@ import type { MappedLimitOrder, OrderStatus } from "./types";

const activeOrdersCache = new LRUCache<string, CacheEntry>(DEFAULT_LRU_OPTIONS);

export function getOrderbookActiveOrdersSQS({
userOsmoAddress,
chainList,
assetList,
}: {
userOsmoAddress: string;
chainList: Chain[];
assetList: AssetList[];
}) {
return cachified({
cache: activeOrdersCache,
key: `orderbookActiveOrders-sqs-${userOsmoAddress}`,
ttl: 10000, // 10 seconds
getFreshValue: () =>
queryActiveOrdersSQS({
userOsmoAddress,
}).then(async ({ orders }) => {
const mappedOrders: MappedLimitOrder[] = orders.map((o) => {
return {
...o,
price: new Dec(o.price),
quantity: o.quantity,
placed_quantity: o.placed_quantity,
percentClaimed: new Dec(o.percentClaimed),
totalFilled: o.totalFilled,
percentFilled: new Dec(o.percentFilled),
quoteAsset: getAssetFromAssetList({
coinMinimalDenom: o.quote_asset.symbol,
assetLists: assetList,
}),
baseAsset: getAssetFromAssetList({
coinMinimalDenom: o.base_asset.symbol,
assetLists: assetList,
}),
output: new Dec(o.output),
};
});
return mappedOrders;
}),
});
}

export function getOrderbookActiveOrders({
orderbookAddress,
userOsmoAddress,
Expand Down
40 changes: 40 additions & 0 deletions packages/server/src/queries/sidecar/orderbooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,47 @@ export type CanonicalOrderbooksResponse = {
contract_address: string;
}[];

export interface SQSActiveOrder {
tick_id: number;
order_id: number;
order_direction: "bid" | "ask";
owner: string;
quantity: number;
etas: string;
placed_quantity: number;
placed_at: number;
price: string;
percentClaimed: string;
totalFilled: number;
percentFilled: string;
orderbookAddress: string;
status: "open" | "partiallyFilled";
output: string;
quote_asset: {
symbol: string;
};
base_asset: {
symbol: string;
};
}

export type ActiveOrdersResponse = {
orders: SQSActiveOrder[];
};

export async function queryCanonicalOrderbooks() {
const url = new URL("/pools/canonical-orderbooks", SIDECAR_BASE_URL);
return await apiClient<CanonicalOrderbooksResponse>(url.toString());
}

export async function queryActiveOrdersSQS({
userOsmoAddress,
}: {
userOsmoAddress: string;
}) {
const url = new URL(
`/orderbook/active-orders?userOsmoAddress=${userOsmoAddress}`,
SIDECAR_BASE_URL
);
return await apiClient<ActiveOrdersResponse>(url.toString());
}
25 changes: 25 additions & 0 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,
getOrderbookActiveOrdersSQS,
getOrderbookHistoricalOrders,
getOrderbookMakerFee,
getOrderbookPools,
Expand Down Expand Up @@ -134,6 +135,30 @@ export const orderbookRouter = createTRPCRouter({
limit: input.limit,
});
}),
getAllOrdersSQS: publicProcedure
.input(GetInfiniteLimitOrdersInputSchema)
.query(async ({ input, ctx }) => {
return maybeCachePaginatedItems({
getFreshItems: async () => {
const { userOsmoAddress } = input;
const orders = await getOrderbookActiveOrdersSQS({
userOsmoAddress,
chainList: ctx.chainList,
assetList: ctx.assetLists,
});
const historicalOrders = await getOrderbookHistoricalOrders({
userOsmoAddress,
assetLists: ctx.assetLists,
chainList: ctx.chainList,
});
return [...orders, ...historicalOrders].sort(defaultSortOrders);
},
cacheKey: `all-active-orders-sqs-${input.userOsmoAddress}`,
ttl: 2000,
cursor: input.cursor,
limit: input.limit,
});
}),
getOrderbookState: publicProcedure
.input(OsmoAddressSchema.required())
.query(async ({ input, ctx }) => {
Expand Down
1 change: 0 additions & 1 deletion packages/web/components/complex/orders-history/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export const OrderHistory = observer(() => {
userAddress: wallet?.address ?? "",
pageSize: 20,
});

const groupedOrders = useMemo(() => groupOrdersByStatus(orders), [orders]);
const groups = useMemo(
() =>
Expand Down
2 changes: 2 additions & 0 deletions packages/web/components/trade-tool/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "~/components/swap-tool/swap-tool-tabs";
import { EventName, EventPage } from "~/config";
import { useAmplitudeAnalytics, useTranslation } from "~/hooks";
import { useOrderbookAllActiveOrders } from "~/hooks/limit-orders/use-orderbook";
import { PreviousTrade } from "~/pages";
import { useStore } from "~/stores";

Expand All @@ -39,6 +40,7 @@ export const TradeTool: FunctionComponent<TradeToolProps> = observer(
const { accountStore } = useStore();
const wallet = accountStore.getWallet(accountStore.osmosisChainId);

useOrderbookAllActiveOrders({ userAddress: wallet?.address ?? "" });
useEffect(() => {
switch (tab) {
case SwapToolTab.BUY:
Expand Down
4 changes: 2 additions & 2 deletions packages/web/hooks/limit-orders/use-orderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export type DisplayableLimitOrder = MappedLimitOrder;
export const useOrderbookAllActiveOrders = ({
userAddress,
pageSize = 10,
refetchInterval = 30000,
refetchInterval = 2000,
}: {
userAddress: string;
pageSize?: number;
Expand All @@ -282,7 +282,7 @@ export const useOrderbookAllActiveOrders = ({
hasNextPage,
refetch,
isRefetching,
} = api.edge.orderbooks.getAllOrders.useInfiniteQuery(
} = api.edge.orderbooks.getAllOrdersSQS.useInfiniteQuery(
{
userOsmoAddress: userAddress,
limit: pageSize,
Expand Down

0 comments on commit 082c86c

Please sign in to comment.