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: issue with fee UI react rendering loop #66

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-staking",
"version": "0.2.31",
"version": "0.2.32",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
24 changes: 11 additions & 13 deletions src/app/components/Staking/Form/StakingFee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,20 @@ export const StakingFee: React.FC<StakingFeeProps> = ({
<div className="flex flex-col justify-center gap-1 items-center">
<div className="min-h-8 flex justify-center flex-col items-center">
{mempoolFeeRates ? (
<p>
Recommended fee rate: <strong>{defaultFeeRate} sats/vB</strong>
</p>
<div>
<p>
Recommended fee rate: <strong>{defaultFeeRate} sats/vB</strong>
</p>
<p>
Transaction fee amount:{" "}
<strong>
{satoshiToBtc(stakingFeeSat)} {coinName}
</strong>
</p>
</div>
) : (
<LoadingSmall text="Loading recommended fee rate..." />
)}
{stakingFeeSat ? (
<p>
Transaction fee amount:{" "}
<strong>
{satoshiToBtc(stakingFeeSat)} {coinName}
</strong>
</p>
) : (
<LoadingSmall text="Loading transaction fee amount..." />
)}
</div>
<button
className="btn btn-sm btn-link no-underline"
Expand Down
1 change: 1 addition & 0 deletions src/app/components/Staking/Staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ export const Staking: React.FC<StakingProps> = ({
stakingAmountSat,
stakingTimeBlocksWithFixed,
!!finalityProvider,
stakingFeeSat,
);

const previewReady =
Expand Down
8 changes: 8 additions & 0 deletions src/utils/isStakingSignReady.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const isStakingSignReady = (
amount: number,
time: number,
fpSelected: boolean,
stakingFeeSat: number,
): { isReady: boolean; reason: string } => {
if (!fpSelected)
return {
Expand Down Expand Up @@ -39,6 +40,13 @@ export const isStakingSignReady = (
isReady: false,
reason: "Please enter a valid staking period",
};
} else if (stakingFeeSat === 0) {
// This is a temporary solution when the fee is not calculated or throw an error
// the staking fee is set to 0 by stakingFeeSat from staking.tsx
return {
isReady: false,
reason: "Not enough funds to cover fees for staking",
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved
};
}
return {
isReady: true,
Expand Down
12 changes: 10 additions & 2 deletions tests/utils/isStakingSignReady.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isStakingSignReady } from "@/utils/isStakingSignReady";

describe("utils/isStakingSignReady", () => {
it("should return false with reason if fpSelected is false", () => {
const result = isStakingSignReady(1, 2, 3, 4, 5, 6, false);
const result = isStakingSignReady(1, 2, 3, 4, 5, 6, false, 1);
expect(result.isReady).toBe(false);
expect(result.reason).toBe("Please select a finality provider");
});
Expand Down Expand Up @@ -44,6 +44,7 @@ describe("utils/isStakingSignReady", () => {
input.amount,
6,
true,
1,
);
expect(result.isReady).toBe(false);
expect(result.reason).toBe("Please enter a valid stake amount");
Expand Down Expand Up @@ -87,14 +88,21 @@ describe("utils/isStakingSignReady", () => {
5,
input.time,
true,
1,
);
expect(result.isReady).toBe(false);
expect(result.reason).toBe("Please enter a valid staking period");
});
});

it("should return false with reason if sufficientBalance is false", () => {
const result = isStakingSignReady(1, 10, 20, 30, 5, 25, true, 0);
expect(result.isReady).toBe(false);
expect(result.reason).toBe("Not enough funds to cover fees for staking");
});

it("should return true with empty reason if amount and time are ready", () => {
const result = isStakingSignReady(1, 10, 20, 30, 5, 25, true);
const result = isStakingSignReady(1, 10, 20, 30, 5, 25, true, 1);
expect(result.isReady).toBe(true);
expect(result.reason).toBe("");
});
Expand Down