Skip to content

Commit

Permalink
fix: scientific notation issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufseyrek committed Nov 16, 2024
1 parent 33e5c81 commit 524dc34
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ReactDOMClient from 'react-dom/client';
import BigNumber from 'bignumber.js';

import './index.css';
import reportWebVitals from './reportWebVitals';
Expand All @@ -16,6 +17,7 @@ if (!container) {

// For some reason Web3 depends on the process api
global.process = require('process');
BigNumber.config({ EXPONENTIAL_AT: 1e9 });

const App = () => (
<ThemeProvider>
Expand Down
20 changes: 10 additions & 10 deletions src/pages/Bridge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ interface BridgeAction {
bridgeTxHash: string;
}

const checkEvmNetworkHasEnoughBalance = async (asset: Asset, normalizedAmount: number) => {
const checkEvmNetworkHasEnoughBalance = async (asset: Asset, normalizedAmount: BigNumber) => {
if (asset.symbol === 'WAE') return true;

const bridgeAddress = Constants.ethereum.bridge_address;
Expand Down Expand Up @@ -156,9 +156,9 @@ const Bridge: React.FC = () => {

const normalizedAmount = React.useMemo(() => {
if (!amount) {
return 0;
return new BigNumber(0);
}
return Number(amount) * 10 ** asset.decimals;
return new BigNumber(amount).shiftedBy(asset.decimals);
}, [asset, amount]);

const isValidDestination = React.useMemo(() => {
Expand Down Expand Up @@ -211,10 +211,10 @@ const Bridge: React.FC = () => {
if (!isValidDestination || !destination?.startsWith('ak_')) {
return showSnackMessage('Invalid destination!');
}
if (!normalizedAmount || normalizedAmount <= 0) {
if (!normalizedAmount || normalizedAmount.isLessThanOrEqualTo(0)) {
return showSnackMessage('Invalid amount!');
}
if (normalizedAmount > Number(ethereum.assetInfo?.asset?.balance || 0)) {
if (normalizedAmount.isGreaterThan(ethereum.assetInfo?.asset?.balance || 0)) {
return showSnackMessage('Not enough balance!');
}

Expand All @@ -225,7 +225,7 @@ const Bridge: React.FC = () => {
let allowanceTxHash = '';
if (asset.ethAddress === Constants.ethereum.default_eth) {
action_type = BRIDGE_ETH_ACTION_TYPE;
eth_amount = BigInt(normalizedAmount);
eth_amount = BigInt(normalizedAmount.toString());
} else if (asset.ethAddress === Constants.ethereum.wae) {
action_type = BRIDGE_AETERNITY_ACTION_TYPE;
} else {
Expand Down Expand Up @@ -270,7 +270,7 @@ const Bridge: React.FC = () => {
direction,
asset,
destination,
amount: new BigNumber(normalizedAmount.toString()).shiftedBy(-asset.decimals).toString(),
amount: normalizedAmount.shiftedBy(-asset.decimals).toString(),
allowanceTxHash,
bridgeTxHash: bridgeOutResult.hash,
});
Expand All @@ -290,10 +290,10 @@ const Bridge: React.FC = () => {
if (!isValidDestination || !destination?.startsWith('0x')) {
return showSnackMessage('Invalid destination!');
}
if (!normalizedAmount || normalizedAmount <= 0) {
if (!normalizedAmount || normalizedAmount.isLessThanOrEqualTo(0)) {
return showSnackMessage('Invalid amount!');
}
if (normalizedAmount > Number(aeternity.assetInfo?.asset?.balance || 0)) {
if (normalizedAmount.isGreaterThan(aeternity.assetInfo?.asset?.balance || 0)) {
return showSnackMessage('Not enough balance!');
}

Expand Down Expand Up @@ -324,7 +324,7 @@ const Bridge: React.FC = () => {

if (asset.aeAddress === Constants.aeternity.default_ae) {
action_type = BRIDGE_AETERNITY_ACTION_TYPE;
ae_amount = BigInt(normalizedAmount);
ae_amount = BigInt(normalizedAmount.toString());
} else {
action_type =
asset.aeAddress === Constants.aeternity.aeeth ? BRIDGE_ETH_ACTION_TYPE : BRIDGE_TOKEN_ACTION_TYPE;
Expand Down

0 comments on commit 524dc34

Please sign in to comment.