Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get file contents #2578

Merged
merged 8 commits into from
Oct 25, 2024
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions examples/get-file-contents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* eslint-disable n/no-extraneous-import */
import {
PrivateKey,
FileContentsQuery,
Hbar,
FileCreateTransaction,
FileDeleteTransaction,
Client,
AccountId,
} from "@hashgraph/sdk";
import dotenv from "dotenv";

import pino from "pino";
import pinoPretty from "pino-pretty";

dotenv.config();

// Set default log level to 'silent' if SDK_LOG_LEVEL is not specified in .env
const SDK_LOG_LEVEL = process.env.SDK_LOG_LEVEL || "SILENT";

// Logger configuration based on SDK_LOG_LEVEL
const logger = pino(
{
level: SDK_LOG_LEVEL.toUpperCase(),
},
pinoPretty({
colorize: true,
translateTime: "SYS:standard",
ignore: "pid,hostname",
}),
);
b-l-u-e marked this conversation as resolved.
Show resolved Hide resolved

/**
* How to get file contents
*
*/

async function main() {
/**
* Step 0:
* Create and configure the SDK Client
*/
if (
!process.env.OPERATOR_ID ||
!process.env.OPERATOR_KEY ||
!process.env.HEDERA_NETWORK
) {
logger.error(
"Environment variables OPERATOR_ID, OPERATOR_KEY, and HEDERA_NETWORK are required.",
);
throw new Error("Missing required environment variables.");
}

const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringED25519(process.env.OPERATOR_KEY);

// Create the client based on the HEDERA_NETWORK environment variable
let client = Client.forName(process.env.HEDERA_NETWORK);
b-l-u-e marked this conversation as resolved.
Show resolved Hide resolved

client.setOperator(operatorId, operatorKey);

/**
* Step 1: Submit the file create transaction
*/

// Content to be stored in the file.
const fileContents = Buffer.from("Hedera is great!", "utf-8");

try {
logger.info("Creating new file...");

// Create the transaction
let transaction = new FileCreateTransaction()
.setKeys([operatorKey.publicKey])
.setContents(fileContents)
.setMaxTransactionFee(new Hbar(2))
.freezeWith(client);

transaction = await transaction.sign(operatorKey);

const response = await transaction.execute(client);

const receipt = await response.getReceipt(client);

// Get the file ID
const newFileId = receipt.fileId;
logger.info(`Created new file with ID: ${newFileId.toString()}`);

/**
* Step 2: Get file contents and print them
*/

const fileContentsQuery = await new FileContentsQuery()
.setFileId(newFileId)
.execute(client);

logger.info("File contents: " + fileContentsQuery.toString());

// Clean up: Delete created file
const fileDeleteTxResponse = await new FileDeleteTransaction()
.setFileId(newFileId)
.execute(client);

await fileDeleteTxResponse.getReceipt(client);

logger.info("File deleted successfully");
} catch (error) {
logger.error("Error occurred during file creation:", error);
} finally {
client.close();
logger.info("Get File Contents Example Complete!");
}
}

void main();