Created
October 22, 2024 13:39
-
-
Save zengzengzenghuy/f3c497970afbafdabc076c37ba92f9eb to your computer and use it in GitHub Desktop.
Mock Oracle for Hashi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { http, createWalletClient, parseAbiItem, publicActions } from "viem"; | |
import { gnosisChiado, sepolia } from "viem/chains"; | |
import { privateKeyToAccount } from "viem/accounts"; | |
const main = async () => { | |
console.log("Start event listener"); | |
// Configuration | |
const sourceChain = sepolia; | |
const destinationChain = gnosisChiado; | |
const account = privateKeyToAccount(""); | |
const REPORTER = ""; // reporter address on source chain | |
const ADAPTER = ""; // adapter address on destination chain | |
const interval = 30000; | |
// 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()) - 10n; | |
let currentBlock; | |
setInterval(async () => { | |
let block = await sourceClient.getBlock(); | |
// let hash = (await block).hash; | |
// Fetching events | |
currentBlock = await sourceClient.getBlockNumber(); | |
console.log("Fetch from ", lastFetched, " to ", currentBlock - 1n); | |
const logs = await sourceClient.getContractEvents({ | |
address: REPORTER, | |
abi: [ | |
parseAbiItem( | |
"event MessageDispatched(uint256 indexed targetChainId,address adapter,uint256 indexed messageId,bytes32 messageHash)" | |
), | |
], | |
eventName: "MessageDispatched", | |
fromBlock: lastFetched, | |
toBlock: currentBlock - 1n, | |
}); | |
lastFetched = currentBlock; | |
console.log("Found", logs.length, "logs"); | |
let messageId; | |
let messageHash; | |
if (logs.length) { | |
console.log(`Found ${logs.length} MessageDispatched event`); | |
for (let i = 0; i < logs.length; i++) { | |
messageId = logs[i].args.messageId; | |
messageHash = logs[i].args.messageHash; | |
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, [messageId], [messageHash]], | |
}); | |
let tx = await destinationClient.writeContract(setHashRequest); | |
console.log("Set hash tx ", tx); | |
} | |
} | |
}, interval); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment