A Beginner's Guide to Smartcontract development on BSV
Tags: webdev, blockchain, smartcontract, scrypt
Published: 2024-05-20 21:27:54


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkrqt33a7ef8r0iz1oq1.png)
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.<br>Explore about the cutting edge of BSV blockchain and it key features by enrolling on [BSV academy ](https://academy.bsvblockchain.org).
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.
<h2> What is Smart Contract?</h2>
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbeo8y5yhy41vql90m50.jpg)
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.
<h2>How do Bitcoin Smart Contracts work?</h2>
Smart contracts on Bitcoin are based on the UTXO model.Each bitcoin transaction consists of some inputs and outputs.<br>
An output contains:

- The amount of bitcoins (satoshis) it contains.

- bytecodes (the `locking script`).<br>
While an input contains:

- A reference to the previous transaction output.

- bytecodes (the `unlocking script`).<br>
<h2>UTXO Model</h2>
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04e64ocnnht4wokiu2zz.jpg)
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.<br>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.
<h2> A little about sCrypt </h2>
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;

```JavaScript 
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')
  }

}
```
The smart contract above requires solving for two equations with unknown variables, `x` and `y`.<br>

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.
<h2>Conlusion</h2>
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](https://scrypt.io).