npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@btc-vision/btc-runtime

v1.3.8

Published

Bitcoin Smart Contract Runtime

Downloads

557

Readme

OPNet Smart Contract Runtime

Bitcoin AssemblyScript TypeScript NodeJS WebAssembly NPM

code style: prettier

Table of Contents

  1. Introduction
  2. Installation
  3. Core Concepts
  4. Usage Examples
  5. Advanced Topics
  6. Additional Documentation
  7. License

Introduction

The OPNet Smart Contract Runtime provides the foundational components required for creating smart contracts on Bitcoin Layer 1 (L1). Written in AssemblyScript, this runtime allows developers to leverage WebAssembly for efficient contract execution while integrating deeply with Bitcoin's decentralized architecture.

Features

  • AssemblyScript and WebAssembly: Efficient and high-performance contract execution using WebAssembly.
  • Bitcoin Integration: Direct interaction with Bitcoin L1, enabling the creation of decentralized applications that operate on the Bitcoin network.
  • Comprehensive Storage Management: Flexible and secure storage management using primary pointers and sub-pointers, ensuring data integrity through cryptographic proofs.
  • Event Handling: Sophisticated event system for contract state changes, allowing easy tracking and logging of contract activities.

Installation

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

Core Concepts

Blockchain Environment

The Blockchain object environment is the backbone of the OPNet runtime, providing essential functionality for interacting with the blockchain, such as managing contract states, handling transactions, and emitting events.

For more detailed information, see the Blockchain.md documentation.

Contracts

Contracts in OPNet are AssemblyScript classes that extend the OP_NET base class. The constructor pattern differs from Solidity's, as it runs every time a contract is instantiated, so developers should not use the constructor for persistent initialization.

For a detailed guide on how to structure contracts, refer to the Contract.md documentation.

Events

Events in OPNet allow contracts to emit signals that external observers can listen to. They are crucial for tracking state changes and interactions within the contract.

For a comprehensive explanation on how to define and use events, refer to the Events.md documentation.

Pointers and Storage Management

Storage in OPNet is managed using a combination of pointers (u16) and sub-pointers (u256). These are encoded and hashed to generate unique storage locations that are secure and verifiable. This approach ensures that the data stored is tamper-proof and can be efficiently accessed.

For more details on pointers and storage management, see the Pointers.md and Storage.md documentation.

Usage Examples

Creating a Basic Token Contract

Here is a real-world example of how to create a basic token contract using the OPNet Smart Contract Runtime. This contract follows the OP20 standard.

import {
    Address,
    Blockchain,
    BytesWriter,
    Calldata,
    DeployableOP_20,
    encodeSelector,
    Map,
    OP20InitParameters,
    Selector,
    AddressMap
} from '@btc-vision/btc-runtime/runtime';
import { u128, u256 } from 'as-bignum/assembly';

@final
export class MyToken extends DeployableOP_20 {
    public constructor() {
        super();

        // IMPORTANT. THIS WILL RUN EVERYTIME THE CONTRACT IS INTERACTED WITH. FOR SPECIFIC INITIALIZATION, USE "onDeployment" METHOD.
    }

    // "solidityLikeConstructor" This is a solidity-like constructor. This method will only run once when the contract is deployed.
    public override onDeployment(_calldata: Calldata): void {
        const maxSupply: u256 = u128.fromString('100000000000000000000000000').toU256(); // Your max supply.
        const decimals: u8 = 18; // Your decimals.
        const name: string = 'MyToken'; // Your token name.
        const symbol: string = 'TOKEN'; // Your token symbol.

        this.instantiate(new OP20InitParameters(maxSupply, decimals, name, symbol));

        // Add your logic here. Eg, minting the initial supply:
        this._mint(Blockchain.tx.origin, maxSupply);
    }

    public override execute(method: Selector, calldata: Calldata): BytesWriter {
        switch (method) {
            case encodeSelector('airdrop'):
                return this.airdrop(calldata);
            case encodeSelector('airdropWithAmount'):
                return this.airdropWithAmount(calldata);
            default:
                return super.execute(method, calldata);
        }
    }

    private airdrop(calldata: Calldata): BytesWriter {
        this.onlyOwner(Blockchain.tx.sender);

        const drops: AddressMap<u256> = calldata.readAddressValueTuple();

        const addresses: Address[] = drops.keys();
        for (let i: i32 = 0; i < addresses.length; i++) {
            const address = addresses[i];
            const amount = drops.get(address);

            this._mint(address, amount, false);
        }

        const writer: BytesWriter = new BytesWriter(1);
        writer.writeBoolean(true);

        return writer;
    }

    private _optimizedMint(address: Address, amount: u256): void {
        this.balanceOfMap.set(address, amount);

        this._totalSupply.addNoCommit(amount);

        this.createMintEvent(address, amount);
    }

    private airdropWithAmount(calldata: Calldata): BytesWriter {
        this.onlyOwner(Blockchain.tx.sender);

        const amount: u256 = calldata.readU256();
        const addresses: Address[] = calldata.readAddressArray();

        for (let i: i32 = 0; i < addresses.length; i++) {
            this._optimizedMint(addresses[i], amount);
        }

        this._totalSupply.commit();

        const writer: BytesWriter = new BytesWriter(1);
        writer.writeBoolean(true);

        return writer;
    }
}

Advanced Topics

Storage Management with Cryptographic Proofs

Storage pointers and sub-pointers are encoded and hashed to create unique and secure storage locations. These storage locations are managed using the Blockchain class's setStorageAt and getStorageAt methods, ensuring data integrity and preventing tampering.

Using Serializable Data Structures

For complex data types, the Serializable class allows you to manage and persist data structures across multiple storage slots.

class ComplexData extends Serializable {
    // Implementation
}

Additional Documentation

For more detailed explanations on specific topics, refer to the individual documentation files:

License

This project is licensed under the MIT License. View the full license here.