DEV Community

John Godwin
John Godwin

Posted on

A Beginner's Guide to Smartcontract development on BSV

Image description
The rapid growth of blockchain technology has capture the attention of investors, technologists and everyday individuals alike, but despite it growing popularity, many people still find the concept of blockchain daunting.However, the Bitcoin SV blockchain amongst many others offers robust scalable,secure and micropayment features for enterprise, investors and developers.
Explore about the cutting edge of BSV blockchain and it key features by enrolling on BSV academy .
In this article, i will be providing you the basic introduction of smartcontract on Bitcoin SV and showing you a simple smartcontract sample with sCrypt.

What is Smart Contract?

Smartcontract is a computer program or script on a blockchain that automatically executes agreements when predetermined conditions are met. Once the conditions are met, the code executes automatically, ensuring a transparent and tamper-proof process.

Image description1
Think of it like a vending machine: you put in the money (fulfill a condition), and the machine dispenses the product (automatically executes an action) without needing a middleman.

How do Bitcoin Smart Contracts work?

Smart contracts on Bitcoin are based on the UTXO model.Each bitcoin transaction consists of some inputs and outputs.

An output contains:

  • The amount of bitcoins (satoshis) it contains.

  • bytecodes (the locking script).

    While an input contains:

  • A reference to the previous transaction output.

  • bytecodes (the unlocking script).

    UTXO Model

    UTXO stands for unspent transaction output. this is referred to the portion of bitcoin not yet spend or used as an input in another transaction.An Unspent Transaction Output (UTXO) is an output not consumed in any transaction yet. The low-level bytecode/opcode is called Bitcoin Script, which is interpreted by the Bitcoin Virtual Machine (BVM).

Image description2
the image above shows and explain the concept of bitcoin UTXO model and how smartcontract works on bitcoin.In the example above, we have two transactions, each having one input (in green) and one output (in red) and the transaction on the right spends that on the left. The locking script can be regarded as a boolean function f that specifies conditions to spend the bitcoins in the UTXO, acting as a lock (thus the name "locking"). The unlocking script in turns provides the function arguments that makes f evaluates to true, i.e., the "key" (also called witness) needed to unlock. Only when the “key” in an input matches previous output’s “lock”, it can spend bitcoins contained in the output.
Now that you have gotten a good introduction on what is smartcontract and how it works on bitcoin, let's move on explaining how write smartcontract using sCrypt.

A little about sCrypt

sCrypt is a typescript framework for writing smartcontracts on Bitcoin SV. it is an embedded Domain Specific Language (eDSL) based on TypeScript.
A smartcontract is a class that extends the smartcontract base class. Here is an example of a smartcontract;

import { SmartContract, method, prop, assert } from "scrypt-ts"

class Equations extends SmartContract {

  @prop()
  sum: bigint

  @prop()
  diff: bigint

  constructor(sum: bigint, diff: bigint) {
    super(...arguments)
    this.sum = sum
    this.diff = diff
  }

  @method()
  public unlock(x: bigint, y: bigint) {
    assert(x + y == this.sum, 'incorrect sum')
    assert(x - y == this.diff, 'incorrect diff')
  }

}
Enter fullscreen mode Exit fullscreen mode

The smart contract above requires solving for two equations with unknown variables, x and y.

Class members decorated with @prop and @method will end up on the blockchain and thus must be a strict subset of TypeScript. Everywhere decorated with them can be regarded in the on-chain context. Members decorated with neither are regular TypeScript and are kept off chain. The significant benefit of sCrypt is that both on-chain and off-chain code are written in the same language: TypeScript.

Conlusion

In the next part of this guide you will learn how to write,deploy and call your first smartcontract using sCrypt, and you can also learn more about sCrypt here.

Top comments (0)