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:
npminstallopnetbitcoinjs-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. */asyncfunctionfetchPoolAddress( provider:JSONRpcProvider, factoryAddress:string, baseToken:string, quoteToken:string, network:bitcoinjs.Network):Promise<string> {try {// Fetch the Motoswap Factory contractconstfactoryContract=getContract<IMotoswapFactoryContract>( factoryAddress, MotoSwapFactoryAbi, provider );// Get pool information for the pair (e.g., baseToken and quoteToken)constpoolInfo=awaitfactoryContract.getPool(baseToken, quoteToken);// If there is an error fetching pool info, return earlyif ("error"in poolInfo) {thrownewError(poolInfo.error); }// Extract the pool ID from the contract responseconstpoolIdBigInt=BigInt(poolInfo.decoded[0].toString());// Convert the pool ID to a Bitcoin Script Hash (PKSH) addressconstpoolAddress=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. */functionpoolIdToAddress(poolId:bigint, network:bitcoinjs.Network):string {constwriter=newBinaryWriter();writer.writeU256(poolId);constvAddress=Buffer.from(writer.getBuffer());returnAddressGenerator.generatePKSH(vAddress, network);}// Example usageasyncfunctionmain() {constprovider=newJSONRpcProvider("https://regtest.opnet.org");constfactoryAddress="bcrt1q9pf9fnpch9z2qrp5e3dgr2avzu3mypq3km2k40"; // Replace with actual factory addressconstbaseToken="bcrt1qw8w4ejas2k22y54avv7hgrslg3cd0hme58h28r"; // Base token address (actually MOTO)constquoteToken="bcrt1qamv2ejattjgsc6k3yf3zqrp0wpuyedqgjmwx0v"; // Quote token address (actually wBTC)constnetwork=bitcoinjs.networks.regtest; // Define the BitcoinJS network (e.g., regtest)constpoolAddress=awaitfetchPoolAddress( provider, factoryAddress, baseToken, quoteToken, network );console.log("Pool Address:", poolAddress);}main();