Revoke Document
Summary:
The script executes a transaction on a smart contract deployed on the Hedera network to revoke a seal using a specific hash. It showcases how to send transactions to the smart contract and process the transaction receipts and event logs emitted by the contract.
1. Imports and Environment Setup:
const {
Client,
ContractFunctionParameters,
ContractExecuteTransaction,
PrivateKey
} = require("@hashgraph/sdk");
require('dotenv').config({ path: 'SmartContract_Service/.env' });
const Web3 = require('web3');
const web3 = new Web3;
let abi;
It imports necessary modules from the Hedera JavaScript SDK (
@hashgraph/sdk
) and thedotenv
module to load environment variables from a.env
file located in theSmartContract_Service
directory.It imports the
Web3
library to decode event logs emitted by the smart contract.
2. Environment Variables Retrieval and Validation:
const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
const contractId = process.env.CONTRACT_ID;
const hash = process.env.HASH;
if (myAccountId == null || myPrivateKey == null) {
throw new Error("Environment variables myAccountId and myPrivateKey must be present");
}
It retrieves the account ID, private key, contract ID, and hash from the
.env
file.It checks whether these variables are present; otherwise, it throws an error.
3. Client Setup:
const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
It creates a client instance for the Hedera testnet and sets the operator account using the user's account ID and private key.
4. Main Function:
async function main() {
const contractExecTx = await new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(100000)
.setFunction("revokeSeal", new ContractFunctionParameters().addString(hash));
const submitExecTx = await contractExecTx.execute(client);
const receipt2 = await submitExecTx.getReceipt(client);
console.log("The transaction status is " + receipt2.status.toString());
const record = await submitExecTx.getRecord(client);
record.contractFunctionResult.logs.forEach(log => {
let logStringHex = '0x'.concat(Buffer.from(log.data).toString('hex')));
let logTopics = [];
log.topics.forEach(topic => {
logTopics.push('0x'.concat(Buffer.from(topic).toString('hex')));
});
decodeEvent("DocumentRevoced", logStringHex, logTopics.slice(1));
});
process.exit();
}
It defines the
main()
function, which is the entry point of the script.It creates a transaction to execute the
revokeSeal
function of the smart contract with the provided hash parameter.The transaction is submitted to the Hedera network, and the result (transaction receipt) is retrieved.
Event logs emitted by the smart contract during the transaction execution are decoded using the
decodeEvent()
function.
5. Event Decoding Function:
function decodeEvent(eventName, log, topics) {
const abiFile = require("../artifacts/contracts/eseal.sol/Eseal.json");
abi = abiFile.abi;
const eventAbi = abi.find(event => (event.name === eventName && event.type === "event"));
const decodedLog = web3.eth.abi.decodeLog(eventAbi.inputs, log, topics);
console.log(decodedLog);
return decodedLog;
}
It defines the
decodeEvent()
function to decode event logs emitted by the smart contract using the contract's ABI definition.
6. Execution:
main();
This line calls the
main()
function to start the execution of the script.
Last updated
Was this helpful?