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
Changes from 2 commits
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
19 changes: 12 additions & 7 deletions components/marketplace/buy-fractional-order-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import z from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { parseUnits } from "viem";
import { calculateBigIntPercentage } from "@/lib/calculateBigIntPercentage";
import { DEFAULT_NUM_FRACTIONS } from "@/configs/hypercerts";

const formSchema = z
.object({
Expand Down Expand Up @@ -83,9 +84,10 @@ export const BuyFractionalOrderForm = ({
const getUnitsToBuy = (percentageAmount: string) => {
try {
const hypercertUnits = BigInt(hypercert.units || 0);
const percentageAsBigInt = BigInt(Number(percentageAmount) * 100);
const percentageAsBigInt = parseUnits(percentageAmount, 8);
pheuberger marked this conversation as resolved.
Show resolved Hide resolved
const unitsToBuy =
(hypercertUnits * percentageAsBigInt) / BigInt(100 * 100);
(hypercertUnits * percentageAsBigInt) /
(BigInt(100) * DEFAULT_NUM_FRACTIONS);
Jipperism marked this conversation as resolved.
Show resolved Hide resolved
return unitsToBuy.toString();
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -153,11 +155,16 @@ export const BuyFractionalOrderForm = ({
const percentageAmount = form.watch("percentageAmount");
const pricePerPercent = form.watch("pricePerPercent");

const unitsToBuy =
pheuberger marked this conversation as resolved.
Show resolved Hide resolved
BigInt(getUnitsToBuy(percentageAmount)) > BigInt(0)
? getUnitsToBuy(percentageAmount)
: "0";

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

const unitsToBuy =
BigInt(getUnitsToBuy(percentageAmount)) > BigInt(0)
? getUnitsToBuy(percentageAmount)
: "0";
const disabled = !form.formState.isValid || unitsToBuy === "0";

return (
<Form {...form}>
Expand Down Expand Up @@ -238,6 +242,7 @@ export const BuyFractionalOrderForm = ({
variant={"outline"}
type="button"
onClick={form.handleSubmit(onSubmit)}
disabled={disabled}
>
Execute order
</Button>
Expand Down