Use the web3js-quorum client library
web3js-quorum is an Ethereum JavaScript library extending web3.js that adds support for Besu-specific JSON-RPC APIs and features. Use the library to create and send RLP-encoded transactions using JSON-RPC.
web3js-quorum supports JSON-RPC over HTTP only.
web3js-quorum includes all quorum.js and web3js-eea features.
If migrating to web3js-quorum, update your JavaScript code as indicated in the following examples.
Read the migration guide for more information about updating your code.
Prerequisites
Add web3js-quorum to project
npm install web3js-quorum
Initialize the web3js-quorum client
Initialize your client where:
<JSON-RPC HTTP endpoint>
is the JSON-RPC HTTP endpoint of your Hyperledger Besu node. Specified by the--rpc-http-host
and--rpc-http-port
command line options.
- Syntax
- Example
const { Web3 } = require("web3");
const Web3Quorum = require("web3js-quorum");
const web3 = new Web3Quorum(new Web3("<JSON-RPC HTTP endpoint>"));
const { Web3 } = require("web3");
const Web3Quorum = require("web3js-quorum");
const web3 = new Web3Quorum(new Web3("http://localhost:8545"));
When migrating from web3js-eea to web3js-quorum, use Web3Quorum
. The constructor doesn't require the chain ID anymore. Chain ID is automatically retrieved from the chain using the specified JSON-RPC HTTP endpoint.
Deploy a contract with generateAndSendRawTransaction
To deploy a private contract, you need the contract binary. You can use Solidity to get the contract binary.
const contractOptions = {
data: `0x123`, // contract binary
privateFrom: "tesseraNode1PublicKey",
privateFor: ["tesseraNode3PublicKey"],
privateKey: "besuNode1PrivateKey",
};
return web3.priv.generateAndSendRawTransaction(contractOptions);
web3.priv.generateAndSendRawTransaction(contractOptions)
returns the transaction hash. To get the private transaction receipt, use web3.priv.waitForTransactionReceipt(txHash)
.
web3js-quorum methods
For more information about the web3js-quorum methods, see the web3js-quorum reference documentation.