Skip to content

Commit

Permalink
fix(bridge-ui): fix use max logic (#13898)
Browse files Browse the repository at this point in the history
  • Loading branch information
jscriptcoder committed Jun 6, 2023
1 parent 2e243fb commit 1abc3ff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
39 changes: 26 additions & 13 deletions packages/bridge-ui/src/components/BridgeForm/BridgeForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -448,28 +448,40 @@
if (isETH($token)) {
try {
const feeData = await fetchFeeData();
const processingFeeInWei = getProcessingFee();
const bridgeAddress = chains[$srcChain.id].bridgeAddress;
const toAddress = showTo && to ? to : await $signer.getAddress();
// Won't be used in ETHBridge.estimateGas()
// TODO: different arguments depending on the type of bridge
const tokenVaultAddress = '0x00';
const tokenAddress = '0x00';
// Whatever amount just to get an estimation
const bnAmount = BigNumber.from(1);
const gasEstimate = await $activeBridge.estimateGas({
amount: BigNumber.from(1),
memo,
tokenAddress,
bridgeAddress,
tokenVaultAddress,
processingFeeInWei,
to: toAddress,
signer: $signer,
tokenAddress: await getAddressForToken(
$token,
$srcChain,
$destChain,
$signer,
),
amount: bnAmount,
srcChainId: $srcChain.id,
destChainId: $destChain.id,
tokenVaultAddress: tokenVaults[$srcChain.id],
processingFeeInWei: getProcessingFee(),
memo: memo,
to: showTo && to ? to : await $signer.getAddress(),
});
const requiredGas = gasEstimate.mul(feeData.gasPrice);
const userBalance = await $signer.getBalance('latest');
const processingFee = getProcessingFee();
// Let's start with substracting the estimated required gas to bridge
let balanceAvailableForTx = userBalance.sub(requiredGas);
// Following we substract the currently selected processing fee
const processingFee = getProcessingFee();
if (processingFee) {
balanceAvailableForTx = balanceAvailableForTx.sub(processingFee);
}
Expand All @@ -479,7 +491,8 @@
console.error(error);
// In case of error default to using the full amount of ETH available.
// The user would still not be able to make the restriction and will have to manually set the amount.
// The user would still not be able to make the restriction and will have to
// manually set the amount.
amount = tokenBalance.toString();
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/bridge-ui/src/pages/home/Home.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<div class="rounded-lg py-4 flex flex-col items-center justify-center">
<SelectChain />
</div>
<div class="md:w-[440px] px-4 flex flex-col items-center justify-center">
<div class="md:w-[440px] px-4">
<BridgeForm />
</div>
</TabPanel>
Expand All @@ -73,7 +73,7 @@
</TabPanel>

<TabPanel tab={tab3.name}>
<div class="md:w-[440px] px-4 flex flex-col items-center justify-center">
<div class="md:w-[440px] px-4">
<Faucet />
</div>
</TabPanel>
Expand Down

0 comments on commit 1abc3ff

Please sign in to comment.