diff --git a/packages/server/src/queries/osmosis/orderbooks.ts b/packages/server/src/queries/osmosis/orderbooks.ts index d3fe74fd4b..73159985dc 100644 --- a/packages/server/src/queries/osmosis/orderbooks.ts +++ b/packages/server/src/queries/osmosis/orderbooks.ts @@ -19,3 +19,46 @@ export const queryOrderbookMakerFee = createNodeQuery< return `/cosmwasm/wasm/v1/contract/${orderbookAddress}/smart/${encodedMsg}`; }, }); + +// pub tick_id: i64, +// pub order_id: u64, +// pub order_direction: OrderDirection, +// pub owner: Addr, +// pub quantity: Uint128, +// pub etas: Decimal256, +// pub claim_bounty: Option, +// // Immutable quantity of the order when placed +// pub placed_quantity: Uint128, + +export interface LimitOrder { + tick_id: number; + order_id: number; + order_direction: "ask" | "bid"; + owner: string; + quantity: string; + etas: string; + claim_bounty?: string; + placed_quantity: string; +} + +interface OrderbookActiveOrdersResponse { + data: LimitOrder[]; +} + +export const queryOrderbookActiveOrders = createNodeQuery< + OrderbookActiveOrdersResponse, + { + orderbookAddress: string; + userAddress: string; + } +>({ + path: ({ orderbookAddress, userAddress }) => { + const msg = JSON.stringify({ + orders_by_owner: { + owner: userAddress, + }, + }); + const encodedMsg = Buffer.from(msg).toString("base64"); + return `/cosmwasm/wasm/v1/contract/${orderbookAddress}/smart/${encodedMsg}`; + }, +}); diff --git a/packages/server/src/trpc-routers/orderbook-router.ts b/packages/server/src/trpc-routers/orderbook-router.ts index 010fd7949c..6afabc2a0e 100644 --- a/packages/server/src/trpc-routers/orderbook-router.ts +++ b/packages/server/src/trpc-routers/orderbook-router.ts @@ -1,5 +1,8 @@ -import { queryOrderbookMakerFee } from "../queries"; -import { ContractOsmoAddressSchema } from "../queries/complex/parameter-types"; +import { queryOrderbookActiveOrders, queryOrderbookMakerFee } from "../queries"; +import { + ContractOsmoAddressSchema, + UserOsmoAddressSchema, +} from "../queries/complex/parameter-types"; import { createTRPCRouter, publicProcedure } from "../trpc"; export const orderbookRouter = createTRPCRouter({ @@ -15,4 +18,17 @@ export const orderbookRouter = createTRPCRouter({ makerFee: data, }; }), + getActiveOrders: publicProcedure + .input( + ContractOsmoAddressSchema.required().and(UserOsmoAddressSchema.required()) + ) + .query(async ({ input, ctx }) => { + const { contractOsmoAddress, userOsmoAddress } = input; + const resp = await queryOrderbookActiveOrders({ + orderbookAddress: contractOsmoAddress, + userAddress: userOsmoAddress, + chainList: ctx.chainList, + }); + return resp.data; + }), }); diff --git a/packages/web/components/place-limit-tool/index.tsx b/packages/web/components/place-limit-tool/index.tsx index 6484cda9a6..fdcc86b06b 100644 --- a/packages/web/components/place-limit-tool/index.tsx +++ b/packages/web/components/place-limit-tool/index.tsx @@ -40,7 +40,7 @@ export const PlaceLimitTool: FunctionComponent = observer( quoteDenom, }); const swapState = usePlaceLimit({ - osmosisChainId: "localosmosis", + osmosisChainId: accountStore.osmosisChainId, poolId, orderDirection, useQueryParams: false, diff --git a/packages/web/hooks/limit-orders/use-orderbook-pool.ts b/packages/web/hooks/limit-orders/use-orderbook-pool.ts index 3f46c8a611..caaf9e3637 100644 --- a/packages/web/hooks/limit-orders/use-orderbook-pool.ts +++ b/packages/web/hooks/limit-orders/use-orderbook-pool.ts @@ -1,6 +1,7 @@ import { Dec } from "@keplr-wallet/unit"; import { useMemo, useState } from "react"; +import { useStore } from "~/stores"; import { api } from "~/utils/trpc"; export const useOrderbookPool = ({ @@ -13,9 +14,15 @@ export const useOrderbookPool = ({ const [contractAddress] = useState( "osmo1svmdh0ega4jg44xc3gg36tkjpzrzlrgajv6v6c2wf0ul8m3gjajs0dps9w" ); + const { accountStore } = useStore(); const { makerFee, isLoading: isMakerFeeLoading } = useMakerFee({ orderbookAddress: contractAddress, }); + const account = accountStore.getWallet(accountStore.osmosisChainId); + const orderState = useOrders({ + orderbookAddress: contractAddress, + userAddress: account?.address ?? "", + }); return { poolId: "1", baseDenom, @@ -23,6 +30,7 @@ export const useOrderbookPool = ({ contractAddress, makerFee, isMakerFeeLoading, + orderState, }; }; @@ -41,3 +49,21 @@ const useMakerFee = ({ orderbookAddress }: { orderbookAddress: string }) => { isLoading, }; }; + +const useOrders = ({ + orderbookAddress, + userAddress, +}: { + orderbookAddress: string; + userAddress: string; +}) => { + const { data: orders, isLoading } = + api.local.orderbook.getActiveOrders.useQuery({ + contractOsmoAddress: orderbookAddress, + userOsmoAddress: userAddress, + }); + return { + orders, + isLoading, + }; +};