Interacting with OP_NET in Your Project

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:

npm install opnet
# or
yarn add opnet

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:

Example Code

Running the Example

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:

npm install --save-dev tsx
# or
yarn add --dev tsx

Then, you can run your TypeScript file directly:

npx tsx file.ts
# or
yarn tsx file.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:

Example Code

Running the Example

To run this JavaScript example, make sure you configure your project for ESM.

  1. Option 1: Enable ESM via package.json:

    In your package.json, add or modify the "type" field to "module":

    {
      "type": "module"
    }
  2. 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:

npm install opnet
# or 
yarn add opnet

Execute the Code

After setting up, run your JavaScript file with:

node file.js
# or, if you're using .mjs files
node file.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.
 */
const provider = new JSONRpcProvider("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.
 */
async function getBalance(address: string) {
  // Fetch the balance of the address in satoshis (1 rBTC = 100,000,000 satoshis)
  const balance = await provider.getBalance(address);

  // Convert the balance from satoshis to rBTC
  const formattedBalance = parseFloat(balance.toString()) / 10 ** 8;

  // Log the balance
  console.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.
 */
async function getBalanceOfToken(address: string, tokenAddress: string) {
  // Get the OP_20 contract instance using the token's contract address and the OP_20 ABI
  const contract = getContract<IOP_20Contract>(
    tokenAddress,
    OP_20_ABI,
    provider
  );

  // Call the balanceOf method of the OP_20 contract to fetch the balance
  const balance = await contract.balanceOf(address);

  // Check if the contract call returned an error
  if ("error" in balance) return console.log(balance.error);

  // Convert the token balance from smallest unit (e.g., satoshis) to full token units
  const formattedBalance = parseFloat(balance.decoded[0].toString()) / 10 ** 8;

  // Log the token balance
  console.log(
    `Balance of ${address} is ${formattedBalance.toLocaleString()} MOTO`
  );
}

/**
 * Main function that demonstrates fetching the rBTC and OP_20 token balances.
 */
async function main() {
  // Fetch the balance in rBTC for a specific address
  await getBalance(
    "bcrt1p823gdnqvk8a90f8cu30w8ywvk29uh8txtqqnsmk6f5ktd7hlyl0qupwyqz"
  );

  // Fetch the balance of the MOTO OP_20 token for the same address
  await getBalanceOfToken(
    "bcrt1p823gdnqvk8a90f8cu30w8ywvk29uh8txtqqnsmk6f5ktd7hlyl0qupwyqz",
    "bcrt1qw8w4ejas2k22y54avv7hgrslg3cd0hme58h28r" // MOTO Contract on Regtest
  );
}

// Execute the main function
main();

Last updated