Get Seal
Summary:
The script queries a smart contract deployed on the Hedera network to retrieve information about a seal using a specific hash. It showcases how to send queries to the smart contract and process the query results.
1. Imports and Environment Setup:
const {
    Client,
    ContractFunctionParameters,
    ContractCallQuery,
    Hbar, PrivateKey
} = require("@hashgraph/sdk");
require('dotenv').config({ path: 'SmartContract_Service/.env' });- It imports necessary modules from the Hedera JavaScript SDK ( - @hashgraph/sdk) and the- dotenvmodule to load environment variables from a- .envfile located in the- SmartContract_Servicedirectory.
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 - .envfile.
- 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() {
    //Query the contract for the contract message
    const contractCallQuery = new ContractCallQuery()
        .setContractId(contractId)
        .setGas(100000)
        .setFunction("getSeal", new ContractFunctionParameters().addString(hash))
        .setQueryPayment(new Hbar(10));
    const contractQuerySubmit = await contractCallQuery.execute(client);
    const sealedBlock = contractQuerySubmit.getUint256(0);
    const revokedBlock = contractQuerySubmit.getUint256(1);
    const sealOwner = contractQuerySubmit.getAddress(2);
    console.log("The seal was created by the address " + sealOwner + " on block #" + sealedBlock + " and got revoked at block #" +revokedBlock);
    process.exit();
}- It defines the - main()function, which is the entry point of the script.
- It creates a query to call the - getSealfunction of the smart contract with the provided hash parameter.
- The query is submitted to the Hedera network, and the result is retrieved. 
- The sealed block, revoked block, and seal owner are extracted from the query result and logged to the console. 
5. Execution:
main();- This line calls the - main()function to start the execution of the script.
Last updated
Was this helpful?