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: Max balance for dApp erc20 approval #13881

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ storiesOf('Component Library / CustomSpendCap', module).add('Default', () => (
<CustomSpendCap
ticker={TICKER}
accountBalance={ACCOUNT_BALANCE}
unroundedAccountBalance={ACCOUNT_BALANCE}
dappProposedValue={DAPP_PROPOSED_VALUE}
onInputChanged={INPUT_VALUE_CHANGED}
isEditDisabled={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function RenderCustomSpendCap(
<CustomSpendCap
ticker={TICKER}
accountBalance={ACCOUNT_BALANCE}
unroundedAccountBalance={ACCOUNT_BALANCE}
dappProposedValue={dappProposedValue}
onInputChanged={INPUT_VALUE_CHANGED}
isEditDisabled={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const CustomSpendCap = ({
ticker,
dappProposedValue,
accountBalance,
unroundedAccountBalance,
onInputChanged,
isEditDisabled,
editValue,
Expand Down Expand Up @@ -75,8 +76,8 @@ const CustomSpendCap = ({
};

useEffect(() => {
if (maxSelected) setValue(accountBalance);
}, [maxSelected, accountBalance]);
if (maxSelected) setValue(unroundedAccountBalance || accountBalance);
}, [maxSelected, accountBalance, unroundedAccountBalance]);
Comment on lines +79 to +80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (maxSelected) setValue(unroundedAccountBalance || accountBalance);
}, [maxSelected, accountBalance, unroundedAccountBalance]);
if (maxSelected) setValue(unroundedAccountBalance);
}, [maxSelected, unroundedAccountBalance]);

I think we may no longer need accountBalance. Else I think since unroundedAccountBalance may be an empty string, || accountBalance may never be reached


useEffect(() => {
if (Number(value) > Number(accountBalance))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface CustomSpendCapProps {
ticker: string;
dappProposedValue: string;
accountBalance: string;
unroundedAccountBalance: string;
/**
* @param value - The value of the input field
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ class ApproveTransactionReview extends PureComponent {
showBlockExplorerModal: false,
address: '',
isCustomSpendInputValid: true,
unroundedAccountBalance: null,
};

customSpendLimitInput = React.createRef();
Expand Down Expand Up @@ -383,7 +384,8 @@ class ApproveTransactionReview extends PureComponent {
tokenName,
tokenStandard,
tokenBalance,
createdSpendCap;
createdSpendCap,
unroundedAccountBalance = '';

const { spenderAddress, encodedAmount: encodedHexAmount } =
decodeApproveData(data);
Expand Down Expand Up @@ -429,6 +431,7 @@ class ApproveTransactionReview extends PureComponent {
erc20TokenBalance,
decimals,
);
unroundedAccountBalance = fromTokenMinimalUnit(erc20TokenBalance || 0, decimals);
}
} catch (e) {
tokenSymbol = contract?.symbol || 'ERC20 Token';
Expand Down Expand Up @@ -484,6 +487,7 @@ class ApproveTransactionReview extends PureComponent {
? tokenAllowanceState?.tokenSpendValue
: '',
spendLimitCustomValue: minTokenAllowance,
unroundedAccountBalance,
},
() => {
this.props.metrics.trackEvent(
Expand Down Expand Up @@ -809,6 +813,7 @@ class ApproveTransactionReview extends PureComponent {
isReadyToApprove,
isCustomSpendInputValid,
method,
unroundedAccountBalance,
} = this.state;

const {
Expand Down Expand Up @@ -1011,6 +1016,7 @@ class ApproveTransactionReview extends PureComponent {
dappProposedValue={originalApproveAmount}
tokenSpendValue={tokenSpendValue}
accountBalance={tokenBalance}
unroundedAccountBalance={unroundedAccountBalance}
tokenDecimal={tokenDecimals}
toggleLearnMoreWebPage={
this.toggleLearnMoreWebPage
Expand Down
Loading