Fetching Motoswap Pool Addresses

In this guide, you will learn how to interact with the Motoswap Factory contract to fetch the pool addresses for a pair of tokens, such as Wrapped Bitcoin (wBTC) and another token on the OP_NET metaprotocol.


Step 1: Install Required Packages

Ensure that you have the following packages installed in your project:

npm install opnet bitcoinjs-lib
# or
yarn add opnet bitcoinjs-lib

Step 2: Fetch the Pool Address

The process involves interacting with the Motoswap Factory contract to retrieve the pool ID for a pair of tokens, and then converting that pool ID into a valid pool address.

import { BinaryWriter } from "@btc-vision/bsi-binary";
import { AddressGenerator } from "@btc-vision/transaction";
import * as bitcoinjs from "bitcoinjs-lib";
import {
  getContract,
  IMotoswapFactoryContract,
  JSONRpcProvider,
  MotoSwapFactoryAbi,
} from "opnet";

/**
 * Fetches the pool address for a pair of tokens (e.g., baseToken and quoteToken) from the Motoswap Factory contract.
 *
 * @param {JSONRpcProvider} provider - The JSON RPC provider for interacting with OP_NET.
 * @param {string} factoryAddress - The address of the Motoswap Factory contract.
 * @param {string} baseToken - The address of the base token in the token pair.
 * @param {string} quoteToken - The address of the quote token (usually wBTC).
 * @param {bitcoinjs.Network} network - The BitcoinJS network (e.g., regtest, mainnet).
 * @returns The pool address for the token pair in Bitcoin Script Hash (PKSH) format.
 */
async function fetchPoolAddress(
  provider: JSONRpcProvider,
  factoryAddress: string,
  baseToken: string,
  quoteToken: string,
  network: bitcoinjs.Network
): Promise<string> {
  try {
    // Fetch the Motoswap Factory contract
    const factoryContract = getContract<IMotoswapFactoryContract>(
      factoryAddress,
      MotoSwapFactoryAbi,
      provider
    );

    // Get pool information for the pair (e.g., baseToken and quoteToken)
    const poolInfo = await factoryContract.getPool(baseToken, quoteToken);

    // If there is an error fetching pool info, return early
    if ("error" in poolInfo) {
      throw new Error(poolInfo.error);
    }

    // Extract the pool ID from the contract response
    const poolIdBigInt = BigInt(poolInfo.decoded[0].toString());

    // Convert the pool ID to a Bitcoin Script Hash (PKSH) address
    const poolAddress = poolIdToAddress(poolIdBigInt, network);

    return poolAddress;
  } catch (error) {
    console.error("Error fetching pool address:", error);
    throw error;
  }
}

/**
 * Converts a pool ID into a Bitcoin Script Hash (PKSH) address.
 *
 * @param {bigint} poolId - The pool ID from the factory contract.
 * @param {bitcoinjs.Network} network - The BitcoinJS network (e.g., regtest, mainnet).
 * @returns The Bitcoin Script Hash (PKSH) address of the pool.
 */
function poolIdToAddress(poolId: bigint, network: bitcoinjs.Network): string {
  const writer = new BinaryWriter();
  writer.writeU256(poolId);
  const vAddress = Buffer.from(writer.getBuffer());
  return AddressGenerator.generatePKSH(vAddress, network);
}

// Example usage
async function main() {
  const provider = new JSONRpcProvider("https://regtest.opnet.org");
  const factoryAddress = "bcrt1q9pf9fnpch9z2qrp5e3dgr2avzu3mypq3km2k40"; // Replace with actual factory address
  const baseToken = "bcrt1qw8w4ejas2k22y54avv7hgrslg3cd0hme58h28r"; // Base token address (actually MOTO)
  const quoteToken = "bcrt1qamv2ejattjgsc6k3yf3zqrp0wpuyedqgjmwx0v"; // Quote token address (actually wBTC)
  const network = bitcoinjs.networks.regtest; // Define the BitcoinJS network (e.g., regtest)

  const poolAddress = await fetchPoolAddress(
    provider,
    factoryAddress,
    baseToken,
    quoteToken,
    network
  );
  console.log("Pool Address:", poolAddress);
}

main();

Last updated