Deploy to Hedera

Summary:

The script demonstrates how to deploy a smart contract on the Hedera network by first uploading its bytecode to a file, then instantiating the contract using the uploaded bytecode and providing any constructor parameters required. Finally, it logs the contract ID once deployed successfully.

1. Imports and Environment Setup:

const {
    Client,
    FileCreateTransaction,
    ContractCreateTransaction,
    PrivateKey,
    ContractFunctionParameters
} = require("@hashgraph/sdk");
require('dotenv').config({ path: 'SmartContract_Service/.env' });
  • The code imports necessary modules from the Hedera JavaScript SDK (@hashgraph/sdk), including Client, FileCreateTransaction, ContractCreateTransaction, PrivateKey, and ContractFunctionParameters.

  • It also imports 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);

// If we weren't able to grab it, we should throw a new error
if (myAccountId == null || myPrivateKey == null) {
    throw new Error("Environment variables myAccountId and myPrivateKey must be present");
}
  • The script retrieves the account ID and private key 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() {
    let esealCompiled = require("../artifacts/contracts/eseal.sol/Eseal.json");
    const bytecode = esealCompiled.bytecode;

    const fileCreateTx = new FileCreateTransaction()
        .setContents(bytecode);
    const submitTx = await fileCreateTx.execute(client);
    const fileReceipt = await submitTx.getReceipt(client);
    const bytecodeFileId = fileReceipt.fileId;

    console.log("The smart contract byte code file ID is " + bytecodeFileId);

    const contractTx = await new ContractCreateTransaction()
        .setBytecodeFileId(bytecodeFileId)
        .setGas(100000)
        .setConstructorParameters(new ContractFunctionParameters().addString("Hello from Hedera!"));
    const contractResponse = await contractTx.execute(client);
    const contractReceipt = await contractResponse.getReceipt(client);
    const newContractId = contractReceipt.contractId;

    console.log("The smart contract ID is " + newContractId);

    process.exit();
}
  • This is the main function where the smart contract deployment process occurs.

  • It requires the compiled bytecode of the smart contract from the eseal.sol file, which is assumed to be located in the artifacts/contracts/eseal.sol directory.

  • The script creates a file on Hedera and stores the hex-encoded bytecode of the contract in it using a FileCreateTransaction.

  • It retrieves the file ID from the transaction receipt and logs it.

  • Next, it instantiates the contract using a ContractCreateTransaction.

  • Constructor parameters are provided using setConstructorParameters, in this case, a string parameter "Hello from Hedera!".

  • The transaction to deploy the contract is executed on the Hedera network, and the resulting contract ID is retrieved from the transaction receipt and logged.

5. Execution:

main();
  • This line calls the main() function to start the execution of the script.

Last updated