Skip to content

Commit

Permalink
chore: fix fee estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
foodaka committed Oct 16, 2024
1 parent e4a1703 commit 728b8c6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
43 changes: 34 additions & 9 deletions src/components/transactions/Bridge/BridgeModalContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const feeTokens = [
extensions: {
isNative: false,
},
balance: '0',
},
{
name: 'Gho Token',
Expand All @@ -82,6 +83,7 @@ const feeTokens = [
extensions: {
isNative: false,
},
balance: '0',
},
{
name: 'Ethereum',
Expand All @@ -94,6 +96,7 @@ const feeTokens = [
extensions: {
isNative: true,
},
balance: '0',
},
{
name: 'Ethereum',
Expand All @@ -106,6 +109,7 @@ const feeTokens = [
extensions: {
isNative: true,
},
balance: '0',
},
];

Expand Down Expand Up @@ -138,22 +142,31 @@ export const BridgeModalContent = () => {
(token) => token.chainId === sourceNetworkObj.chainId
);

const { data: baseTokenList } = useTokensBalance(
const { data: feeTokenListWithBalance } = useTokensBalance(
filteredFeeTokensByChainId,
sourceNetworkObj.chainId,
user
);

const [selectedFeeToken, setSelectedFeeToken] = useState(filteredFeeTokensByChainId[0]);
const [selectedFeeToken, setSelectedFeeToken] = useState(
feeTokenListWithBalance?.[0] || filteredFeeTokensByChainId[0]
);
const handleTokenChange = (event: SelectChangeEvent) => {
const token = filteredFeeTokensByChainId.find((token) => token.address === event.target.value);
const token = feeTokenListWithBalance?.find((token) => token.address === event.target.value);

if (token) {
setSelectedFeeToken(token);
} else {
setSelectedFeeToken(filteredFeeTokensByChainId[0]);
}
};

useEffect(() => {
if (feeTokenListWithBalance && feeTokenListWithBalance.length > 0 && !selectedFeeToken) {
setSelectedFeeToken(feeTokenListWithBalance[0]);
}
}, [feeTokenListWithBalance, selectedFeeToken]);

useEffect(() => {
// reset when source network changes
setAmount('');
Expand All @@ -180,8 +193,8 @@ export const BridgeModalContent = () => {
amount,
sourceTokenAddress: sourceTokenInfo?.address || '',
destinationAccount,
feeToken: selectedFeeToken.address,
feeTokenOracle: selectedFeeToken.oracle as string,
feeToken: selectedFeeToken?.address || '',
feeTokenOracle: selectedFeeToken?.oracle || ('' as string),
});

const { data: bridgeLimits, isFetching: fetchingBridgeLimits } = useGetBridgeLimit(
Expand Down Expand Up @@ -531,8 +544,8 @@ export const BridgeModalContent = () => {
},
}}
>
{baseTokenList &&
baseTokenList.map((token) => (
{feeTokenListWithBalance &&
feeTokenListWithBalance.map((token) => (
<MenuItem
sx={{ background: 'white' }}
key={token.address}
Expand Down Expand Up @@ -561,10 +574,10 @@ export const BridgeModalContent = () => {
) : (
<Stack direction="column" alignItems="flex-end" position="relative">
<Stack direction="row" alignItems="center">
<TokenIcon symbol={selectedFeeToken.symbol} sx={{ mr: 1, fontSize: '16px' }} />
<TokenIcon symbol={selectedFeeToken?.symbol} sx={{ mr: 1, fontSize: '16px' }} />
<FormattedNumber
value={bridgeFeeFormatted}
symbol={selectedFeeToken.symbol}
symbol={selectedFeeToken?.symbol}
variant="secondary14"
/>
</Stack>
Expand Down Expand Up @@ -616,6 +629,18 @@ export const BridgeModalContent = () => {
</Typography>
</Warning>
)}
{selectedFeeToken && parseFloat(selectedFeeToken.balance) < 0 && (
<Warning severity="warning" sx={{ my: 2 }}>
<Stack direction="row">
<Typography variant="caption">
<Trans>
You do not have a balance to pay for CCIP fee with your {selectedFeeToken.name},
try changing to another token.
</Trans>
</Typography>
</Stack>
</Warning>
)}

<BridgeActions {...bridgeActionsProps} />
</>
Expand Down
5 changes: 3 additions & 2 deletions src/components/transactions/Bridge/useGetBridgeMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const useGetBridgeMessage = ({
let transactionCostUsd;

if (feeToken === constants.AddressZero) {
console.log('fee token ETH');
// Handling for Ether (native token)
const sourceLaneConfig = laneConfig.find(
(config) => config.sourceChainId === sourceChainId
Expand Down Expand Up @@ -129,7 +130,7 @@ export const useGetBridgeMessage = ({
setLoading(false);
}
}, 500);
}, [amount, destinationChainId, sourceChainId, sourceTokenAddress, destinationAccount]);
}, [amount, destinationChainId, sourceChainId, sourceTokenAddress, destinationAccount, feeToken]);

useEffect(() => {
if (amount && sourceTokenAddress) {
Expand All @@ -145,7 +146,7 @@ export const useGetBridgeMessage = ({
return () => {
debounced.cancel();
};
}, [amount, debounced, sourceTokenAddress]);
}, [amount, debounced, sourceTokenAddress, feeToken]);

return {
message,
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/generic/useTokensBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import { getProvider } from 'src/utils/marketsAndNetworksConfig';

export interface TokenInfoWithBalance extends TokenInfo {
balance: string;
oracle?: string;
}

export const useTokensBalance = (tokenList: TokenInfo[], chainId: number, user: string) => {
console.log('do I have the setup', tokenList, chainId, user);

return useQuery<TokenInfoWithBalance[]>({
queryKey: queryKeysFactory.tokensBalance(tokenList, chainId, user),
enabled: tokenList.length > 0,
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en/messages.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/locales/en/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -3221,6 +3221,10 @@ msgstr "You cancelled the transaction."
msgid "You did not participate in this proposal"
msgstr "You did not participate in this proposal"

#: src/components/transactions/Bridge/BridgeModalContent.tsx
msgid "You do not have a balance to pay for CCIP fee with your {0}, try changing to another token."
msgstr "You do not have a balance to pay for CCIP fee with your {0}, try changing to another token."

#: src/components/transactions/CollateralChange/CollateralChangeModalContent.tsx
msgid "You do not have supplies in this currency"
msgstr "You do not have supplies in this currency"
Expand Down

0 comments on commit 728b8c6

Please sign in to comment.