Skip to content

Commit

Permalink
Merge branch 'fix/ui-20240801' into tmp/20240731
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016x committed Aug 1, 2024
2 parents dc08706 + e671eea commit 0d1f2a8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
48 changes: 47 additions & 1 deletion src/ui/utils/token.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { BigNumber } from 'bignumber.js';
import { TokenItem } from '@rabby-wallet/rabby-api/dist/types';
import { GasLevel, TokenItem } from '@rabby-wallet/rabby-api/dist/types';
import { Contract, providers } from 'ethers';
import { hexToString } from 'web3-utils';
import { AbstractPortfolioToken } from './portfolio/types';
import { CustomTestnetToken } from '@/background/service/customTestnet';
import { findChain } from '@/utils/chain';
import { MINIMUM_GAS_LIMIT } from '@/constant';

export const geTokenDecimals = async (
id: string,
Expand Down Expand Up @@ -211,3 +212,48 @@ export const isTestnetTokenItem = (token: TokenItem) => {
serverId: token.chain,
})?.isTestnet;
};

function checkGasIsEnough({
token_balance_hex,
price,
gasLimit,
}: {
token_balance_hex: TokenItem['raw_amount_hex_str'];
price: number;
gasLimit: number;
}) {
return new BigNumber(token_balance_hex || 0, 16).gte(
new BigNumber(gasLimit).times(price)
);
}
export function checkIfTokenBalanceEnough(
token: TokenItem,
options?: {
gasList?: GasLevel[];
gasLimit?: number;
}
) {
const { gasLimit = MINIMUM_GAS_LIMIT, gasList = [] } = options || {};
const normalLevel = gasList?.find((e) => e.level === 'normal');
const slowLevel = gasList?.find((e) => e.level === 'slow');
const customLevel = gasList?.find((e) => e.level === 'custom');

const isNormalEnough = checkGasIsEnough({
token_balance_hex: token?.raw_amount_hex_str,
price: normalLevel?.price || 0,
gasLimit,
});
const isSlowEnough = checkGasIsEnough({
token_balance_hex: token?.raw_amount_hex_str,
price: slowLevel?.price || 0,
gasLimit,
});

return {
normalLevel,
isNormalEnough,
isSlowEnough,
slowLevel,
customLevel,
};
}
24 changes: 22 additions & 2 deletions src/ui/views/SendToken/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ import { Chain } from '@debank/common';
import IconAlertInfo from './alert-info.svg';
import { formatTxInputDataOnERC20 } from '@/ui/utils/transaction';
import ThemeIcon from '@/ui/component/ThemeMode/ThemeIcon';
import { customTestnetTokenToTokenItem } from '@/ui/utils/token';
import {
checkIfTokenBalanceEnough,
customTestnetTokenToTokenItem,
} from '@/ui/utils/token';
import { copyAddress } from '@/ui/utils/clipboard';
import { MaxButton } from './components/MaxButton';
import {
Expand Down Expand Up @@ -1320,6 +1323,23 @@ const SendToken = () => {
selectedGasLevel,
]);

useEffect(() => {
if (currentToken && gasList) {
const result = checkIfTokenBalanceEnough(currentToken, {
gasList,
gasLimit: MINIMUM_GAS_LIMIT,
});

if (result.isNormalEnough && result.normalLevel) {
setSelectedGasLevel(result.normalLevel);
} else if (result.isSlowEnough && result.slowLevel) {
setSelectedGasLevel(result.slowLevel);
} else if (result.customLevel) {
setSelectedGasLevel(result.customLevel);
}
}
}, [currentToken, gasList]);

return (
<div className="send-token">
<PageHeader onBack={handleClickBack} forceShowBack>
Expand Down Expand Up @@ -1614,7 +1634,7 @@ const SendToken = () => {
<SendReserveGasPopup
selectedItem={selectedGasLevel?.level as GasLevelType}
chain={chain}
limit={MINIMUM_GAS_LIMIT}
limit={Math.max(estimateGas, MINIMUM_GAS_LIMIT)}
onGasChange={(gasLevel) => {
handleGasLevelChanged(gasLevel);
}}
Expand Down

0 comments on commit 0d1f2a8

Please sign in to comment.