BVM
  • About bvm
    • Development infrastructure for Bitcoin
    • Follow our progress
  • getting started
    • Build a Bitcoin L2 with BVM Studio
    • Level up with Bitcoin dapps
    • Deploy your own Bitcoin dapp
  • L1 Scaling solutions
    • What are Bitcoin Shards? [WIP]
    • Compute sharding [WIP]
    • Data sharding [WIP]
    • Case studies [WIP]
      • SHARD_BVM
        • Bitcoin Virtual Machine #0
      • SHARD_DA [WIP]
      • SHARD_AI [WIP]
  • L2 Scaling solutions
    • What are Bitcoin rollups?
    • Optimistic rollups
    • ZK rollups
  • l3 scaling solutions
    • What are Bitcoin appchains?
    • Rollups from appchains to L2s
  • Bitcoin decentralized bridges
    • Bitcoin <> BVM bridge
    • Bitcoin <> Ethereum bridge [WIP]
    • Bitcoin <> Solana bridge [WIP]
  • bvm studio
    • Overview
    • Build a Bitcoin L2
    • Monitor a Bitcoin L2
    • Scale a Bitcoin L2
    • Building blocks
  • bitcoin heartbeats
    • Overview
    • Chain heartbeats
      • Key metrics
      • Bitcoin's 5 levels of scalability
    • Wallet heartbeats [WIP]
    • Dapp heartbeats [WIP]
  • bitcoin api
    • RaaS API
    • Chain API [WIP]
  • bitcoin dapps
    • Overview
    • EVM code tutorials
      • Build a Bitcoin Name System
      • Build an Ordinals alternative
      • BFS: Build an IPFS alternative
      • Decentralized AI
      • Auction
      • Decentralized Discord
      • Fully onchain Tic-Tac-Toe
      • BRC-721: NFTs
      • Operate your project using a DAO
      • Raise funds through a Crowdsale
      • Issue your own governance token
    • SVM code tutorials [WIP]
Powered by GitBook
On this page
  • Write an Ordinals Protocol smart contract
  • Clone the smart contract examples
  • Compile the contracts
  • Deploy the contracts
  • Interact with the contracts
  1. bitcoin dapps
  2. EVM code tutorials

Build an Ordinals alternative

Let's implement a simplified version of the Ordinals Protocol from scratch with Bitcoin Virtual Machine.

  • create an inscription

  • retrieve an inscription

  • send an inscription

  • buy and sell inscriptions

Write an Ordinals Protocol smart contract

It turns out that writing an Ordinals Protocol smart contract is very simple; all it is is a file storage system inside the Bitcoin network. Here is a basic contract to provide an Ordinals-like inscription system on Bitcoin Virtual Machine.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract Ordinals is ERC721 {

        using Counters for Counters.Counter;
        Counters.Counter private _inscriptionNumbers;

        mapping(uint256 => bytes) public inscriptions;

        constructor() ERC721("Ordinals", "ORD") {}

        function inscribe(address owner, bytes memory inscription) 
                public 
                returns (uint256) 
        {
                uint256 num = _inscriptionNumbers.current();
                _mint(owner, num);
                inscriptions[num] = inscription;

                _inscriptionNumbers.increment();
                return num;
        }
}

Extending ERC-721, we only need to implement the inscribe() function to create an inscription as an NFT. Sending and receiving an inscription is now as simple as sending and receiving an NFT. You can also trade the inscription on open markets since it's an ERC-721.

Clone the smart contract examples

We've prepared a few different examples for you to get started. The Ordinal Theory example is located at smart-contract-examples/contracts/Ordinals.sol.

git clone https://github.com/trustlesscomputer/smart-contract-examples.git

Compile the contracts

To compile your contracts, use the built-in hardhat compile task.

cd smart-contract-examples
npm install
npx hardhat compile

Deploy the contracts

Review config file hardhat.config.ts. The network configs should look like this.

  networks: {
    mynw: {
      url: "http://localhost:10002",
      accounts: {
        mnemonic: "<your mnemonic with funds>"
      },
      timeout: 100_000,
    },
    blockscoutVerify: {
      blockscoutURL: "http://localhost:4000", // your explorer URL
      ...
    }
  }

Run the deploy scripts using hardhat-deploy.

npx hardhat deploy --tags Ordinals

Make sure the accounts in hardhat.config.ts have some $BVM.

Interact with the contracts

Once the contracts are deployed, you can interact with them. We've prepared a few hardhat tasks to make it easy for you to interact with the contracts.

# create an inscription
npx hardhat ord-inscribe --data <raw-hex>

# your new inscriptions will be given id=0,1,...
# read an inscription
npx hardhat ord-read --id <inscription-number>
PreviousBuild a Bitcoin Name SystemNextBFS: Build an IPFS alternative

Last updated 10 months ago