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: diff --git a/src/components/LockupEvm.tsx b/src/components/LockupEvm.tsx index 5a7422a2..77e81973 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,9 @@ 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 lockupGasUsage = 46_000n; const InsufficientBalance = () => { const { t } = useGlobalContext(); @@ -37,7 +41,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 +49,68 @@ const LockupEvm = (props: { return; } - setSignerBalance( - await signer().provider.getBalance(await signer().getAddress()), - ); + 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 ( 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} + /> + ); }; 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; };