Skip to content

Commit

Permalink
fix: error is displayed before maxValue is loaded (#7614)
Browse files Browse the repository at this point in the history
* fix: wait for max value to load

chore: clean up

* chore: changeset
  • Loading branch information
liviuciulinaru authored Aug 19, 2024
1 parent 65a3622 commit f8756b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-weeks-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": patch
---

add delay to determine if the max amout value is less than 0
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
NotEnoughGasSwap,
} from "@ledgerhq/errors";
import { Account } from "@ledgerhq/types-live";
import { useMemo } from "react";
import { useEffect, useMemo, useState } from "react";
import useBridgeTransaction, { Result } from "../../../bridge/useBridgeTransaction";
import { Transaction } from "../../../generated/types";
import {
Expand Down Expand Up @@ -171,10 +171,27 @@ export const useSwapTransaction = ({
isEnabled,
});

// libs/coin-modules/coin-evm/src/prepareTransaction.ts L47
// returns 0 if the balance - fees is less than 0
const maxAmountLowerThanBallanceError =
isMaxEnabled && fromState.amount?.eq(0) ? new NotEnoughBalanceSwap() : undefined;
const [maxAmountLowerThanBallanceError, setMaxAmountLowerThanBallanceError] = useState<
Error | undefined
>(undefined);

useEffect(() => {
const timer = setTimeout(
() => {
// libs/coin-modules/coin-evm/src/prepareTransaction.ts L47
// returns 0 if the balance - fees is less than 0
const error =
isMaxEnabled && !isMaxLoading && fromState.amount?.eq(0)
? new NotEnoughBalanceSwap()
: undefined;
setMaxAmountLowerThanBallanceError(error);
},
isMaxEnabled ? 500 : 0,
);

// Cleanup the timeout if the component unmounts or the dependencies change
return () => clearTimeout(timer);
}, [isMaxEnabled, isMaxLoading, fromState]);

return {
...bridgeTransaction,
Expand Down

0 comments on commit f8756b2

Please sign in to comment.