Skip to content

Instantly share code, notes, and snippets.

@zengzengzenghuy
Last active November 26, 2024 15:24
Show Gist options
  • Select an option

  • Save zengzengzenghuy/45d87a01847ff8714e84644180c9ea3c to your computer and use it in GitHub Desktop.

Select an option

Save zengzengzenghuy/45d87a01847ff8714e84644180c9ea3c to your computer and use it in GitHub Desktop.
Mock Adapter for Safe cross chain
import { http, createWalletClient, parseAbiItem, publicActions } from "viem";
import { gnosis, sepolia } from "viem/chains";
import { privateKeyToAccount } from "viem/accounts";
const main = async () => {
console.log("Start event listener");
// Configuration
const sourceChain = sepolia;
const destinationChain = gnosis;
const account = privateKeyToAccount(
""
);
const PERIPHERAL = ""; // PERIPHERAL address on source chain
const ADAPTER = "0xF2168b3f225eDFf9F2B5D23bE5931038662Cc5F3"; // adapter address on Gnosis chain
const interval = 60000; // in ms
// Create client
const sourceClient = createWalletClient({
account,
chain: sourceChain,
transport: http(),
}).extend(publicActions);
const destinationClient = createWalletClient({
account,
chain: destinationChain,
transport: http(),
}).extend(publicActions);
let lastFetched = (await sourceClient.getBlockNumber()) - 50n;
let currentBlock;
setInterval(async () => {
// Fetching events
currentBlock = await sourceClient.getBlockNumber();
console.log(
"Fetching Operation event on PERIPHERAL contract from block ",
lastFetched,
" to block ",
currentBlock
);
let isBlockRangeMismatch = lastFetched < currentBlock ? false : true;
if (isBlockRangeMismatch) {
// swap if fromBlock > toBlock
let temp = lastFetched;
lastFetched = currentBlock;
currentBlock = temp;
}
const logs = await sourceClient.getContractEvents({
address: PERIPHERAL,
abi: [
parseAbiItem(
"event Operation(uint256 nonce, address safe, bytes data)"
),
],
eventName: "Operation",
fromBlock: lastFetched,
toBlock: currentBlock,
});
lastFetched = currentBlock;
console.log("Found", logs.length, "logs");
if (logs.length) {
console.log(`Found ${logs.length} Operation event`);
for (let i = 0; i < logs.length; i++) {
let blockNumber = logs[i].blockNumber;
let blockHash = logs[i].blockHash;
const { request: setHashRequest } =
await destinationClient.simulateContract({
account,
abi: [
parseAbiItem(
"function setHashes(uint256 domain, uint256[] memory ids, bytes32[] memory hashes) external"
),
],
address: ADAPTER,
functionName: "setHashes",
args: [sourceChain.id, [blockNumber], [blockHash]],
});
let tx = await destinationClient.writeContract(setHashRequest);
console.log(`Set hash for block ${blockNumber}: ${tx}`);
}
}
}, interval);
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment