HTTP Provider
The HTTP provider is a core component of the OP_NET metaprotocol. It allows applications to interact with OP_NET nodes over standard HTTP connections, enabling features such as fetching blockchain data, sending transactions, and querying smart contracts.
This guide focuses on setting up the JSONRpcProvider
as an HTTP provider for OP_NET.
A provider facilitates communication between your application and an OP_NET node. It handles:
- Reading blockchain data (e.g., blocks, transactions, and contracts).
- Writing data to the blockchain, such as broadcasting transactions.
- Querying smart contracts deployed on the OP_NET protocol.
The JSONRpcProvider
The JSONRpcProvider
, part of the opnet
package, is the class responsible for establishing connections with OP_NET nodes. It inherits from the AbstractRpcProvider
class and provides methods to send JSON-RPC requests to the node.
Constructor
export declare class JSONRpcProvider extends AbstractRpcProvider {
private readonly timeout;
readonly url: string;
constructor(url: string, network: Network, timeout?: number);
_send(payload: JsonRpcPayload | JsonRpcPayload[]): Promise<JsonRpcCallResult>;
protected providerUrl(url: string): string;
}
url: string
: The URL of the OP_NET node.network: Network
: Network configuration provided by the@btc-vision/bitcoin
package.timeout?: number
: Optional timeout for RPC calls in milliseconds.
Setting Up an HTTP Provider
Before setting up the HTTP provider, ensure you have all the prerequisites installed. If not, refer to the Prerequisites guide.
import { JSONRpcProvider } from "opnet";
import { networks } from "@btc-vision/bitcoin";
// Define the RPC endpoint and network
const url = "https://regtest.opnet.org";
const network = networks.regtest; // or networks.testnet, networks.mainnet
// Initialize the provider
const provider = new JSONRpcProvider(url, network);
// Optional: Set a custom timeout (in milliseconds)
const timeout = 10000; // 10 seconds
const customProvider = new JSONRpcProvider(url, network, timeout);
To get the list of supported networks, refer to the Networks guide.
Best Practices
When setting up an HTTP provider, consider the following best practices:
- Network Configuration: Always ensure that the
network
parameter matches the node's configuration to avoid mismatches. - Error Handling: Implement robust error handling for failed requests and timeouts.
- Active Node Selection: Use active nodes from the supported networks table.
What’s Next?
After setting up an HTTP provider, you can proceed to: