Skip to main content

4.6 Off-Chain Smart Contracts

Smart contracts utilize the advanced Bitcoin smart contract language provided by sCrypt to ensure that the indexing validators complete execution within a limited time. This also supports advanced use cases including Swaps, Oracles, and Zero-Knowledge Proofs among others.

An example of a simple off-chain contract is as follows:

export class N20_Sample extends SmartContract {
@prop()
readonly tick: ByteString

@prop()
readonly max: bigint

@prop()
readonly lim: bigint

@prop()
readonly dec: bigint

constructor(tick: ByteString, max: bigint, lim: bigint, dec: bigint) {
super(...arguments)
this.tick = tick
this.max = max
this.lim = lim
this.dec = dec
}

@method()
public check(
bh: BlockHeader,
merkleproof: MerkleProof,
tx: ByteString,
prevTx: ByteString
) {
// verify transaction in the block
const txid = hash256(tx)
assert(Blockchain.txInBlock(txid, bh, merkleproof))
}

@method()
public mint(tick: ByteString, amt: bigint) {
assert(tick == this.tick, 'Tick does not match')
assert(amt <= this.lim, 'Amount check failed')
}

@method()
public transfer(tick: ByteString, amt: bigint) {
assert(tick == this.tick, 'Tick does not match')
assert(amt <= this.lim, 'Amount check failed')
}
}

In this segment, the check function is responsible for ascertaining the presence of a Bitcoin transaction within a block. The mint and transfer functions are tasked with validating the feasibility of executing the two specific actions associated with N20. The indexer commits to updating the record of account balances solely upon successful contract validation.