In this guide, you'll learn how to set up a basic project to start interacting with OP_NET using the opnet package. Whether you're setting up a Node.js project with TypeScript or building a Next.js application, these examples will help you quickly integrate OP_NET functionalities into your project.
Setting Up OP_NET in a TypeScript Project
For a general TypeScript environment, you can easily install the necessary packages and start interacting with OP_NET.
Installation
You can install the opnet package via npm or yarn:
npminstallopnet# oryarnaddopnet
This setup provides all the tools necessary to interact with OP_NET, including balance checks and token interactions.
Here’s an example of how to use the opnet package to retrieve balances and interact with an OP_20 contract:
To run this TypeScript example, you can use tsx, a popular tool for running TypeScript files without needing to compile them separately. First, install tsx as a development dependency:
npminstall--save-devtsx# oryarnadd--devtsx
Then, you can run your TypeScript file directly:
npxtsxfile.ts# oryarntsxfile.ts
This will execute the script without the need for prior compilation, making development faster and more convenient.
Setting Up OP_NET in a JavaScript Project
To integrate OP_NET into a JavaScript project, ensure your project is set up to use ECMAScript Modules (ESM). You can achieve this by enabling "type": "module" in your package.json or using .mjs file extensions. Below is an example of how to retrieve balances and interact with an OP_20 contract using opnet.
Here’s an example of how to use the opnet package to retrieve balances and interact with an OP_20 contract:
To run this JavaScript example, make sure you configure your project for ESM.
Option 1: Enable ESM via package.json:
In your package.json, add or modify the "type" field to "module":
{"type":"module"}
Option 2: Use .mjs File Extension:
Alternatively, rename your JavaScript files with a .mjs extension (e.g., file.mjs).
Install Required Packages
Run the following command to install the necessary dependencies:
npminstallopnet# or yarnaddopnet
Execute the Code
After setting up, run your JavaScript file with:
nodefile.js# or, if you're using .mjs filesnodefile.mjs
Code Overview
import { getContract, IOP_20Contract, JSONRpcProvider, OP_20_ABI } from"opnet";/** * Creates a JSON-RPC provider to interact with the OP_NET metaprotocol. * * @param rpcUrl The URL of the OP_NET RPC (for regtest, testnet, or mainnet). * @returns A provider to interact with the OP_NET metaprotocol. */constprovider=newJSONRpcProvider("https://regtest.opnet.org");/** * Fetches and logs the balance of an address in rBTC (regtest Bitcoin on the OP_NET metaprotocol). * * @param address The Taproot address to check the balance for. * @returns Logs the rBTC balance of the address. */asyncfunctiongetBalance(address:string) {// Fetch the balance of the address in satoshis (1 rBTC = 100,000,000 satoshis)constbalance=awaitprovider.getBalance(address);// Convert the balance from satoshis to rBTCconstformattedBalance=parseFloat(balance.toString()) /10**8;// Log the balanceconsole.log(`Balance of ${address} is ${formattedBalance.toLocaleString()} rBTC` );}/** * Fetches and logs the balance of a specific OP_20 token for an address. * * @param address The Taproot address to check the balance for. * @param tokenAddress The contract address of the OP_20 token on OP_NET. * @returns Logs the OP_20 token balance of the address. */asyncfunctiongetBalanceOfToken(address:string, tokenAddress:string) {// Get the OP_20 contract instance using the token's contract address and the OP_20 ABIconstcontract=getContract<IOP_20Contract>( tokenAddress,OP_20_ABI, provider );// Call the balanceOf method of the OP_20 contract to fetch the balanceconstbalance=awaitcontract.balanceOf(address);// Check if the contract call returned an errorif ("error"in balance) returnconsole.log(balance.error);// Convert the token balance from smallest unit (e.g., satoshis) to full token unitsconstformattedBalance=parseFloat(balance.decoded[0].toString()) /10**8;// Log the token balanceconsole.log(`Balance of ${address} is ${formattedBalance.toLocaleString()} MOTO` );}/** * Main function that demonstrates fetching the rBTC and OP_20 token balances. */asyncfunctionmain() {// Fetch the balance in rBTC for a specific addressawaitgetBalance("bcrt1p823gdnqvk8a90f8cu30w8ywvk29uh8txtqqnsmk6f5ktd7hlyl0qupwyqz" );// Fetch the balance of the MOTO OP_20 token for the same addressawaitgetBalanceOfToken("bcrt1p823gdnqvk8a90f8cu30w8ywvk29uh8txtqqnsmk6f5ktd7hlyl0qupwyqz","bcrt1qw8w4ejas2k22y54avv7hgrslg3cd0hme58h28r"// MOTO Contract on Regtest );}// Execute the main functionmain();