-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestledger.js
More file actions
48 lines (39 loc) · 1.67 KB
/
Copy pathtestledger.js
File metadata and controls
48 lines (39 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const { Client, FileCreateTransaction, TransactionReceiptQuery } = require("@hashgraph/sdk");
const fs = require('fs');
require('dotenv').config();
async function uploadFile() {
const client = Client.forTestnet();
client.setOperator(process.env.MY_ACCOUNT_ID, process.env.MY_PRIVATE_KEY);
fs.readFile('output.txt', 'utf8', async (err, data) => {
if (err) {
console.error("Error reading the file:", err);
return;
}
try {
const fileCreateTx = new FileCreateTransaction().setContents(data);
const fileCreateTxResponse = await fileCreateTx.execute(client);
const transactionId = fileCreateTxResponse.transactionId;
console.log(`File create transaction ID: ${transactionId}`);
// Wait for the transaction to be confirmed
const receipt = await new TransactionReceiptQuery()
.setTransactionId(transactionId)
.execute(client);
// Log the full receipt
console.log(`File create transaction receipt: ${JSON.stringify(receipt)}`);
const fileId = receipt.fileId.toString(); // Convert to string
console.log("File ID:", fileId);
// Write fileId to another file
fs.writeFile('fileId.txt', fileId, (err) => {
if (err) {
console.error("Error writing fileId to file:", err);
} else {
console.log("fileId written to fileId.txt");
}
});
} catch (error) {
console.error("Error during transaction:", error);
}
});
}
// Call the function
uploadFile();