# Seal Document

### Summary:

The script interacts with a deployed smart contract on the Hedera network by executing a function call and decoding the emitted events. It showcases how to send transactions to the smart contract and parse the resulting event data.

### 1. Imports and Environment Setup:

```javascript
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`), including `Client`, `ContractFunctionParameters`, `ContractExecuteTransaction`, and `PrivateKey`.
* It also imports the `dotenv` module to load environment variables from a `.env` file located in the `SmartContract_Service` directory.
* The script imports the `Web3` module to interact with the Ethereum-like smart contract and creates a `web3` instance.

### 2. Environment Variables Retrieval and Validation:

```javascript
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");
}
```

* The script 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:

```javascript
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:

```javascript
async function main() {
    // Transaction to update the contract message
    const contractExecTx = await new ContractExecuteTransaction()
        .setContractId(contractId)
        .setGas(100000)
        .setFunction("seal", 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("DocumentSealed", logStringHex, logTopics.slice(1));
    });

    process.exit();
}
```

* The `main()` function is the entry point of the script.
* It creates a transaction to execute a function (`seal`) on the smart contract with the given contract ID and hash parameter.
* The transaction is executed on the Hedera network, and its receipt is retrieved to confirm the execution status.
* Events emitted by the smart contract function call are parsed and decoded using the `decodeEvent` function.

### 5. Event Decoding Function:

```javascript
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;
}
```

* The `decodeEvent` function decodes the event contents using the ABI definition of the event.
* It loads the ABI file of the smart contract and finds the event ABI based on the event name.
* The event data is decoded using `web3.eth.abi.decodeLog`.

### 6. Execution:

```javascript
main();
```

* This line calls the `main()` function to start the execution of the script.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://abc.bharatblockchain.io/hedera/tutorials/smartcontract-service/seal-document.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
