Skip to content

btc-vision/opnet

Repository files navigation

OP_NET - Smart Contracts on Bitcoin L1

Bitcoin TypeScript NodeJS NPM Gulp ESLint

code style: prettier

Introduction

A complete, compact and simple library for the Bitcoin ecosystem, written in TypeScript. This library is designed to be easy to use and understand, while providing a comprehensive set of functions for creating, reading and manipulating Bitcoin transactions. This library is designed to be able to manipulate anything related to BSI (Bitcoin Smart Inscription), smart contracts, and other Bitcoin-related technologies.

Getting Started

Prerequisites

  • Node.js version 16.x or higher
  • npm (Node Package Manager)

Installation

npm i opnet

Documentation

Documentation available at https://dev.opnet.org or in the docs/ directory of the repository.

Development

  1. Clone the repository:
    git clone https://github.com/btc-vision/opnet.git
  2. Navigate to the repository directory:
    cd opnet
  3. Install the required dependencies:
    npm i

Example

Calling a contract function from typescript/javascript is as simple as the following code:

import { getContract, IOP_20Contract, JSONRpcProvider, OP_20_ABI } from 'opnet';

async function main() {
    const provider: JSONRpcProvider = new JSONRpcProvider('https://testnet.opnet.org');
    const contract: IOP_20Contract = getContract<IOP_20Contract>(
        'tb1q4tyhf8hpu04qjj3qaag20knun0spctultxzakw', // MOTO Contract
        OP_20_ABI,
        provider,
    );

    const balanceExample = await contract.balanceOf(
        'tb1p823gdnqvk8a90f8cu30w8ywvk29uh8txtqqnsmk6f5ktd7hlyl0q3cyz4c', // Random address
    );

    if ('error' in balanceExample) throw new Error('Error in fetching balance');
    console.log('MOTO Balance:', balanceExample.decoded[0]);
}

main().catch(console.error);

You can call any function of the contract by using the contract object. This object will encode the correct calldata based of your provided ABI and simulate the call. A bunch more documentation will be available soon.

You can get the calldata generated by doing balanceExample.calldata and generate your transaction based on that. In scenarios where you are writing data on-chain, you must specify the sender in the getContract function. This is important for the simulation to pass.

Contribution

Contributions are welcome! Please read through the CONTRIBUTING.md file for guidelines on how to submit issues, feature requests, and pull requests. We appreciate your input and encourage you to help us improve OP_NET.

License

This project is open source and available under the MIT License. If you have any suggestions or contributions, please feel free to submit a pull request.