Read commits via nit module
Example
const ethers = require("ethers");
const nit = require("@numbersprotocol/nit");
// Declare configuration object for connecting to blockchain network.
// This should match the blockchain network you used during the commit process
const config = {
"defaultNetwork": "jade",
"network": {
"jade": {
"url": "https://mainnetrpc.num.network",
"chainId": 10507,
"accounts": [
"<private-key>"
],
"contract": "0x7EC2F14ABE8b0Ea2F657bBb62b6fEfDe161c9001",
"explorerBaseUrl": "https://mainnet.num.network/tx"
}
}
};
// Declare variable for asset Nid
const assetNid = "bafybeigvqgqzob4754cqqic3wmagjak6kwhcadmhnyhs76ladkqejmc6ie";
// Function to retrieve block numbers for asset NID
async function getBlock(assetNid, blockchainInfo) {
// Use Nit library to get block numbers
const blocks = await nit.getCommitBlockNumbers(assetNid, blockchainInfo);
return { "blocks": blocks };
}
// Function to retrieve commit data for asset NID
async function getCommitsFromNid(assetNid, blockchainInfo) {
// Get block numbers for asset Nid
const blocks = await getBlock(assetNid, blockchainInfo);
let commits = []
// Loop through block numbers
for(let i = 0; i < blocks.blocks.length; i++){
// Create filter to retrieve "Commit" event logs
let filter = await blockchainInfo.contract.filters.Commit(null, assetNid);
// Declare ABI for "Commit" event
const abi = [
"event Commit(address indexed recorder, string indexed assetNid, string commitData)"
];
// Set filter to retrieve event logs from specific block
filter.fromBlock = blocks.blocks[i];
filter.toBlock = blocks.blocks[i];
// Use provider to get event logs
const eventLogs = await blockchainInfo.provider.getLogs(filter);
// Only one event log because restricting block range to a specific block number
const eventLog = eventLogs[0];
// Use ethers library to parse event log
const commitEventInterface = new ethers.utils.Interface(abi);
console.log(`\nBlock number: ${(eventLog.blockNumber)}`);
console.log(`${blockchainInfo.explorerBaseUrl}/${(eventLog.transactionHash)}`);
// Parse commit data from event log
const commitEvent = await commitEventInterface.parseLog(eventLog);
const commitData = JSON.parse(commitEvent.args[2])
// Add transaction ID to the commitData
commitData["transaction"] = eventLog.transactionHash;
commits.push(commitData)
}
// Return commit data array
return commits
}Sample results
Last updated