Skip to content

Commit

Permalink
feat: added contract query for active limit orders
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Jun 12, 2024
1 parent 85404c1 commit 4634c64
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
43 changes: 43 additions & 0 deletions packages/server/src/queries/osmosis/orderbooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Decimal256>,
// // 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}`;
},
});
20 changes: 18 additions & 2 deletions packages/server/src/trpc-routers/orderbook-router.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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;
}),
});
2 changes: 1 addition & 1 deletion packages/web/components/place-limit-tool/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const PlaceLimitTool: FunctionComponent<PlaceLimitToolProps> = observer(
quoteDenom,
});
const swapState = usePlaceLimit({
osmosisChainId: "localosmosis",
osmosisChainId: accountStore.osmosisChainId,
poolId,
orderDirection,
useQueryParams: false,
Expand Down
26 changes: 26 additions & 0 deletions packages/web/hooks/limit-orders/use-orderbook-pool.ts
Original file line number Diff line number Diff line change
@@ -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 = ({
Expand All @@ -13,16 +14,23 @@ export const useOrderbookPool = ({
const [contractAddress] = useState<string>(
"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,
quoteDenom,
contractAddress,
makerFee,
isMakerFeeLoading,
orderState,
};
};

Expand All @@ -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,
};
};

0 comments on commit 4634c64

Please sign in to comment.