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

feat(example): Custom web proxies usage and documentation #2729

Merged
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
4 changes: 4 additions & 0 deletions common_js_test/src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ describe("CommonJS", function () {
console.log(`Failed for ${nodeAccountId}`);
}
}

// Close the client connection
client.close();

// Ensure that at least one attempt was successful
if (!succeededAtLeastOnce) {
throw new Error("No successful query attempts were made.");
}
Expand Down
6 changes: 6 additions & 0 deletions examples/custom-grpc-web-proxies-network/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Hedera Operator Account ID
REACT_APP_OPERATOR_ID="YOUR_OPERATOR_ID"

# Hedera Operator Private Key
REACT_APP_OPERATOR_KEY="YOUR_PRIVATE_KEY"

13 changes: 13 additions & 0 deletions examples/custom-grpc-web-proxies-network/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

# production
/build

.env

npm-debug.log*
yarn-debug.log*
yarn-error.log*
62 changes: 62 additions & 0 deletions examples/custom-grpc-web-proxies-network/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Custom Network Configuration with gRPC Web Proxies

This guide demonstrates how to configure the Hedera SDK to communicate with a custom network using gRPC web proxies in a React application. The example sets up a transfer transaction, which is triggered when the page is loaded, using a custom network configuration with multiple gRPC web proxies.

## Steps to configure the custom network

### Prerequisites

- Install the required Node modules by running `npm install`
- Create a `.env` file in the root directory and add your credentials.

### 1. Setup the operator account

First, you need to set up your operator account and private key. These credentials are required to sign and authorize the transaction.

```javascript
const operatorId = AccountId.fromString("<YOUR_ACCOUNT_ID>");
const operatorKey = PrivateKey.fromStringECDSA("<YOUR_PRIVATE_KEY>");
```

### 2. Create a custom network with gRPC web proxies

Define a list of gRPC web proxies to communicate with your custom network. Each entry consists of a proxy URL and its associated account ID.

```javascript
const nodes = {
"https://testnet-node02-00-grpc.hedera.com:443": new AccountId(5),
"https://testnet-node03-00-grpc.hedera.com:443": new AccountId(6),
"https://testnet-node04-00-grpc.hedera.com:443": new AccountId(7),
};
```

These proxy URLs correspond to the gRPC web proxies you wish to use for the custom network.

### 3. Setup the `Client` using `Client.forNetwork()`

To configure the client with the custom network, use `Client.forNetwork()` and pass the list of nodes you defined. The SDK automatically detects the environment (whether it's a browser or Node.js environment).

```javascript
const client = Client.forNetwork(nodes);
client.setOperator(operatorId, operatorKey);
```

In the browser environment, this will use gRPC web proxies.

### 4. Configure the transaction for the Custom Network

Before sending a transaction, you need to configure it with the necessary parameters, such as the sender, receiver, and amount. Here's how to create a transfer transaction using the configured client for the custom network.

```javascript
const transferTransaction = new TransferTransaction()
.addSender(senderAccountId, amountToSend)
.addReceiver(receiverAccountId, amountToReceive)
.setTransactionMemo("Transfer via custom gRPC web proxy network")
.setTransactionFee(transactionFee);

const response = await transferTransaction.execute(client);
```

### 5. Running the application

Once you've set up the client and transaction as described above, you can run your React application with `npm start`. The transfer transaction will be executed using the **custom gRPC web proxies** configured in the Client.forNetwork() method.
Loading
Loading