Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bridge-ui): fix use max logic #13898

Merged
merged 7 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -138,7 +138,7 @@
const parsedAmount = ethers.utils.parseUnits(amount, token.decimals);

log(
`Checking allowance for token ${token.symbol} and amount ${parsedAmount}`,

Check warning on line 141 in packages/bridge-ui/src/components/BridgeForm/BridgeForm.svelte

View workflow job for this annotation

GitHub Actions / build

Invalid type "BigNumber" of template literal expression
);

const isRequired = await $activeBridge.requiresAllowance({
Expand Down Expand Up @@ -277,7 +277,7 @@
const hasEnoughBalance = balanceAvailableForTx.gte(requiredGas);

log(
`Is required gas ${requiredGas} less than available balance ${balanceAvailableForTx}? ${hasEnoughBalance}`,

Check warning on line 280 in packages/bridge-ui/src/components/BridgeForm/BridgeForm.svelte

View workflow job for this annotation

GitHub Actions / build

Invalid type "BigNumber" of template literal expression

Check warning on line 280 in packages/bridge-ui/src/components/BridgeForm/BridgeForm.svelte

View workflow job for this annotation

GitHub Actions / build

Invalid type "BigNumber" of template literal expression
);

return hasEnoughBalance;
Expand Down 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