A General Method to Describe a "Smart Contract" on CKB


The morphology of smart contract on CKB is quite different from that on an account based blockchain like Ethereum. We need a new conception to define what a CKB smart contract is and a new routine to describe how to call a CKB smart contract.

What is CKB “Smart Contract”

As we know, the official name of smart contract on CKB is “script”. We have two types of script, lock script and type script. Sometimes, we call them lock and type for short. To dive deep on lock and type, you can refer to these posts: Introduction to CKB Script Programming 1: Validation Model and Introduction to CKB Script Programming 2: Script Basics.

Compared with account based smart contract, scripts have these features:

  • Smart contracts focus on execution, while scripts focus on state.
  • Smart contracts coupling states and logic codes, while scripts decoupling them.
    • Which means we store code at one place, and the state at another place.
    • One script code can be reused as multiple instances.

How to Execute a CKB “Smart Contract”

Every operation on CKB is a description of state transition, including script execuation. To describe an action of a script, we need to know or define that in such a transaction,

  • Input cells should meet
    • specific type script(s)
    • specific lock script(s)
    • specific data
  • The deps cell list
  • Output cells should meet
    • specific type script(s)
    • specific lock script(s)
    • specific data
  • Logic
    • the constraint that input and output cell shoud follow
    • the witness should be provided

To execute a CKB “smart contract”, one should gather cells that follow the input cell constraint, prepare witness and generate output cells according to the logic description, and finally sign the TX to unlock the input cells and make the TX immutable.

An Example

Here is a simple bank smart contract on CKB. This virtual on-chain bank has two functions, deposit and withdraw, no interest and commission included. And the bank holds deposits in collateral cells.

Deposit Action

  • Input Cells
    • Normal cell
      • description: provides users’ CKB token to deposit
      • number of such cells: one or more
    • Collateral Cell
      • description: the bank’s previous collateral cell
      • capacity: the amount that users already deposited
      • number of such cells: zero or more
      • lock script: Lock_Deposit_Pool
  • Output Cells
    • Deposit record cell
      • number of such cells: one or more
      • capacity: same as used cell storage bytes
      • data: 64bit number of deposit amount
      • type script: Type_Bank_Receipt
      • lock script: user defined lock
    • Collateral Cell
      • capacity: greater than the original capacity of input collateral cell, and less than that plus the total capacity of other input cells. (note that we need subtract some capacity for mining fee)
      • type script: none
      • lock script: Lock_Deposit_Pool
      • number of such cells: one or more
  • deps
    • Type_Bank_Receipt code cell
    • Lock_Deposit_Pool code cell
  • Logic of Lock_Deposit_Pool and Type_Bank_Receipt
    • already described in the input / output cell definition section.
+----------------+                  +------------------------+
| Normal Cells   |                  | Deposit Record Cell    |
+----------------+                  +------------------------+
| provide CKB    |                  | Data: deposit amount   |
|                |                  +------------------------+
+----------------+     deposit      | Type: Type_Bank_Receipt|
                     +----------->  | Lock: user_defined     |
+----------------+     action       +------------------------+
| Collateral Cell|
+----------------+                  +-------------------+
| Optional       |                  | Collateral Cells  |
|                |                  +-------------------+
+----------------+                  | hold bank's       |
                                    | total CKB capacity|
                                    | Lock:             |
                                    | Lock_Deposit_Pool |

Withdraw Action

  • Input Cells
    • one or more Deposit Record Cells
    • one or more Collateral Cells
  • Output Cells
    • one or more normal cells
    • zero or more Collateral Cells
    • zero or more Deposit Record Cells
  • Logic
    • output normal cells’s total capacity + miner’s fee = sum of input Deposit Record Cells’ data field number - sum of output Deposit Record Cells’ data field number

Quick Recap

  • CKB‘s smart contracts are called script
  • Script is the verification logic of special action(s)
  • A Ethereum-like smart contract on CKB can be considerated as a set of script actions
  • We constrain the input cell / output cell / verification logic to define a script action
1 Like