Skip to content

Commit

Permalink
fix useFullAmount function
Browse files Browse the repository at this point in the history
  • Loading branch information
jscriptcoder committed Jun 6, 2023
1 parent fbed474 commit 755df20
Showing 1 changed file with 58 additions and 35 deletions.
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

0 comments on commit 755df20

Please sign in to comment.