|
| 1 | +# Custom Network Configuration with gRPC Web Proxies |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Steps to configure the custom network |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +- Install the required Node modules by running `npm install` |
| 10 | +- Create a `.env` file in the root directory and add your credentials. |
| 11 | + |
| 12 | +### 1. Setup the operator account |
| 13 | + |
| 14 | +First, you need to set up your operator account and private key. These credentials are required to sign and authorize the transaction. |
| 15 | + |
| 16 | +```javascript |
| 17 | +const operatorId = AccountId.fromString("<YOUR_ACCOUNT_ID>"); |
| 18 | +const operatorKey = PrivateKey.fromStringECDSA("<YOUR_PRIVATE_KEY>"); |
| 19 | +``` |
| 20 | + |
| 21 | +### 2. Create a custom network with gRPC web proxies |
| 22 | + |
| 23 | +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. |
| 24 | + |
| 25 | +```javascript |
| 26 | +const nodes = { |
| 27 | + "https://testnet-node02-00-grpc.hedera.com:443": new AccountId(5), |
| 28 | + "https://testnet-node03-00-grpc.hedera.com:443": new AccountId(6), |
| 29 | + "https://testnet-node04-00-grpc.hedera.com:443": new AccountId(7), |
| 30 | +}; |
| 31 | +``` |
| 32 | + |
| 33 | +These proxy URLs correspond to the gRPC web proxies you wish to use for the custom network. |
| 34 | + |
| 35 | +### 3. Setup the `Client` using `Client.forNetwork()` |
| 36 | + |
| 37 | +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). |
| 38 | + |
| 39 | +```javascript |
| 40 | +const client = Client.forNetwork(nodes); |
| 41 | +client.setOperator(operatorId, operatorKey); |
| 42 | +``` |
| 43 | + |
| 44 | +In the browser environment, this will use gRPC web proxies. |
| 45 | + |
| 46 | +### 4. Configure the transaction for the Custom Network |
| 47 | + |
| 48 | +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. |
| 49 | + |
| 50 | +```javascript |
| 51 | +const transferTransaction = new TransferTransaction() |
| 52 | + .addSender(senderAccountId, amountToSend) |
| 53 | + .addReceiver(receiverAccountId, amountToReceive) |
| 54 | + .setTransactionMemo("Transfer via custom gRPC web proxy network") |
| 55 | + .setTransactionFee(transactionFee); |
| 56 | + |
| 57 | +const response = await transferTransaction.execute(client); |
| 58 | +``` |
| 59 | + |
| 60 | +### 5. Running the application |
| 61 | + |
| 62 | +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. |
0 commit comments