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
Changes from 2 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
93 changes: 58 additions & 35 deletions packages/bridge-ui/src/components/BridgeForm/BridgeForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -445,44 +445,67 @@
}

async function useFullAmount() {
if (isETH($token)) {
try {
const feeData = await fetchFeeData();
const gasEstimate = await $activeBridge.estimateGas({
amount: BigNumber.from(1),
signer: $signer,
tokenAddress: await getAddressForToken(
$token,
$srcChain,
$destChain,
$signer,
),
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 balanceAvailableForTx = userBalance.sub(requiredGas);

if (processingFee) {
balanceAvailableForTx = balanceAvailableForTx.sub(processingFee);
}
try {
const feeData = await fetchFeeData();
const processingFeeInWei = getProcessingFee();

const bridgeAddress = chains[$srcChain.id].bridgeAddress; // for ETH
const tokenVaultAddress = tokenVaults[$srcChain.id]; // for ERC20

amount = ethers.utils.formatEther(balanceAvailableForTx);
} catch (error) {
console.error(error);
const toAddress = showTo && to ? to : await $signer.getAddress();

// 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.
amount = tokenBalance.toString();
// For ETH this function returns '0x00'
const tokenAddress = await getAddressForToken(
$token,
$srcChain,
$destChain,
$signer,
);

// Whatever amount just to get an estimation
const bnAmount = BigNumber.from(1);

// TODO: different arguments depending on the type of bridge
const gasEstimate = await $activeBridge.estimateGas({
memo,
tokenAddress,
bridgeAddress,
tokenVaultAddress,
processingFeeInWei,
to: toAddress,
signer: $signer,
amount: bnAmount,
srcChainId: $srcChain.id,
destChainId: $destChain.id,
});

const requiredGas = gasEstimate.mul(feeData.gasPrice);

// We might have decimals. BigNumber can't take strings
// with decimals. E.g. BigNumber.from('1.0') will throw an error.
// We can get a BigNumber by parsing, passing the right amount
// of decimals: ethers.utils.parseUnits('1.0', decimals)
const userBalance = ethers.utils.parseUnits(
tokenBalance,
$token.decimals || 18, // defaults to 18 decimals
);

// 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);
}
} else {

// We convert back to user readable string: `1.0`
amount = ethers.utils.formatUnits(balanceAvailableForTx, $token.decimals);
} catch (error) {
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.
amount = tokenBalance.toString();
}
}
Expand Down