Skip to content

Commit

Permalink
refactor: calculate.ts
Browse files Browse the repository at this point in the history
dont use signals in utils
use BigNumber
  • Loading branch information
dni committed Jan 5, 2024
1 parent 0e4ce18 commit 0cccf7f
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 145 deletions.
52 changes: 42 additions & 10 deletions src/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ const Create = () => {
const target = evt.currentTarget as HTMLInputElement;
const amount = target.value.trim();
const satAmount = convertAmount(BigNumber(amount), denomination());
const sendAmount = calculateSendAmount(satAmount.toNumber());
const sendAmount = calculateSendAmount(
satAmount,
boltzFee(),
minerFee(),
reverse(),
);
setAmountChanged(sideReceive);
setReceiveAmount(BigNumber(satAmount));
setSendAmount(BigNumber(sendAmount));
setReceiveAmount(satAmount);
setSendAmount(sendAmount);
validateAmount();
target.setCustomValidity("");
target.classList.remove("invalid");
Expand All @@ -75,10 +80,15 @@ const Create = () => {
const target = evt.currentTarget as HTMLInputElement;
const amount = target.value.trim();
const satAmount = convertAmount(BigNumber(amount), denomination());
const receiveAmount = calculateReceiveAmount(satAmount.toNumber());
const receiveAmount = calculateReceiveAmount(
satAmount,
boltzFee(),
minerFee(),
reverse(),
);
setAmountChanged(sideSend);
setSendAmount(BigNumber(satAmount));
setReceiveAmount(BigNumber(receiveAmount));
setSendAmount(satAmount);
setReceiveAmount(receiveAmount);
validateAmount();
target.setCustomValidity("");
target.classList.remove("invalid");
Expand Down Expand Up @@ -151,7 +161,14 @@ const Create = () => {

const setAmount = (amount: number) => {
setSendAmount(BigNumber(amount));
setReceiveAmount(BigNumber(calculateReceiveAmount(amount)));
setReceiveAmount(
calculateReceiveAmount(
BigNumber(amount),
boltzFee(),
minerFee(),
reverse(),
),
);
validateAmount();
sendAmountRef.focus();
};
Expand All @@ -164,11 +181,21 @@ const Create = () => {
on([boltzFee, minerFee, reverse, asset], () => {
if (amountChanged() === sideReceive) {
setSendAmount(
BigNumber(calculateSendAmount(receiveAmount().toNumber())),
calculateSendAmount(
receiveAmount(),
boltzFee(),
minerFee(),
reverse(),
),
);
} else {
setReceiveAmount(
BigNumber(calculateReceiveAmount(sendAmount().toNumber())),
calculateReceiveAmount(
sendAmount(),
boltzFee(),
minerFee(),
reverse(),
),
);
}
validateAmount();
Expand Down Expand Up @@ -282,7 +309,12 @@ const Create = () => {
required
type="text"
placeholder={formatAmount(
BigNumber(calculateReceiveAmount(minimum())),
calculateReceiveAmount(
BigNumber(minimum()),
boltzFee(),
minerFee(),
reverse(),
),
denomination(),
)}
maxlength={calculateDigits(maximum(), denomination())}
Expand Down
18 changes: 15 additions & 3 deletions src/components/Fees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ const Fees = () => {
setMinerFee(fee);
}

const calculateLimit = (limit: number) => {
return reverse() ? limit : calculateSendAmount(limit);
const calculateLimit = (limit: number): number => {
return reverse()
? limit
: calculateSendAmount(
BigNumber(limit),
boltzFee(),
minerFee(),
reverse(),
).toNumber();
};

setMinimum(calculateLimit(cfg.limits.minimal));
Expand Down Expand Up @@ -86,7 +93,12 @@ const Fees = () => {
{t("fee")} ({boltzFee()}%):{" "}
<span class="boltz-fee">
{formatAmount(
BigNumber(calculateBoltzFeeOnSend(sendAmount())),
calculateBoltzFeeOnSend(
sendAmount(),
boltzFee(),
minerFee(),
reverse(),
),
denomination(),
true,
)}
Expand Down
11 changes: 10 additions & 1 deletion src/components/InvoiceInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { RBTC } from "../consts";
import t from "../i18n";
import {
asset,
boltzFee,
denomination,
invoice,
minerFee,
receiveAmount,
receiveAmountFormatted,
reverse,
Expand Down Expand Up @@ -35,7 +37,14 @@ const InvoiceInput = () => {
} else {
const sats = validateInvoice(inputValue);
setReceiveAmount(BigNumber(sats));
setSendAmount(BigNumber(calculateSendAmount(sats)));
setSendAmount(
calculateSendAmount(
BigNumber(sats),
boltzFee(),
minerFee(),
reverse(),
),
);
setInvoice(inputValue);
setLnurl("");
setInvoiceValid(true);
Expand Down
83 changes: 49 additions & 34 deletions src/utils/calculate.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,73 @@
import { BigNumber } from "bignumber.js";

import { boltzFee, minerFee, reverse } from "../signals";

const bigRound = (big: BigNumber): BigNumber => {
const bigCeil = (big: BigNumber): BigNumber => {
return big.integerValue(BigNumber.ROUND_CEIL);
};

export const calculateReceiveAmount = (sendAmount: number): number => {
const receiveAmount = reverse()
? BigNumber(sendAmount)
.minus(bigRound(BigNumber(sendAmount).times(boltzFee()).div(100)))
.minus(minerFee())
: BigNumber(sendAmount)
.minus(minerFee())
.div(BigNumber(1).plus(BigNumber(boltzFee()).div(100)));
return Math.max(Math.floor(receiveAmount.toNumber()), 0);
const bigFloor = (big: BigNumber): BigNumber => {
return big.integerValue(BigNumber.ROUND_FLOOR);
};

export const calculateReceiveAmount = (
sendAmount: BigNumber,
boltzFee: number,
minerFee: number,
reverse: boolean,
): BigNumber => {
const receiveAmount = reverse
? sendAmount
.minus(bigCeil(sendAmount.times(boltzFee).div(100)))
.minus(minerFee)
: sendAmount
.minus(minerFee)
.div(BigNumber(1).plus(BigNumber(boltzFee).div(100)));
return BigNumber.maximum(bigFloor(receiveAmount), 0);
};

export const calculateBoltzFeeOnSend = (sendAmount: BigNumber): number => {
export const calculateBoltzFeeOnSend = (
sendAmount: BigNumber,
boltzFee: number,
minerFee: number,
reverse: boolean,
): BigNumber => {
let fee: BigNumber;

if (reverse()) {
fee = bigRound(sendAmount.times(boltzFee()).div(100));
if (reverse) {
fee = bigCeil(sendAmount.times(boltzFee).div(100));
} else {
fee = sendAmount
.minus(calculateReceiveAmount(sendAmount.toNumber()))
.minus(minerFee());
.minus(
calculateReceiveAmount(sendAmount, boltzFee, minerFee, reverse),
)
.minus(minerFee);

if (sendAmount.toNumber() < minerFee()) {
if (sendAmount.toNumber() < minerFee) {
fee = BigNumber(0);
}
}

return Math.ceil(fee.toNumber());
return bigCeil(fee);
};

export const calculateSendAmount = (receiveAmount: number): number => {
return reverse()
? Math.ceil(
BigNumber(receiveAmount)
.plus(minerFee())
.div(BigNumber(1).minus(BigNumber(boltzFee()).div(100)))
.toNumber(),
export const calculateSendAmount = (
receiveAmount: BigNumber,
boltzFee: number,
minerFee: number,
reverse: boolean,
): BigNumber => {
return reverse
? bigCeil(
receiveAmount
.plus(minerFee)
.div(BigNumber(1).minus(BigNumber(boltzFee).div(100))),
)
: Math.floor(
BigNumber(receiveAmount)
: bigFloor(
receiveAmount
.plus(
bigRound(
BigNumber(receiveAmount).times(
BigNumber(boltzFee()).div(100),
),
bigCeil(
receiveAmount.times(BigNumber(boltzFee).div(100)),
),
)
.plus(minerFee())
.toNumber(),
.plus(minerFee),
);
};
7 changes: 6 additions & 1 deletion tests/components/Create.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ describe("Create", () => {

expect(setReceiveAmount).toHaveBeenCalledTimes(1);
expect(setReceiveAmount).toHaveBeenCalledWith(
BigNumber(calculateReceiveAmount(amount)),
calculateReceiveAmount(
BigNumber(amount),
signals.boltzFee(),
signals.minerFee(),
signals.reverse(),
),
);
});
});
15 changes: 13 additions & 2 deletions tests/components/Fees.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render } from "@solidjs/testing-library";
import { BigNumber } from "bignumber.js";
import { beforeAll, beforeEach, describe, expect, test, vi } from "vitest";

import Fees from "../../src/components/Fees";
Expand Down Expand Up @@ -33,10 +34,20 @@ describe("Fees component", () => {
signals.setReverse(false);

expect(setMinimum).toHaveBeenLastCalledWith(
calculateSendAmount(cfg["BTC/BTC"].limits.minimal),
calculateSendAmount(
BigNumber(cfg["BTC/BTC"].limits.minimal),
signals.boltzFee(),
signals.minerFee(),
signals.reverse(),
).toNumber(),
);
expect(setMaximum).toHaveBeenLastCalledWith(
calculateSendAmount(cfg["BTC/BTC"].limits.maximal),
calculateSendAmount(
BigNumber(cfg["BTC/BTC"].limits.maximal),
signals.boltzFee(),
signals.minerFee(),
signals.reverse(),
).toNumber(),
);
});
});
Loading

0 comments on commit 0cccf7f

Please sign in to comment.