BSV-20 Tokens codes

Integrate Ordinals with smart contracts on Bitcoin: Part 3

Getting your Trinity Audio player ready...

This post was first published on Medium. Read Part 1 here and Part 2 here.

Previously, we have shown how to integrate Bitcoin smart contracts with Ordinals, which can be regarded as NFTs on chain. In this article, we show how to integrate them with the fungible version, i.e., BSV-20 tokens. We use auction as an example as before, which works the same as the NFT version with only minimal changes.

BSV-20 Tokens image

How BSV-20 fungible tokens work

BSV-20¹ is a fungible token standard on the BSV network, inspired by the BRC-20 standard on BTC.

Ordinal inscriptions are comprised of a content type, also referred to a MIME type, and content, which is an arbitrary byte string.

In BSV-20, you create an Ordinal inscription output with content type of application/bsv-20 and content of a JSON.


For example, to deploy an ordi token with a max supply of 21000000, you create an inscription with the following JSON.


To mint 1000 ordi tokens, you inscribe the following JSON.


Tokens are stored within Unspent Transaction Outputs (UTXOs), much like how native bitcoins are held. To initiate token transfers, you spent the designated UTXO and generate new outputs using the same process as spending regular satoshis, except these new output(s) need to contain transfer inscriptions. This differs from Ordinal NFTs, where the inscription is only needed at minting and is dropped when transferring.

For instance, to transfer the tokens minted above, you would create a transaction with an input spending the minting UTXO (providing the signature and public key normally to spend the P2PKH script), with outputs containing the following inscriptions. It splits the 1000 tokens into three outputs, each containing 100, 500, and 400 tokens, respectively.

Transfer image

For more details on how BSV-20 tokens work, please read the official documentation.

Auctioning BSV-20 tokens

The auction smart contract is the almost same as the one used to auction an ordinal NFT in our previous article.

Last Bid, Close image

The only difference lies in the “close” public method. In addition to checking the first output goes to the winning bidder, we also check it contains the expected transfer inscription at the very beginning. If we omit this check, the BSV-20 tokens, carried by the unlocked UTXO, would be deemed burned.

Contract code

Suppose we are auctioning the 1000 ordi tokens minted above, “this.transferInscription” would be the following:

[1] We use V1, First is first mode, of BSV-20 here. V2, Tickerless Mode, can be supported with slight modification.

Watch: The Bitcoin Masterclasses #1 Day 2 with Craig Wright: Private Identity, Identity Proofs, Topic Reviews

YouTube video

New to blockchain? Check out CoinGeek’s Blockchain for Beginners section, the ultimate resource guide to learn more about blockchain technology.