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): max amount button should deduct gas #12661

Merged
merged 7 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 65 additions & 4 deletions packages/bridge-ui/src/components/form/BridgeForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import { MessageStatus } from "../../domain/message";
import { Funnel } from "svelte-heros-v2";
import FaucetModal from "../modals/FaucetModal.svelte";
import { fetchFeeData } from "@wagmi/core";

let amount: string;
let amountInput: HTMLInputElement;
Expand Down Expand Up @@ -177,13 +178,27 @@
}
}

async function checkUserHasEnoughBalance(gasEstimate: BigNumber) {
const feeData = await fetchFeeData();
const requiredGas = gasEstimate.mul(feeData.gasPrice);
const userBalance = await $signer.getBalance("latest");

let balanceAvailableForTx = userBalance;

if($token.symbol === ETH.symbol) {
balanceAvailableForTx = userBalance.sub(ethers.utils.parseEther(amount));
}

return balanceAvailableForTx.gte(requiredGas);
}

async function bridge() {
try {
loading = true;
if (requiresAllowance) throw Error("requires additional allowance");

const amountInWei = ethers.utils.parseUnits(amount, $token.decimals);
const tx = await $activeBridge.Bridge({
const bridgeOpts = {
amountInWei: amountInWei,
signer: $signer,
tokenAddress: await addrForToken(),
Expand All @@ -192,7 +207,20 @@
tokenVaultAddress: $chainIdToTokenVaultAddress.get($fromChain.id),
processingFeeInWei: getProcessingFee(),
memo: memo,
};

const gasEstimate = await $activeBridge.EstimateGas({
...bridgeOpts,
amountInWei: BigNumber.from(1),
});
const doesUserHaveEnoughBalance = await checkUserHasEnoughBalance(gasEstimate)

if(!doesUserHaveEnoughBalance) {
errorToast("Insufficient ETH balance");
return;
}

const tx = await $activeBridge.Bridge(bridgeOpts);

// tx.chainId is not set immediately but we need it later. set it
// manually.
Expand Down Expand Up @@ -243,9 +271,42 @@
}
}

function useFullAmount() {
amount = tokenBalance;
amountInput.value = tokenBalance.toString();
async function useFullAmount() {
if($token.symbol === ETH.symbol) {
try {
const feeData = await fetchFeeData();
const gasEstimate = await $activeBridge.EstimateGas({
amountInWei: BigNumber.from(1),
cyberhorsey marked this conversation as resolved.
Show resolved Hide resolved
signer: $signer,
tokenAddress: await addrForToken(),
fromChainId: $fromChain.id,
toChainId: $toChain.id,
tokenVaultAddress: $chainIdToTokenVaultAddress.get($fromChain.id),
processingFeeInWei: getProcessingFee(),
memo: memo,
});
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);
}

amount = ethers.utils.formatEther(balanceAvailableForTx);
amountInput.value = ethers.utils.formatEther(balanceAvailableForTx);
} catch (error) {
console.log(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;
amountInput.value = tokenBalance.toString();
}
} else {
amount = tokenBalance;
amountInput.value = tokenBalance.toString();
}
}

function updateAmount(e: any) {
Expand Down
1 change: 1 addition & 0 deletions packages/bridge-ui/src/domain/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface Bridge {
RequiresAllowance(opts: ApproveOpts): Promise<boolean>;
Approve(opts: ApproveOpts): Promise<Transaction>;
Bridge(opts: BridgeOpts): Promise<Transaction>;
EstimateGas(opts: BridgeOpts): Promise<BigNumber>;
cyberhorsey marked this conversation as resolved.
Show resolved Hide resolved
Claim(opts: ClaimOpts): Promise<Transaction>;
}

Expand Down
71 changes: 49 additions & 22 deletions packages/bridge-ui/src/erc20/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@ class ERC20Bridge implements Bridge {
this.prover = prover;
}

static async prepareTransaction(opts: BridgeOpts) {
const contract: Contract = new Contract(
opts.tokenVaultAddress,
TokenVault,
opts.signer
);

const owner = await opts.signer.getAddress();
const message = {
sender: owner,
srcChainId: opts.fromChainId,
destChainId: opts.toChainId,
owner: owner,
to: owner,
refundAddress: owner,
depositValue: opts.amountInWei,
callValue: 0,
processingFee: opts.processingFeeInWei ?? BigNumber.from(0),
gasLimit: opts.processingFeeInWei
? BigNumber.from(100000)
: BigNumber.from(0),
memo: opts.memo ?? "",
};

return { contract, owner, message };
}

private async spenderRequiresAllowance(
tokenAddress: string,
signer: Signer,
Expand Down Expand Up @@ -76,28 +103,7 @@ class ERC20Bridge implements Bridge {
throw Error("token vault does not have required allowance");
}

const contract: Contract = new Contract(
opts.tokenVaultAddress,
TokenVault,
opts.signer
);

const owner = await opts.signer.getAddress();
const message = {
sender: owner,
srcChainId: opts.fromChainId,
destChainId: opts.toChainId,
owner: owner,
to: owner,
refundAddress: owner,
depositValue: opts.amountInWei,
callValue: 0,
processingFee: opts.processingFeeInWei ?? BigNumber.from(0),
gasLimit: opts.processingFeeInWei
? BigNumber.from(100000)
: BigNumber.from(0),
memo: opts.memo ?? "",
};
const { contract, owner, message } = await ERC20Bridge.prepareTransaction(opts);

const tx = await contract.sendERC20(
message.destChainId,
Expand All @@ -116,6 +122,27 @@ class ERC20Bridge implements Bridge {
return tx;
}

async EstimateGas(opts: BridgeOpts): Promise<BigNumber> {

const { contract, owner, message } = await ERC20Bridge.prepareTransaction(opts);

const gasEstimate = await contract.estimateGas.sendERC20(
message.destChainId,
owner,
opts.tokenAddress,
opts.amountInWei,
message.gasLimit,
message.processingFee,
message.refundAddress,
message.memo,
{
value: message.processingFee.add(message.callValue),
}
);

return gasEstimate;
}

async Claim(opts: ClaimOpts): Promise<Transaction> {
const contract: Contract = new Contract(
opts.destBridgeAddress,
Expand Down
46 changes: 36 additions & 10 deletions packages/bridge-ui/src/eth/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,7 @@ class ETHBridge implements BridgeInterface {
this.prover = prover;
}

RequiresAllowance(opts: ApproveOpts): Promise<boolean> {
return Promise.resolve(false);
}

// ETH does not need to be approved for transacting
Approve(opts: ApproveOpts): Promise<Transaction> {
return new Promise((resolve) => resolve({} as unknown as Transaction));
}

async Bridge(opts: BridgeOpts): Promise<Transaction> {
static async prepareTransaction(opts: BridgeOpts): Promise<{contract: Contract, message: any, owner: string}> {
const contract: Contract = new Contract(
opts.tokenVaultAddress,
TokenVault,
Expand All @@ -52,6 +43,21 @@ class ETHBridge implements BridgeInterface {
memo: opts.memo ?? "",
};

return { contract, owner, message };
}

RequiresAllowance(opts: ApproveOpts): Promise<boolean> {
return Promise.resolve(false);
}

// ETH does not need to be approved for transacting
Approve(opts: ApproveOpts): Promise<Transaction> {
return new Promise((resolve) => resolve({} as unknown as Transaction));
}

async Bridge(opts: BridgeOpts): Promise<Transaction> {
const { contract, owner, message } = await ETHBridge.prepareTransaction(opts);

const tx = await contract.sendEther(
message.destChainId,
owner,
Expand All @@ -69,6 +75,26 @@ class ETHBridge implements BridgeInterface {
return tx;
}

async EstimateGas(opts: BridgeOpts): Promise<BigNumber> {
const { contract, owner, message } = await ETHBridge.prepareTransaction(opts);

const gasEstimate = await contract.estimateGas.sendEther(
message.destChainId,
owner,
message.gasLimit,
message.processingFee,
message.refundAddress,
message.memo,
{
value: message.depositValue
.add(message.processingFee)
.add(message.callValue),
}
);

return gasEstimate;
}

async Claim(opts: ClaimOpts): Promise<Transaction> {
const contract: Contract = new Contract(
opts.destBridgeAddress,
Expand Down