Skip to content

Commit

Permalink
fix: invalidation of negative amounts and too many decimal points (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-ziv authored Apr 5, 2022
1 parent e8848f8 commit d94b8ea
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 38 deletions.
35 changes: 15 additions & 20 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
## [1.1.0-dev.1](https://github.com/starkware-libs/starkgate-frontend/compare/v1.0.0...v1.1.0-dev.1) (2022-04-05)


### 🧩 Features

* support more tokens on testnet ([#133](https://github.com/starkware-libs/starkgate-frontend/issues/133)) ([1e95d33](https://github.com/starkware-libs/starkgate-frontend/commit/1e95d330e5a47a4182f77bf7cb1af4d3a39adcbe))

- support more tokens on testnet ([#133](https://github.com/starkware-libs/starkgate-frontend/issues/133)) ([1e95d33](https://github.com/starkware-libs/starkgate-frontend/commit/1e95d330e5a47a4182f77bf7cb1af4d3a39adcbe))

### 🔧 Fixes

* when clicking Max button on L2 network 0 is shown instead of max balance ([#134](https://github.com/starkware-libs/starkgate-frontend/issues/134)) ([fb4618b](https://github.com/starkware-libs/starkgate-frontend/commit/fb4618b4a0c4171296858e904fcc94b56e04215c))
- when clicking Max button on L2 network 0 is shown instead of max balance ([#134](https://github.com/starkware-libs/starkgate-frontend/issues/134)) ([fb4618b](https://github.com/starkware-libs/starkgate-frontend/commit/fb4618b4a0c4171296858e904fcc94b56e04215c))

## [1.0.0](https://github.com/starkware-libs/starkgate-frontend/compare/v0.0.1...v1.0.0) (2022-04-04)


### ⚠ BREAKING CHANGES

* alpha version (#126)
- alpha version (#126)

### 💉 Improvements

* utils ([#82](https://github.com/starkware-libs/starkgate-frontend/issues/82)) ([c38e56e](https://github.com/starkware-libs/starkgate-frontend/commit/c38e56e1d942b3350e45811a435da22472dcf00e))

- utils ([#82](https://github.com/starkware-libs/starkgate-frontend/issues/82)) ([c38e56e](https://github.com/starkware-libs/starkgate-frontend/commit/c38e56e1d942b3350e45811a435da22472dcf00e))

### 🧩 Features

* alpha version ([#126](https://github.com/starkware-libs/starkgate-frontend/issues/126)) ([5b15ca0](https://github.com/starkware-libs/starkgate-frontend/commit/5b15ca0b1c87f25beab2f30367384d63503d8ef9))
* handle browser support ([#96](https://github.com/starkware-libs/starkgate-frontend/issues/96)) ([56d920d](https://github.com/starkware-libs/starkgate-frontend/commit/56d920d46af53e9d60368321435aed86e463063e))
* remove redux and use providers ([#98](https://github.com/starkware-libs/starkgate-frontend/issues/98)) ([ed38ae4](https://github.com/starkware-libs/starkgate-frontend/commit/ed38ae4925468e7331f974a618f3d4da04773ca8))
* **toasts:** add link to transfer log ([#79](https://github.com/starkware-libs/starkgate-frontend/issues/79)) ([b743b85](https://github.com/starkware-libs/starkgate-frontend/commit/b743b85be4d573a93e3dbfe1e58fdbd8042a5e61))
* use blockNumber=pending for balanceOf calls ([#58](https://github.com/starkware-libs/starkgate-frontend/issues/58)) ([9a84b05](https://github.com/starkware-libs/starkgate-frontend/commit/9a84b05142a8ea44e2e2dd0a39aba39f35b327b4))

- alpha version ([#126](https://github.com/starkware-libs/starkgate-frontend/issues/126)) ([5b15ca0](https://github.com/starkware-libs/starkgate-frontend/commit/5b15ca0b1c87f25beab2f30367384d63503d8ef9))
- handle browser support ([#96](https://github.com/starkware-libs/starkgate-frontend/issues/96)) ([56d920d](https://github.com/starkware-libs/starkgate-frontend/commit/56d920d46af53e9d60368321435aed86e463063e))
- remove redux and use providers ([#98](https://github.com/starkware-libs/starkgate-frontend/issues/98)) ([ed38ae4](https://github.com/starkware-libs/starkgate-frontend/commit/ed38ae4925468e7331f974a618f3d4da04773ca8))
- **toasts:** add link to transfer log ([#79](https://github.com/starkware-libs/starkgate-frontend/issues/79)) ([b743b85](https://github.com/starkware-libs/starkgate-frontend/commit/b743b85be4d573a93e3dbfe1e58fdbd8042a5e61))
- use blockNumber=pending for balanceOf calls ([#58](https://github.com/starkware-libs/starkgate-frontend/issues/58)) ([9a84b05](https://github.com/starkware-libs/starkgate-frontend/commit/9a84b05142a8ea44e2e2dd0a39aba39f35b327b4))

### 🔧 Fixes

* add missing env poll block number interval ([d364cb4](https://github.com/starkware-libs/starkgate-frontend/commit/d364cb44b8bf1ef62bba3cf08a13cd48c2897c6e))
* empty callback in promise .then ([#127](https://github.com/starkware-libs/starkgate-frontend/issues/127)) ([9d13c3b](https://github.com/starkware-libs/starkgate-frontend/commit/9d13c3bf01090b3f0db2d392ae3747d56fd3c9a5))
* **getTransactionHash:** add fee and version to calculation ([#90](https://github.com/starkware-libs/starkgate-frontend/issues/90)) ([938ffe0](https://github.com/starkware-libs/starkgate-frontend/commit/938ffe079c794409df04982096de22fd6e9d31ad))
* redux error on switching menu "non-serializable value was detected" ([#89](https://github.com/starkware-libs/starkgate-frontend/issues/89)) ([9dc4972](https://github.com/starkware-libs/starkgate-frontend/commit/9dc49729c9cef14c016be348fe170f084d478843))
* remove pending transfer toast ([#92](https://github.com/starkware-libs/starkgate-frontend/issues/92)) ([20f51f3](https://github.com/starkware-libs/starkgate-frontend/commit/20f51f3d68bf5d9c8a1212e6442fa5d246fbbeb2))
* update ETH token addresses and add SLF token ([#84](https://github.com/starkware-libs/starkgate-frontend/issues/84)) ([2c5a9df](https://github.com/starkware-libs/starkgate-frontend/commit/2c5a9df17acf28fb3c87bf91a245dc0f0f6e5b2e))
- add missing env poll block number interval ([d364cb4](https://github.com/starkware-libs/starkgate-frontend/commit/d364cb44b8bf1ef62bba3cf08a13cd48c2897c6e))
- empty callback in promise .then ([#127](https://github.com/starkware-libs/starkgate-frontend/issues/127)) ([9d13c3b](https://github.com/starkware-libs/starkgate-frontend/commit/9d13c3bf01090b3f0db2d392ae3747d56fd3c9a5))
- **getTransactionHash:** add fee and version to calculation ([#90](https://github.com/starkware-libs/starkgate-frontend/issues/90)) ([938ffe0](https://github.com/starkware-libs/starkgate-frontend/commit/938ffe079c794409df04982096de22fd6e9d31ad))
- redux error on switching menu "non-serializable value was detected" ([#89](https://github.com/starkware-libs/starkgate-frontend/issues/89)) ([9dc4972](https://github.com/starkware-libs/starkgate-frontend/commit/9dc49729c9cef14c016be348fe170f084d478843))
- remove pending transfer toast ([#92](https://github.com/starkware-libs/starkgate-frontend/issues/92)) ([20f51f3](https://github.com/starkware-libs/starkgate-frontend/commit/20f51f3d68bf5d9c8a1212e6442fa5d246fbbeb2))
- update ETH token addresses and add SLF token ([#84](https://github.com/starkware-libs/starkgate-frontend/issues/84)) ([2c5a9df](https://github.com/starkware-libs/starkgate-frontend/commit/2c5a9df17acf28fb3c87bf91a245dc0f0f6e5b2e))

## [1.0.0-dev.2](https://github.com/starkware-libs/starkgate-frontend/compare/v1.0.0-dev.1...v1.0.0-dev.2) (2022-04-04)

Expand Down
35 changes: 35 additions & 0 deletions src/__tests__/utils/number.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import utils from '../../utils';

const {afterDecimal, isNegative, isZero} = utils.number;

describe('afterDecimal', () => {
it.only('should return number of decimals places', () => {
expect(afterDecimal('')).toEqual(0);
expect(afterDecimal(0)).toEqual(0);
expect(afterDecimal('0')).toEqual(0);
expect(afterDecimal(0.1)).toEqual(1);
expect(afterDecimal('0.1')).toEqual(1);
expect(afterDecimal(0.00001)).toEqual(5);
expect(afterDecimal('0.00000001')).toEqual(8);
});
});

describe('isZero', () => {
it('should return true for zero numbers', () => {
expect(isZero(0.0)).toBeTruthy();
expect(isZero(-0.0)).toBeTruthy();
expect(isZero(0)).toBeTruthy();
expect(isZero('0')).toBeTruthy();
expect(isZero(0.1)).toBeFalsy();
expect(isZero(0.000001)).toBeFalsy();
});
});

describe('isNegative', () => {
it('should return true for negative numbers', () => {
expect(isNegative(0)).toBeFalsy();
expect(isNegative(-1)).toBeTruthy();
expect(isNegative(-0.1)).toBeTruthy();
expect(isNegative('-0.1')).toBeTruthy();
});
});
44 changes: 31 additions & 13 deletions src/components/Features/Transfer/Transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {useMaxDeposit, useTransferToL1, useTransferToL2} from '../../../hooks';
import {useMenu} from '../../../providers/MenuProvider';
import {useL1Token, useL2Token, useTokens} from '../../../providers/TokensProvider';
import {useAmount, useIsL1, useIsL2, useTransfer} from '../../../providers/TransferProvider';
import utils from '../../../utils';
import {
Loading,
Menu,
Expand All @@ -17,7 +18,12 @@ import {
} from '../../UI';
import {LoadingSize} from '../../UI/Loading/Loading.enums';
import styles from './Transfer.module.scss';
import {INSUFFICIENT_BALANCE_ERROR_MSG, MAX_DEPOSIT_ERROR_MSG} from './Transfer.strings';
import {
INSUFFICIENT_BALANCE_ERROR_MSG,
MAX_DEPOSIT_ERROR_MSG,
NEGATIVE_VALUE_ERROR_MSG,
TOO_MANY_DIGITS_ERROR_MSG
} from './Transfer.strings';

export const Transfer = () => {
const [isL1, swapToL1] = useIsL1();
Expand All @@ -44,24 +50,36 @@ export const Transfer = () => {
useEffect(() => {
if (selectedToken) {
setHasInputError(false);
if (selectedToken.isLoading || Math.ceil(amount) === 0 || (isL1 && !maxDeposit)) {
if (selectedToken.isLoading || utils.number.isZero(amount) || (isL1 && !maxDeposit)) {
setIsButtonDisabled(true);
} else {
if (amount > selectedToken.balance) {
setHasInputError(true);
setErrorMsg(INSUFFICIENT_BALANCE_ERROR_MSG);
setIsButtonDisabled(true);
} else if (isL1 && amount > maxDeposit) {
setHasInputError(true);
setErrorMsg(MAX_DEPOSIT_ERROR_MSG);
setIsButtonDisabled(true);
} else {
setIsButtonDisabled(false);
}
validateAmount();
}
}
}, [amount, selectedToken, maxDeposit, isL1]);

const validateAmount = () => {
let errorMsg = '';

if (utils.number.afterDecimal(amount) > selectedToken.decimals) {
errorMsg = TOO_MANY_DIGITS_ERROR_MSG;
} else if (utils.number.isNegative(amount)) {
errorMsg = NEGATIVE_VALUE_ERROR_MSG;
} else if (amount > selectedToken.balance) {
errorMsg = INSUFFICIENT_BALANCE_ERROR_MSG;
} else if (isL1 && amount > maxDeposit) {
errorMsg = MAX_DEPOSIT_ERROR_MSG;
}

if (errorMsg) {
setHasInputError(true);
setErrorMsg(errorMsg);
setIsButtonDisabled(true);
} else {
setIsButtonDisabled(false);
}
};

const onMaxClick = () => {
try {
track(TrackEvent.TRANSFER.MAX_CLICK);
Expand Down
11 changes: 6 additions & 5 deletions src/components/Features/Transfer/Transfer.strings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import utils from '../../../utils';

export const INSUFFICIENT_BALANCE_ERROR_MSG = utils.getTranslation(
'menus.transfer.insufficient_balance_error_msg'
);

export const MAX_DEPOSIT_ERROR_MSG = utils.getTranslation('menus.transfer.max_deposit_error_msg');
export const {
insufficient_balance_error_msg: INSUFFICIENT_BALANCE_ERROR_MSG,
max_deposit_error_msg: MAX_DEPOSIT_ERROR_MSG,
too_many_digits_error_msg: TOO_MANY_DIGITS_ERROR_MSG,
negative_value_error_msg: NEGATIVE_VALUE_ERROR_MSG
} = utils.getTranslation('menus.transfer');
2 changes: 2 additions & 0 deletions src/config/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const strings = {
transfer: {
to_txt: 'to',
from_txt: 'from',
negative_value_error_msg: 'Amount must be a positive number',
too_many_digits_error_msg: 'Too many decimal places',
insufficient_balance_error_msg: 'Insufficient balance',
max_deposit_error_msg:
'You have exceeded the maximum transfer amount. Please reduce the amount and try again.',
Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as blockchain from './blockchain';
import * as browser from './browser';
import * as date from './date';
import * as logger from './logger';
import * as number from './number';
import * as object from './object';
import * as parser from './parser';
import * as storage from './storage';
Expand Down Expand Up @@ -32,6 +33,7 @@ const utils = {
blockchain,
logger,
storage,
number,
printPackageInfo,
getTranslation
};
Expand Down
14 changes: 14 additions & 0 deletions src/utils/number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const afterDecimal = num => {
if (Number.isInteger(Number(num))) {
return 0;
}
return num.toString().split('.')[1]?.length;
};

export const isZero = num => {
return Math.sign(num) === 0;
};

export const isNegative = num => {
return Math.sign(num) === -1;
};

0 comments on commit d94b8ea

Please sign in to comment.