From 957f43cfb2875bb6ba13b16b2689737e6f837269 Mon Sep 17 00:00:00 2001 From: michael1011 Date: Sat, 9 Nov 2024 13:53:16 +0100 Subject: [PATCH 1/3] feat: loading spinner when fetching signer balance --- src/components/LockupEvm.tsx | 92 +++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/src/components/LockupEvm.tsx b/src/components/LockupEvm.tsx index 5a7422a2..e5ec14e5 100644 --- a/src/components/LockupEvm.tsx +++ b/src/components/LockupEvm.tsx @@ -1,3 +1,4 @@ +import log from "loglevel"; import { Show, createEffect, createSignal } from "solid-js"; import { useGlobalContext } from "../context/Global"; @@ -7,6 +8,7 @@ import { HardwareSigner } from "../utils/hardware/HadwareSigner"; import { prefix0x, satoshiToWei } from "../utils/rootstock"; import ConnectWallet from "./ConnectWallet"; import ContractTransaction from "./ContractTransaction"; +import LoadingSpinner from "./LoadingSpinner"; const InsufficientBalance = () => { const { t } = useGlobalContext(); @@ -37,7 +39,7 @@ const LockupEvm = (props: { const value = () => satoshiToWei(props.amount); - const [signerBalance, setSignerBalance] = createSignal(0n); + const [signerBalance, setSignerBalance] = createSignal(undefined); // eslint-disable-next-line solid/reactivity createEffect(async () => { @@ -45,51 +47,63 @@ const LockupEvm = (props: { return; } - setSignerBalance( - await signer().provider.getBalance(await signer().getAddress()), + const balance = await signer().provider.getBalance( + await signer().getAddress(), ); + log.info("EVM signer balance", balance); + setSignerBalance(balance); }); return ( value()} - fallback={}> - { - const contract = getEtherSwap(); - const tx = await contract.lock( - prefix0x(props.preimageHash), - props.claimAddress, - props.timeoutBlockHeight, - { - value: value(), - }, - ); - const currentSwap = await getSwap(props.swapId); - currentSwap.lockupTx = tx.hash; - currentSwap.signer = signer().address; + when={signerBalance() !== undefined} + fallback={ + }> + + + }> + value()} + fallback={}> + { + const contract = getEtherSwap(); + const tx = await contract.lock( + prefix0x(props.preimageHash), + props.claimAddress, + props.timeoutBlockHeight, + { + value: value(), + }, + ); + const currentSwap = await getSwap(props.swapId); + currentSwap.lockupTx = tx.hash; + currentSwap.signer = signer().address; - if (customDerivationPathRdns.includes(signer().rdns)) { - currentSwap.derivationPath = ( - providers()[signer().rdns] - .provider as unknown as HardwareSigner - ).getDerivationPath(); - } + if (customDerivationPathRdns.includes(signer().rdns)) { + currentSwap.derivationPath = ( + providers()[signer().rdns] + .provider as unknown as HardwareSigner + ).getDerivationPath(); + } - setSwap(currentSwap); - await setSwapStorage(currentSwap); - }} - children={} - address={{ - address: props.signerAddress, - derivationPath: props.derivationPath, - }} - buttonText={t("send")} - promptText={t("transaction_prompt", { button: t("send") })} - waitingText={t("tx_in_mempool_subline")} - showHr={false} - /> + setSwap(currentSwap); + await setSwapStorage(currentSwap); + }} + children={} + address={{ + address: props.signerAddress, + derivationPath: props.derivationPath, + }} + buttonText={t("send")} + promptText={t("transaction_prompt", { button: t("send") })} + waitingText={t("tx_in_mempool_subline")} + showHr={false} + /> + ); }; From 50ed8b78ca79e3801a082392258d1652f3816104 Mon Sep 17 00:00:00 2001 From: michael1011 Date: Sat, 9 Nov 2024 13:56:48 +0100 Subject: [PATCH 2/3] fix: catch EVM provider error in create --- src/components/CreateButton.tsx | 35 ++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/components/CreateButton.tsx b/src/components/CreateButton.tsx index 9195a9c8..242d74c7 100644 --- a/src/components/CreateButton.tsx +++ b/src/components/CreateButton.tsx @@ -250,27 +250,30 @@ export const CreateButton = () => { let claimAddress = onchainAddress(); - if (assetReceive() === RBTC) { - const [balance, gasPrice] = await Promise.all([ - signer().provider.getBalance(await signer().getAddress()), - signer() - .provider.getFeeData() - .then((data) => data.gasPrice), - ]); - log.debug("RSK balance", balance); + try { + if (assetReceive() === RBTC) { + const [balance, gasPrice] = await Promise.all([ + signer().provider.getBalance(await signer().getAddress()), + signer() + .provider.getFeeData() + .then((data) => data.gasPrice), + ]); + log.debug("RSK balance", balance); - const balanceNeeded = gasPrice * GasNeededToClaim; - log.debug("RSK balance needed", balanceNeeded); + const balanceNeeded = gasPrice * GasNeededToClaim; + log.debug("RSK balance needed", balanceNeeded); - if (balance <= balanceNeeded) { - claimAddress = (await getSmartWalletAddress(signer())).address; - log.info("Using RIF smart wallet as claim address"); + if (balance <= balanceNeeded) { + claimAddress = (await getSmartWalletAddress(signer())) + .address; + log.info("Using RIF smart wallet as claim address"); + } else { + log.info("RIF smart wallet not needed"); + } } - } - const useRif = onchainAddress() !== claimAddress; + const useRif = onchainAddress() !== claimAddress; - try { let data: SomeSwap; switch (swapType()) { case SwapType.Submarine: From 5a854d473dd462cbf36b528fcd3fde0ae615738a Mon Sep 17 00:00:00 2001 From: michael1011 Date: Sat, 9 Nov 2024 17:10:25 +0100 Subject: [PATCH 3/3] 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; };