Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/fractional order form rounding errors #317

Merged
merged 5 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions components/marketplace/buy-fractional-order-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ export const BuyFractionalOrderForm = ({
const unitsToBuy =
(hypercertUnits * percentageAsBigInt) /
(BigInt(100) * DEFAULT_NUM_FRACTIONS);
Jipperism marked this conversation as resolved.
Show resolved Hide resolved
return unitsToBuy.toString();
return unitsToBuy < BigInt(0) ? BigInt(0) : unitsToBuy;
} catch (e) {
console.error(e);
return "0";
return BigInt(0);
}
};

Expand Down Expand Up @@ -155,17 +155,15 @@ export const BuyFractionalOrderForm = ({
const percentageAmount = form.watch("percentageAmount");
const pricePerPercent = form.watch("pricePerPercent");

const unitsToBuy =
BigInt(getUnitsToBuy(percentageAmount)) > BigInt(0)
? getUnitsToBuy(percentageAmount)
: "0";
const unitsToBuy = getUnitsToBuy(percentageAmount);
const pricePerUnit = getPricePerUnit(
pricePerPercent,
BigInt(hypercert.units || 0),
);

const totalPrice = formatPrice(
order.chainId,
getTotalPriceFromPercentage(
Jipperism marked this conversation as resolved.
Show resolved Hide resolved
BigInt(pricePerPercent),
unitsToBuy === "0" ? 0 : Number(percentageAmount),
),
unitsToBuy * pricePerUnit,
currency.address,
true,
);
Expand All @@ -176,7 +174,7 @@ export const BuyFractionalOrderForm = ({
currency.address,
);

const disabled = !form.formState.isValid || unitsToBuy === "0";
const disabled = !form.formState.isValid || unitsToBuy === BigInt(0);

return (
<Form {...form}>
Expand All @@ -193,7 +191,7 @@ export const BuyFractionalOrderForm = ({
<div className="text-sm text-slate-500">
You will buy{" "}
<b>
<FormattedUnits>{unitsToBuy}</FormattedUnits>
<FormattedUnits>{unitsToBuy.toString()}</FormattedUnits>
</b>{" "}
units , for a total of <b>{totalPrice}</b>. (min:{" "}
{minPercentageAmount}%, max: {maxPercentageAmount}%)
Expand Down
12 changes: 4 additions & 8 deletions marketplace/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ export const useBuyFractionalMakerAsk = () => {
totalUnitsInHypercert,
}: {
order: MarketplaceOrder;
unitAmount: string;
unitAmount: bigint;
pricePerUnit: string;
hypercertName?: string | null;
totalUnitsInHypercert?: bigint;
Expand Down Expand Up @@ -473,7 +473,7 @@ export const useBuyFractionalMakerAsk = () => {
takerOrder = hypercertExchangeClient.createFractionalSaleTakerBid(
order,
address,
unitAmount,
unitAmount.toString(),
pricePerUnit,
);
} catch (e) {
Expand All @@ -492,7 +492,7 @@ export const useBuyFractionalMakerAsk = () => {
);
}

const totalPrice = BigInt(order.price) * BigInt(unitAmount);
const totalPrice = BigInt(order.price) * unitAmount;
try {
await setStep("ERC20");
if (currency.address !== zeroAddress) {
Expand Down Expand Up @@ -566,11 +566,7 @@ export const useBuyFractionalMakerAsk = () => {
<span>
Congratulations, you successfully bought{" "}
<b>
{calculateBigIntPercentage(
BigInt(unitAmount),
totalUnitsInHypercert,
)}
%
{calculateBigIntPercentage(unitAmount, totalUnitsInHypercert)}%
</b>{" "}
of <b>{hypercertName}</b>.
</span>
Expand Down