From 5a854d473dd462cbf36b528fcd3fde0ae615738a Mon Sep 17 00:00:00 2001 From: michael1011 Date: Sat, 9 Nov 2024 17:10:25 +0100 Subject: [PATCH] refactor: stricter EVM signer balance check --- src/components/LockupEvm.tsx | 17 ++++++++++++----- src/context/Web3.tsx | 10 ++++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/components/LockupEvm.tsx b/src/components/LockupEvm.tsx index e5ec14e5..77e81973 100644 --- a/src/components/LockupEvm.tsx +++ b/src/components/LockupEvm.tsx @@ -10,6 +10,8 @@ import ConnectWallet from "./ConnectWallet"; import ContractTransaction from "./ContractTransaction"; import LoadingSpinner from "./LoadingSpinner"; +const lockupGasUsage = 46_000n; + const InsufficientBalance = () => { const { t } = useGlobalContext(); @@ -47,11 +49,16 @@ const LockupEvm = (props: { return; } - const balance = await signer().provider.getBalance( - await signer().getAddress(), - ); - log.info("EVM signer balance", balance); - setSignerBalance(balance); + const [balance, gasPrice] = await Promise.all([ + signer().provider.getBalance(await signer().getAddress()), + signer() + .provider.getFeeData() + .then((data) => data.gasPrice), + ]); + + const spendable = balance - gasPrice * lockupGasUsage; + log.info("EVM signer spendable balance", spendable); + setSignerBalance(spendable); }); return ( diff --git a/src/context/Web3.tsx b/src/context/Web3.tsx index fcc18157..263a1837 100644 --- a/src/context/Web3.tsx +++ b/src/context/Web3.tsx @@ -1,6 +1,11 @@ import { abi as EtherSwapAbi } from "boltz-core/out/EtherSwap.sol/EtherSwap.json"; import { EtherSwap } from "boltz-core/typechain/EtherSwap"; -import { BrowserProvider, Contract, JsonRpcSigner } from "ethers"; +import { + BrowserProvider, + Contract, + JsonRpcProvider, + JsonRpcSigner, +} from "ethers"; import log from "loglevel"; import { Accessor, @@ -160,7 +165,8 @@ const Web3SignerProvider = (props: { return new Contract( contracts().swapContracts.EtherSwap, EtherSwapAbi, - signer(), + signer() || + new JsonRpcProvider(config.assets["RBTC"]?.network?.rpcUrls[0]), ) as unknown as EtherSwap; };