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

update: example for HIP-869 #2443

Merged
Show file tree
Hide file tree
Changes from all 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
95 changes: 95 additions & 0 deletions examples/create-update-delete-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {
Client,
PrivateKey,
AccountId,
NodeCreateTransaction,
NodeUpdateTransaction,
NodeDeleteTransaction,
ServiceEndpoint,
} from "@hashgraph/sdk";

import dotenv from "dotenv";

dotenv.config();

async function main() {
if (
process.env.OPERATOR_ID == null ||
process.env.OPERATOR_KEY == null ||
process.env.HEDERA_NETWORK == null
) {
throw new Error(
"Environment variables OPERATOR_ID, HEDERA_NETWORK, and OPERATOR_KEY are required.",
);
}

const network = process.env.HEDERA_NETWORK;
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringDer(process.env.OPERATOR_KEY);
const client = Client.forName(network).setOperator(operatorId, operatorKey);

// Transaction parameters
const accountId = AccountId.fromString("0.0.999");
const description = "This is a description of the node.";
const newDescription = "This is new a description of the node.";
const ipAddressV4 = Uint8Array.of(127, 0, 0, 1);
const port = 50211;
const gossipEndpoint = new ServiceEndpoint()
.setIpAddressV4(ipAddressV4)
.setPort(port);
const gossipEndpoints = [gossipEndpoint];
const serviceEndpoint = new ServiceEndpoint()
.setIpAddressV4(ipAddressV4)
.setPort(port);
const serviceEndpoints = [serviceEndpoint];
const gossipCaCertificate = new Uint8Array();
const certificateHash = new Uint8Array();
const adminKey = PrivateKey.generate();

// 1. Create a new node
console.log("Creating a new node...");
const createTransaction = new NodeCreateTransaction()
.setAccountId(accountId)
.setDescription(description)
.setGossipEndpoints(gossipEndpoints)
.setServiceEndpoints(serviceEndpoints)
.setGossipCaCertificate(gossipCaCertificate)
.setCertificateHash(certificateHash)
.setAdminKey(adminKey);
const createTransactionResponse = await createTransaction.execute(client);
const createTransactionReceipt =
await createTransactionResponse.getReceipt(client);
const nodeId = createTransactionReceipt.nodeId;
console.log(
`Node create transaction status: ${createTransactionReceipt.status.toString()}`,
);
console.log(
`Node has been created successfully with node id: ${nodeId.toString()}`,
);

// 2. Update the node
console.log("Updating the node...");
const updateTransaction = new NodeUpdateTransaction()
.setNodeId(nodeId)
.setDescription(newDescription);
const updateTrasnactionResponse = await updateTransaction.execute(client);
const updateTrasnactionReceipt =
await updateTrasnactionResponse.getReceipt(client);
console.log(
`Node update transaction status: ${updateTrasnactionReceipt.status.toString()}`,
);

// 3. Delete the node
console.log("Deleting the node...");
const deleteTransaction = new NodeDeleteTransaction().setNodeId(nodeId);
const deleteTransactionResponse = await deleteTransaction.execute(client);
const deleteTransactionReceipt =
await deleteTransactionResponse.getReceipt(client);
console.log(
`Node delete transaction status: ${deleteTransactionReceipt.status.toString()}`,
);

client.close();
}

void main();
4 changes: 3 additions & 1 deletion src/node/NodeUpdateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ export default class NodeUpdateTransaction extends Transaction {
*/
setDescription(description) {
if (description.length > DESCRIPTION_MAX_LENGTH) {
throw new Error(`Description must be at most ${DESCRIPTION_MAX_LENGTH} characters.`);
throw new Error(
`Description must be at most ${DESCRIPTION_MAX_LENGTH} characters.`,
);
}
this._description = description;

Expand Down
4 changes: 2 additions & 2 deletions src/node/ServiceEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

export default class ServiceEndpoint {
/**
* @param {object} props
* @param {object} [props]
* @param {?Uint8Array} [props.ipAddressV4]
* @param {number} [props.port]
* @param {?number} [props.port]
* @param {?string} [props.domainName]
*/
constructor(props) {
Expand Down