Yusuf Idi Maina
3 min readOct 6, 2023

sCrypt : How to Write a Smart Contract

Smart contracts are self-executing agreements with the terms of the agreement directly written into code. They are stored on a blockchain and automatically execute when predetermined terms and conditions are met. In this article, i will guide you through the process of writing a smart contract using sCrypt, a typescript framework for developing smart contracts on Bitcoin sv.

Prerequisites
Before you start, make sure you have installed the necessary tools for sCrypt development. You can follow the installation guide on the official sCrypt documentation (docs.scrypt.io).

Writing a Smart Contract
A smart contract in sCrypt is a class that extends the SmartContract base class. Here is a simple example:

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

export class Demo extends SmartContract {
@prop()
sum: bigint

@prop()
diff: bigint

// The values of the `sum` and `diff` properties get passed via the
// smart contract's constructor.
constructor(sum: bigint, diff: bigint) {
super(...arguments)
this.sum = sum
this.diff = diff
}

// Public method which can be unlocked by providing the solution `x` and `y`
// for the two equations `x + y = summ` and `x - y = diff`.
@method()
public unlock(x: bigint, y: bigint) {
assert(x + y == this.sum, 'incorrect sum')
assert(x - y == this.diff, 'incorrect diff')
}
}

In this example, Demo is a smart contract with two properties sum and diff, and one method unlock. The `@prop` decorator is used to declare properties that will end up on the blockchain, and the `@method` decorator is used to declare methods that can be called on-chain.
The unlock method in this example takes two arguments x and y, and asserts that their sum and difference match the stored sum and diff respectively. If the assertions pass, the contract is unlocked.

Understanding the Decorators
The `@prop` and `@method` decorators are used to indicate which parts of the contract will end up on the blockchain. Class members decorated with `@prop` and `@method` must be a strict subset of TypeScript, as they will be executed in the on-chain context. Members decorated with neither are regular TypeScript and are kept off-chain.

Testing the Contract
Before deploying a smart contract, it’s crucial to test it thoroughly to avoid any potential issues that could lead to economic losses. sCrypt provides a testing framework that allows you to write tests for your contracts. Here is an example of how to test the Demo contract:

import { expect, use } from 'chai' 
import { Demo } from '../src/contracts/demo'
import { getDefaultSigner } from './utils/txHelper'
import chaiAsPromised from 'chai-as-promised'

// https://www.chaijs.com/plugins/chai-as-promised/
// https://stackoverflow.com/a/40842060
use(chaiAsPromised)

describe('Test SmartContract `Demo`', () => {
let demo: Demo

before(async () => {
Demo.loadArtifact()

demo = new Demo(10n, -4n)
await demo.connect(getDefaultSigner())
})

it('should pass `unlock` with correct solution', async () => {
await demo.deploy(1)
const callContract = async () => demo.methods.unlock(3n, 7n)
return expect(callContract()).not.rejected
})

it('should throw when calling `unlock` with wrong solution', async () => {
await demo.deploy(1)
const callContract = async () => demo.methods.unlock(4n, 6n)
return expect(callContract()).to.be.rejectedWith(/incorrect diff/)
})
})

Conclusion
Writing a smart contract with sCrypt involves defining a class that extends the SmartContract base class, using the `@prop` and `@method` decorators to define on-chain properties and methods, and writing tests to ensure the contract behaves as expected. The sCrypt framework provides a powerful and flexible way to write smart contracts for the Bitcoin sv blockchain.

Additional Considerations
In addition to the above, here are a few other things to keep in mind when writing smart contracts with sCrypt:

Use the sCrypt documentation and resources (docs.scrypt.io). The sCrypt team provides a comprehensive documentation and resource library to help you learn more about the framework and how to use it.

Join the sCrypt community. There is a thriving sCrypt community on Discord and other forums. These communities are a great place to ask questions and get help from other developers.

slack : (https://join.slack.com/t/scryptworkspace/shared_invite/zt-1zeoorxyt-zggK4fGc31swt94~1X7M8w)

discord : (https://discord.gg/bsv)