Skip to content

Commit

Permalink
refactor: stricter EVM signer balance check
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Nov 9, 2024
1 parent 50ed8b7 commit 5a854d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/components/LockupEvm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 (
Expand Down
10 changes: 8 additions & 2 deletions src/context/Web3.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
};

Expand Down

0 comments on commit 5a854d4

Please sign in to comment.