Example: Simple UDT Script
User-Defined Token (UDT) is a fungible token standard on CKB blockchain.
In this tutorial, we will create a UDT Script, a simplified version of XUDT standard to help you better understand how fungible tokens work on CKB.
It is highly recommended to go through the dApp tutorial Create a Fungible Token first before writing your own UDT Script.
The full code of the UDT Script in this tutorial can be found at Github.
Data Structure for simple UDT Cellβ
data:
<amount: uint128>
type:
code_hash: UDT type script hash
args: <owner lock script hash>
lock: <user_defined>
Here the issuer's Lock Script Hash
works like the unique ID for the custom token.
Different Lock Script Hashes correspond to different types of token issued by different owners.
This hash is also used to determine if a transaction is initiated by the token issuer or a regular token holder, to apply different security checks.
- Token Owner: Can perform any operation.
- Regular Holders: The UDT Script ensures that the amount in the output Cells does not exceed that in the input Cells. For a more detailed explanation, please refer to Create a Fungible Token or RFC0025: Simple UDT
Now, let's create a new project to build the UDT Script. We will use OFFCKB and ckb-script-templates.
Initialize a Script Projectβ
Let's run the command to generate a new Script project called sudt-script
(short for simple UDT):
- Command
- Response
offckb create --script sudt-script
β οΈ Favorite `gh:cryptape/ckb-script-templates` not found in config, using it as a git repository: https://github.com/cryptape/ckb-script-templates.git
π€· Project Name: sudt-script
π§ Destination: /tmp/sudt-script ...
π§ project-name: sudt-script ...
π§ Generating template ...
π§ Moving generated files into: `/tmp/sudt-script`...
π§ Initializing a fresh Git repository
β¨ Done! New project created /tmp/sudt-script
This command sets up a boilerplate project called sudt-script
and does not automatically generate an sUDT Script.
Create a New Scriptβ
Letβs create a new Script called sudt
inside the project.
- Command
- Response
cd sudt-script
make generate
π€· Project Name: sudt
π§ Destination: /tmp/sudt-script/contracts/sudt ...
π§ project-name: sudt ...
π§ Generating template ...
π§ Moving generated files into: `/tmp/sudt-script/contracts/sudt`...
π§ Initializing a fresh Git repository
β¨ Done! New project created /tmp/sudt-script/contracts/sudt
After running the make generate
, the boilerplate for the sUDT Script is generated inside the project.
Now our project is successfully setup. You can run tree .
to view the project structure:
- Command
- Response
tree .
.
βββ Cargo.toml
βββ Makefile
βββ README.md
βββ contracts
βΒ Β βββ sudt
βΒ Β βββ Cargo.toml
βΒ Β βββ Makefile
βΒ Β βββ README.md
βΒ Β βββ src
βΒ Β βββ main.rs
βββ scripts
βΒ Β βββ find_clang
βΒ Β βββ reproducible_build_docker
βββ tests
βββ Cargo.toml
βββ src
βββ lib.rs
βββ tests.rs
7 directories, 12 files
contracts/sudt/src/main.rs
contains the source code of the sUDT Script, and tests/tests.rs
provides the unit tests for our Scripts. We will introduce the tests after completing the Script.