How to Retrieve Transaction History and Logs for a Deployed Smart Contract on Local Devnet Using Hardhat? #604
Replies: 2 comments
-
if you are using hardhat(I don't know how you did that cause the deploy script is all under foundry modules), you can use web3.js/ ethers.js to subscribe the log and set the filter rules to get the data you want. |
Beta Was this translation helpful? Give feedback.
-
You need to use getLogs For ethers import { ethers } from 'ethers';
// Connect to an local devnet RPC
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545');
// The contract address you want to query logs from
const contractAddress = '0xYourContractAddress';
// The event/topic hash (use a specific event signature)
const eventSignature = ethers.id('Transfer(address,address,uint256)');
// Define the filter object
const filter = {
address: contractAddress, // Address of the contract
topics: [eventSignature], // Event signature or topic hash
fromBlock: 1, // Block number to start from (optional)
toBlock: 'latest', // Block number to end at (optional)
};
// Fetch logs from the provider
async function fetchLogs() {
try {
const logs = await provider.getLogs(filter);
console.log('Logs:', logs);
} catch (error) {
console.error('Error fetching logs:', error);
}
}
fetchLogs(); Viem import { createPublicClient, http, parseAbiItem } from 'viem';
import { hardhat } from 'viem/chains';
// Create a client to connect to the local devnet network
const client = createPublicClient({
chain: hardhat,
transport: http('http://127.0.0.1:8545'),
});
// Define the contract address you want to query logs from
const contractAddress = '0xYourContractAddress';
// Define the ABI of the event you want to query (Transfer event as an example)
const transferEventAbi = parseAbiItem('event Transfer(address indexed from, address indexed to, uint256 value)');
// Set up the filter for fetching logs
const filter = {
address: contractAddress, // The contract address
event: transferEventAbi, // The ABI of the event to query
fromBlock: 1n, // Block number to start from (in BigInt format)
toBlock: 'latest', // Block number to end at
};
// Fetch logs using the filter
async function fetchLogs() {
try {
const logs = await client.getLogs({ ...filter });
console.log('Logs:', logs);
} catch (error) {
console.error('Error fetching logs:', error);
}
}
fetchLogs(); You can run this code outside of Hardhat. However, if you are using Hardhat, you can place the script inside the If you want to use hardhat ignition with these scripts, refer to this document: https://hardhat.org/ignition/docs/guides/scripts |
Beta Was this translation helpful? Give feedback.
-
Did you check the documentation?
Did you check for duplicate questions?
Issue Description
I have successfully deployed a smart contract on a local devnet using Hardhat Ignition. Now, I want to retrieve the logs or transaction history related to this contract. Could someone guide me on the best practices or methods to check the transaction history and logs of a smart contract on a local devnet.
Additional Information
No response
Feedback
No response
Beta Was this translation helpful? Give feedback.
All reactions