Skip to content

Commit

Permalink
add router for buy form
Browse files Browse the repository at this point in the history
  • Loading branch information
Robiquet committed Apr 26, 2024
1 parent e2b97a4 commit 8488161
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions components/trade-form/BuyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { useForm } from "react-hook-form";
import { ISubmittableResult } from "@polkadot/types/types";
import { assetsAreEqual } from "lib/util/assets-are-equal";
import { perbillToNumber } from "lib/util/perbill-to-number";
import { useOrders } from "lib/hooks/queries/orderbook/useOrders";
import { selectOrdersForMarketBuy } from "lib/util/order-selection";

const slippageMultiplier = (100 - DEFAULT_SLIPPAGE_PERCENTAGE) / 100;

Expand Down Expand Up @@ -74,6 +76,7 @@ const BuyForm = ({
const baseSymbol = assetMetadata?.symbol;
const { data: baseAssetBalance } = useBalance(wallet.realAddress, baseAsset);
const { data: pool } = useAmm2Pool(marketId);
const { data: orders } = useOrders({ marketId_eq: marketId });

const swapFee = pool?.swapFee.div(ZTG);
const creatorFee = new Decimal(perbillToNumber(market?.creatorFee ?? 0));
Expand Down Expand Up @@ -171,17 +174,33 @@ const BuyForm = ({
!amount ||
amount === "" ||
market?.categories?.length == null ||
!selectedAsset
!selectedAsset ||
!newSpotPrice ||
!orders
) {
return;
}
const maxPrice = newSpotPrice.mul(1 / slippageMultiplier); // adjust by slippage

return sdk.api.tx.neoSwaps.buy(
const selectedOrders = selectOrdersForMarketBuy(
maxPrice,
orders.map(({ id, side, price, outcomeAmount }) => ({
id: Number(id),
amount: outcomeAmount,
price,
side,
})),
new Decimal(amount).abs().mul(ZTG),
);

return sdk.api.tx.hybridRouter.buy(
marketId,
market?.categories?.length,
selectedAsset,
new Decimal(amount).abs().mul(ZTG).toFixed(0),
maxPrice.mul(ZTG).toFixed(0),
minAmountOut.toFixed(0),
selectedOrders.map(({ id }) => id),
"ImmediateOrCancel",
);
},
{
Expand Down

0 comments on commit 8488161

Please sign in to comment.