Interacting with a Browser Wallet that Directly Supports OP_NET
To interact with OP_NET, browser wallets like OP_WALLET offer direct support via the window.opnet
object. This guide explains how to standardize your interactions with the wallet and access its features.
Standardizing Wallet Interactions
The window.opnet
interface provides seamless access to OP_NET functionalities directly from the browser. Using a standardized interface ensures compatibility with OP_WALLET and simplifies integration for developers.
Custom Window Interface
To ensure compatibility, you can define a custom interface for your window object:
import { Unisat } from "@btc-vision/transaction";
export interface CustomWindow {
opnet?: Unisat;
}
const _window: CustomWindow = window as CustomWindow;
const opnet: Unisat | undefined = _window.opnet;
Fetching Wallet Balance
To retrieve the wallet balance, interact with the window.opnet
object:
const balance = BigInt((await opnet.getBalance()).confirmed).toString();
console.log("Balance:", balance);
Explanation:
-
Access the Wallet Instance:
Use thewindow.opnet
object to interact with the OP_WALLET. -
Fetch Balance:
ThegetBalance
method retrieves the confirmed balance associated with the wallet.
Accessing the Web3Provider Interface
The Web3Provider is accessible through window.opnet.web3
and enables advanced interactions like signing transactions, deploying contracts, and broadcasting transactions.
const web3Provider = opnet.web3;
Web3Provider Methods
The Web3Provider interface includes the following methods:
-
signInteraction
Signs a transaction interaction. -
signAndBroadcastInteraction
Signs and directly broadcasts a transaction interaction. -
deployContract
Deploys a smart contract on OP_NET. -
broadcast
Broadcasts raw transactions to the network.
export interface Web3Provider {
signInteraction(
interactionParameters: InteractionParametersWithoutSigner
): Promise<[string, string, UTXO[]]>;
signAndBroadcastInteraction(
interactionParameters: InteractionParametersWithoutSigner
): Promise<[BroadcastedTransaction, BroadcastedTransaction, UTXO[]]>;
deployContract(
params: IDeploymentParametersWithoutSigner
): Promise<DeploymentResult>;
broadcast(
transactions: BroadcastTransactionOptions[]
): Promise<BroadcastedTransaction[]>;
}
Best Practices
- Always check if the wallet instance (
window.opnet
) is available before interacting. - Use the
Web3Provider
interface for advanced interactions like signing or broadcasting. - Provide clear error messages or alternative flows in case the wallet is unavailable.