Skip to content

Commit

Permalink
Get file contents (#2578)
Browse files Browse the repository at this point in the history
* Add get-file-contents.js

Signed-off-by: b-l-u-e <[email protected]>

* modified Client with access to mainnet, localnet, testnet and made some few other changes

Signed-off-by: b-l-u-e <[email protected]>

* modified file

Signed-off-by: b-l-u-e <[email protected]>

* updated client configuration, replaced console logs with logger

Signed-off-by: b-l-u-e <[email protected]>

* client configuration and logger updated

Signed-off-by: b-l-u-e <[email protected]>

* logger updated

Signed-off-by: b-l-u-e <[email protected]>

* updated logger

Signed-off-by: b-l-u-e <[email protected]>

* updated logger

Signed-off-by: b-l-u-e <[email protected]>

---------

Signed-off-by: b-l-u-e <[email protected]>
  • Loading branch information
b-l-u-e authored Oct 25, 2024
1 parent cfe7be7 commit 6b0b713
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions examples/get-file-contents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {
PrivateKey,
FileContentsQuery,
Hbar,
FileCreateTransaction,
FileDeleteTransaction,
Client,
AccountId,
Logger,
LogLevel,
} from "@hashgraph/sdk";
import dotenv from "dotenv";

dotenv.config();
/**
* 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
) {
console.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
const client = Client.forName(process.env.HEDERA_NETWORK);

client.setOperator(operatorId, operatorKey);

// Set logger
const infoLogger = new Logger(LogLevel.Info);
client.setLogger(infoLogger);

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

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

try {
console.log("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;
console.log(`Created new file with ID: ${newFileId.toString()}`);

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

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

console.log("File contents: " + fileContentsQuery.toString());

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

await fileDeleteTxResponse.getReceipt(client);

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

void main();

0 comments on commit 6b0b713

Please sign in to comment.