Skip to content

Commit

Permalink
Merge pull request #731 from BoltzExchange/rsk-fixes
Browse files Browse the repository at this point in the history
RSK fixes
  • Loading branch information
michael1011 authored Nov 10, 2024
2 parents f62ef91 + 5a854d4 commit f5ebe1f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 58 deletions.
35 changes: 19 additions & 16 deletions src/components/CreateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
101 changes: 61 additions & 40 deletions src/components/LockupEvm.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import log from "loglevel";
import { Show, createEffect, createSignal } from "solid-js";

import { useGlobalContext } from "../context/Global";
Expand All @@ -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();
Expand Down Expand Up @@ -37,59 +41,76 @@ const LockupEvm = (props: {

const value = () => satoshiToWei(props.amount);

const [signerBalance, setSignerBalance] = createSignal<bigint>(0n);
const [signerBalance, setSignerBalance] = createSignal<bigint>(undefined);

// eslint-disable-next-line solid/reactivity
createEffect(async () => {
if (signer() === undefined) {
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 (
<Show
when={signer() === undefined || signerBalance() > value()}
fallback={<InsufficientBalance />}>
<ContractTransaction
/* eslint-disable-next-line solid/reactivity */
onClick={async () => {
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={
<Show
when={signer() !== undefined}
fallback={<ConnectWallet />}>
<LoadingSpinner />
</Show>
}>
<Show
when={signer() === undefined || signerBalance() > value()}
fallback={<InsufficientBalance />}>
<ContractTransaction
/* eslint-disable-next-line solid/reactivity */
onClick={async () => {
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={<ConnectWallet />}
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={<ConnectWallet />}
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}
/>
</Show>
</Show>
);
};
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 f5ebe1f

Please sign in to comment.