Applied Blockchain Center (ABC)
  • System requirements for ABC
  • Hedera
    • 🅰️About Hedera
    • 🖥️Environment Setup
    • Tutorials
      • Accounts
        • Get My Account Info
        • Get Other Account Info
        • Transfer HBAR
      • Consensus Service
        • Create Topic
        • Subscribe to a topic
        • Submit a message
      • File Service
        • Create a File
        • Retrieve File
      • Scheduled_TX
        • Create Scheduled Transaction
        • Scheduled Transaction Info
        • Delete Scheduled Transaction
        • Submit Signature
      • SmartContract Service
        • Deploy to Hedera
        • Seal Document
        • Get Seal
        • Check Seal Revocation Status
        • Revoke Document
      • Token Service
        • Fungible Token
        • Non-fungible Token
  • Hyperledger Fabric
    • 📑About Hyperledger Fabric
    • 💻Prerequisite Installation
    • Fabric Installation
      • 🧪Installation & Test
      • ✈️Launch Network
      • ⛓️Channel Creation
      • 🚚Chaincode Deployment
      • ▶️Chaincode Execution
  • Hyperledger Besu
    • Besu Network Set
Powered by GitBook
On this page
  • Summary:
  • 1. Imports and Environment Setup:
  • 2. Environment Variables Retrieval and Validation:
  • 3. Client Setup:
  • 4. Main Function:
  • 5. Execution:

Was this helpful?

  1. Hedera
  2. Tutorials
  3. SmartContract Service

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 dotenv module to load environment variables from a .env file located in the SmartContract_Service directory.

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() {
    //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 getSeal function 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.

PreviousSeal DocumentNextCheck Seal Revocation Status

Last updated 1 year ago

Was this helpful?