Skip to content

Commit

Permalink
feat: use custom RPC provider for log scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Aug 14, 2024
1 parent 10e9419 commit 4adcbd3
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ tags
.idea/
.vscode/

.env
*.log
dist/
ts-out/
Expand Down
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
"@babel/preset-typescript",
],
plugins: [
"babel-plugin-transform-vite-meta-env",
[
"@babel/plugin-transform-modules-commonjs",
{ allowTopLevelThis: true },
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/node": "^22.0.2",
"@webbtc/webln-types": "^3.0.0",
"babel-jest": "^29.7.0",
"babel-plugin-transform-vite-meta-env": "^1.0.3",
"babel-preset-jest": "^29.6.3",
"babel-preset-solid": "^1.8.18",
"eventsource": "^2.0.2",
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Asset = {

rifRelay?: string;
contracts?: {
deployHeight: number;
smartWalletFactory?: string;
deployVerifier?: string;
};
Expand Down
1 change: 1 addition & 0 deletions src/configs/regtest.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"rifRelay": "http://localhost:8090",
"contracts": {
"deployHeight": 0,
"smartWalletFactory": "0x59b670e9fA9D0A427751Af201D676719a970857b",
"deployVerifier": "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9"
}
Expand Down
1 change: 1 addition & 0 deletions src/configs/testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"rifRelay": "https://boltz.testnet.relay.rifcomputing.net",
"contracts": {
"deployHeight": 4781682,
"smartWalletFactory": "0x82bc3558863b3f0C9914539DbB2d143AfB9c8768",
"deployVerifier": "0x5e8F98ddAd4Da6eE8A8eA3D64E09385dF6b609D0"
}
Expand Down
2 changes: 1 addition & 1 deletion src/context/Web3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,4 @@ const Web3SignerProvider = (props: {

const useWeb3Signer = () => useContext(Web3SignerContext);

export { useWeb3Signer, Web3SignerProvider };
export { useWeb3Signer, Web3SignerProvider, EtherSwapAbi };
40 changes: 35 additions & 5 deletions src/utils/contractLogs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import type { EtherSwap } from "boltz-core/typechain/EtherSwap";
import { Result, Signer, solidityPackedKeccak256 } from "ethers";
import {
Contract,
JsonRpcProvider,
Result,
Signer,
solidityPackedKeccak256,
} from "ethers";
import log from "loglevel";

import { config } from "../config";
import { AssetType, RBTC } from "../consts/Assets";
import { EtherSwapAbi } from "../context/Web3";
import { weiToSatoshi } from "./rootstock";

const scanInterval = 10_000;
const scanInterval = 2_000;

export type LogRefundData = {
asset: AssetType;
Expand Down Expand Up @@ -48,19 +56,41 @@ async function* scanLogsForPossibleRefunds(
signer.getAddress(),
signer.provider.getBlockNumber(),
]);
log.info(`Scanning for possible refunds of: ${signerAddress}`);

const deployHeight = config.assets[RBTC].contracts.deployHeight;
const filter = etherSwap.filters.Lockup(null, null, null, signerAddress);

for (let toBlock = latestBlock; toBlock >= 0; toBlock -= scanInterval) {
log.info(
`Scanning for possible refunds of ${signerAddress} from ${deployHeight} to ${latestBlock}`,
);

const scanProviderUrl = import.meta.env.VITE_RSK_LOG_SCAN_ENDPOINT;
const etherSwapScan =
scanProviderUrl !== undefined
? (new Contract(
await etherSwap.getAddress(),
EtherSwapAbi,
new JsonRpcProvider(scanProviderUrl),
) as unknown as EtherSwap)
: etherSwap;

for (
let toBlock = latestBlock;
toBlock >= deployHeight;
toBlock -= scanInterval
) {
if (abortSignal.aborted) {
log.info(`Cancelling refund log scan of: ${signerAddress}`);
return;
}

const fromBlock = Math.max(toBlock - scanInterval, 0);
log.debug(`Scanning possible refunds from ${fromBlock} to ${toBlock}`);
const events = await etherSwap.queryFilter(filter, fromBlock, toBlock);
const events = await etherSwapScan.queryFilter(
filter,
fromBlock,
toBlock,
);

const results: LogRefundData[] = [];

Expand Down

0 comments on commit 4adcbd3

Please sign in to comment.