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 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
alias create-ckb-scripts="cargo generate gh:cryptape/ckb-script-templates workspace"
create-ckb-scripts
⚠️ 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