Skip to content

Commit

Permalink
add limit buy and sell txs
Browse files Browse the repository at this point in the history
  • Loading branch information
Robiquet committed Apr 25, 2024
1 parent 74ff2e0 commit 7df5e01
Showing 1 changed file with 58 additions and 4 deletions.
62 changes: 58 additions & 4 deletions components/trade-form/LimitOrderForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
MarketOutcomeAssetId,
ZTG,
getIndexOf,
isRpcSdk,
parseAssetId,
} from "@zeitgeistpm/sdk";
import MarketContextActionOutcomeSelector from "components/markets/MarketContextActionOutcomeSelector";
Expand All @@ -14,6 +15,7 @@ import { useAssetMetadata } from "lib/hooks/queries/useAssetMetadata";
import { useBalance } from "lib/hooks/queries/useBalance";
import { useMarket } from "lib/hooks/queries/useMarket";
import { useMarketSpotPrices } from "lib/hooks/queries/useMarketSpotPrices";
import { useExtrinsic } from "lib/hooks/useExtrinsic";
import { useSdkv2 } from "lib/hooks/useSdkv2";
import { useNotifications } from "lib/state/notifications";
import { useWallet } from "lib/state/wallet";
Expand Down Expand Up @@ -64,13 +66,39 @@ export const LimitBuyOrderForm = ({

const maxAmount = baseAssetBalance?.div(price ?? 0) ?? new Decimal(0);

const { isLoading, send: buy } = useExtrinsic<{
price: Decimal;
amount: Decimal;
}>(
(params) => {
if (!isRpcSdk(sdk) || !market || !selectedAsset || !params) return;
const { price, amount } = params;
return sdk.api.tx.hybridRouter.buy(
marketId,
market.assets.length,
selectedAsset,
amount.mul(ZTG).toFixed(0),
price.mul(ZTG).toFixed(0),
[],
"LimitOrder",
);
},
{
onSuccess: () => {
notificationStore.pushNotification(`Placed buy order`, {
type: "Success",
});
},
},
);

return (
<LimitOrderForm
marketId={marketId}
asset={selectedAsset}
side="buy"
onSubmit={() => {
// place buy order
onSubmit={(price, amount) => {
buy({ price, amount });
}}
onAssetChange={(asset) => {
setSelectedAsset(asset);
Expand Down Expand Up @@ -113,13 +141,39 @@ export const LimitSellOrderForm = ({
selectedAsset,
);

const { isLoading, send: sell } = useExtrinsic<{
price: Decimal;
amount: Decimal;
}>(
(params) => {
if (!isRpcSdk(sdk) || !market || !selectedAsset || !params) return;
const { price, amount } = params;
return sdk.api.tx.hybridRouter.sell(
marketId,
market.assets.length,
selectedAsset,
amount.mul(ZTG).toFixed(0),
price.mul(ZTG).toFixed(0),
[],
"LimitOrder",
);
},
{
onSuccess: () => {
notificationStore.pushNotification(`Placed sell order`, {
type: "Success",
});
},
},
);

return (
<LimitOrderForm
marketId={marketId}
asset={selectedAsset}
side="sell"
onSubmit={() => {
// place sell order
onSubmit={(price, amount) => {
sell({ price, amount });
}}
onAssetChange={(asset) => {
setSelectedAsset(asset);
Expand Down

0 comments on commit 7df5e01

Please sign in to comment.