CKB Contract Flow

Opticrum(Fiber 流动性租赁)Lock Script 约束Opticrum (Fiber liquidity rental) Lock Script Constraints

来源:Opticrum/ckb-contract-script @803523a contracts/opticrum/src、opticrum-protocol/src/lib.rs · 框架 ckb-cinnabar-verifier @f50d8e9 · testnet type_id 0x1f84…0247(deployment/testnet/opticrum.json)Source: Opticrum/ckb-contract-script @803523a contracts/opticrum/src, opticrum-protocol/src/lib.rs · framework ckb-cinnabar-verifier @f50d8e9 · testnet type_id 0x1f84…0247 (deployment/testnet/opticrum.json)
Lock 脚本Lock scriptDeFiDeFi支付通道 / FiberPayment channel / Fiber测试网Testnet开源Open source社区Community
贡献者:Contributed by @ashuralyk @ashuralyk

给 AI agent:For AI agents: opticrum_lock.md · llms.txt

0 → Order买方挂单(lock 不执行)Buyer posts an Order (lock not executed)

Inputs 输入Inputs
买方资金buyer funds任意 cellany cells
Outputs 输出Outputs
Order cellCell{ lock: OPTICRUM{ args: fiber_pubkey ‖ buyer_lock_hash }, data: xudt_amount ‖ channel_capacity ‖ shannons_per_block }
Order lock.args(65B)Order lock.args (65B)
fiber_pubkey33B
buyer_lock_hash32B
  • fiber_pubkey33B买方 Fiber 节点公钥(压缩 secp256k1);合约只用于比较 argsbuyer Fiber node pubkey (compressed secp256k1); the contract only compares it as args35
  • buyer_lock_hash32B买方 lock hash:取消 / 注资 / 取回的授权buyer lock hash: authorizes cancel / inject / withdraw长度合计 = 65total length = 6535
fiber_pubkey0x037aa2f692451e2f14f0fb1305e0f2a7e564fde080c44649dfe18c1e12d4e8f262
buyer_lock_hash0x716619d4cc4d8ca5ae7f67b9bf26bdff2acd8a8139fa96a2196e4c2e57cf6753
示例:testnet tx 0xd5e460e4… 的输入 Order cellExample: the input Order cell of testnet tx 0xd5e460e4…
Order data(恰好 32B)Order data (exactly 32B)
xudt_amount16B u128 LE
channel_capacity8B u64 LE
shannons_per_block8B u64 LE
  • xudt_amount16B u128 LE提供的 xUDT 租金(CKB 模式为 0)xUDT rent offered (0 in CKB mode)25
  • channel_capacity8B u64 LE要求的通道容量(shannons)requested channel capacity (shannons)25
  • shannons_per_block8B u64 LE每块租金rent per block25
xudt_amount0x00000000000000000000000000000000
channel_capacity0x0030ef7dba020000 (3 000 000 000 000)
shannons_per_block0xb169010000000000 (92 593)

限制Constraints

  • 创建creationlock 只在被消费时运行:创建时 args / data 不做任何检查,错误格式的 Order 只会在消费时失败a lock only runs when spent: args / data are not checked on creation; a malformed Order only fails when spent—

Order → 0买方取消挂单Buyer cancels the Order

Inputs 输入Inputs
Order cellCell{ lock: OPTICRUM{ args: … ‖ buyer_lock_hash }, data: … }
买方 cellbuyer cellCell{ lock: hash = buyer_lock_hash }
Outputs 输出Outputs
无 Opticrum 输出no Opticrum output资金任意去向funds go anywhere
Order lock.args(65B)Order lock.args (65B)
fiber_pubkey33B
buyer_lock_hash32B
  • fiber_pubkey33B买方 Fiber 节点公钥(压缩 secp256k1);合约只用于比较 argsbuyer Fiber node pubkey (compressed secp256k1); the contract only compares it as args35
  • buyer_lock_hash32B买方 lock hash:取消 / 注资 / 取回的授权buyer lock hash: authorizes cancel / inject / withdraw长度合计 = 65total length = 6535
fiber_pubkey0x037aa2f692451e2f14f0fb1305e0f2a7e564fde080c44649dfe18c1e12d4e8f262
buyer_lock_hash0x716619d4cc4d8ca5ae7f67b9bf26bdff2acd8a8139fa96a2196e4c2e57cf6753
示例:testnet tx 0xd5e460e4… 的输入 Order cellExample: the input Order cell of testnet tx 0xd5e460e4…

限制Constraints

  • lock.args长度 65 → Order,133 → Match,其他 → 35length 65 → Order, 133 → Match, else → 3535
  • 输出 Opticrum celloutput Opticrum cell第一个 lock code_hash 相同、hash_type = type 的输出(不比较 args)the first output whose lock has the same code_hash and hash_type = type (args not compared)—
  • Order data恰好 32Bexactly 32B25
  • 路由routeOrder 且无 Opticrum 输出 → OrderCancelOrder with no Opticrum output → OrderCancel40
  • 授权authorizationbuyer_lock_hash 等于某个输入的 lock hashbuyer_lock_hash equals some input lock hash36

Order → Match卖方开通道并撮合Seller opens a channel and matches

Inputs 输入Inputs
Order cellCell{ lock: OPTICRUM{ args: order_args }, data: xudt_amount ‖ channel_capacity ‖ shannons_per_block }
卖方 cellseller cellCell{ lock: hash = seller_lock_hash }
Outputs 输出Outputs
Match cellCell{ lock: OPTICRUM{ args: order_args ‖ channel_outpoint ‖ seller_lock_hash }, data: xudt_amount ‖ shannons_per_block ‖ last_extraction_block = 0 }
Match lock.args(133B)Match lock.args (133B)
order_args65B
channel_outpoint36B
seller_lock_hash32B
  • order_args65B= 原 Order args(fiber_pubkey ‖ buyer_lock_hash)= the original Order args (fiber_pubkey ‖ buyer_lock_hash)撮合时必须相等must be equal on match40
  • channel_outpoint36BFiber 通道 funding cell:tx_hash 32B ‖ index u32 LEFiber channel funding cell: tx_hash 32B ‖ index u32 LE必须在 cell_deps 中must be in cell_deps22
  • seller_lock_hash32B卖方 lock hash:撮合 / 提取 / 销毁的授权seller lock hash: authorizes match / extract / destroy长度合计 = 133total length = 13335
order_args0x037aa2…f262 ‖ 0x716619…6753
channel_outpoint0xfade972d2b5d27890345dd06b69985752d85183f417c18916464f29cbb1847e9 ‖ 00000000
seller_lock_hash0x5cbfb4398b8d8c080e6e68da34bce43761229d70c7eff56d8502e755b7516980
示例:testnet tx 0xd5e460e4… 的输出 Match cellExample: the output Match cell of testnet tx 0xd5e460e4…
Match data(≥ 32B,只读前 32B)Match data (≥ 32B, only the first 32B are read)
xudt_amount16B u128 LE
shannons_per_block8B u64 LE
last_extraction_block8B u64 LE
  • xudt_amount16B u128 LE剩余 xUDT 租金remaining xUDT rent撮合时 = Order.xudt_amount= Order.xudt_amount on match26
  • shannons_per_block8B u64 LE每块租金rent per block永不改变never changes33
  • last_extraction_block8B u64 LE卖方上次提取的块高(0 = 从未)block of the seller’s last extraction (0 = never)撮合时 = 0= 0 on match28 / 30
xudt_amount0x00000000000000000000000000000000
shannons_per_block0xb169010000000000
last_extraction_block0x0000000000000000
cell_deps: [ opticrum code, Fiber 通道 funding cell = channel_outpoint ]cell_deps: [ opticrum code, Fiber channel funding cell = channel_outpoint ]
header_deps: [ Order cell 所在块, 通道 cell 所在块 ]header_deps: [ Order cell block, channel cell block ]

限制Constraints

  • lock.args长度 65 → Order,133 → Match,其他 → 35length 65 → Order, 133 → Match, else → 3535
  • 输出 Opticrum celloutput Opticrum cell第一个 lock code_hash 相同、hash_type = type 的输出(不比较 args)the first output whose lock has the same code_hash and hash_type = type (args not compared)—
  • 路由route输出为 Match、Match.order_args = Order args、xUDT(数量 + type)相同 → OrderMatch;否则 40output is Match, Match.order_args = Order args, same xUDT (amount + type) → OrderMatch; else 4040
  • Match data≥ 32B≥ 32B28
  • channel_outpoint在 cell_deps 中;该 cell lock 为 Fiber funding(code_hash = testnet 0x6c67…6d7c / mainnet 0xe45b…f4f1 / mock,hash_type type);capacity ≥ channel_capacity,或 xUDT 模式下同 type 且数量 ≥ xudt_amountin cell_deps; its lock is Fiber funding (code_hash = testnet 0x6c67…6d7c / mainnet 0xe45b…f4f1 / mock, hash_type type); capacity ≥ channel_capacity, or in xUDT mode same type and amount ≥ xudt_amount22
  • 授权authorizationseller_lock_hash 在输入中seller_lock_hash among inputs37
  • Match datashannons_per_block = Order 的值,last_extraction_block = 0shannons_per_block = Order value, last_extraction_block = 028
  • capacityOrder 与 Match 的未占用 capacity 相等unoccupied capacity of Order and Match equal23
  • xudt_amountMatch = OrderMatch = Order26
  • header_deps含 Order cell 与通道 cell 所在块;缺失 → 29contain the Order cell block and the channel cell block; missing → 2929
  • 时序ordering通道 cell 块高 > Order cell 块高channel cell block > Order cell block24

Match → Match (seller)卖方提取租金Seller extracts rent

Inputs 输入Inputs
Match cellCell{ lock: OPTICRUM{ args: order_args ‖ channel_outpoint ‖ seller_lock_hash }, data: xudt_amount ‖ rate ‖ base }
卖方 cellseller cellCell{ lock: hash = seller_lock_hash }
Outputs 输出Outputs
Match cellCell{ lock: OPTICRUM{ args: 不变 }, data: xudt_amount ‖ rate ‖ tip capacity: − rate × (tip − base) }Cell{ lock: OPTICRUM{ args: unchanged }, data: xudt_amount ‖ rate ‖ tip capacity: − rate × (tip − base) }
Match data(≥ 32B,只读前 32B)Match data (≥ 32B, only the first 32B are read)
xudt_amount16B u128 LE
shannons_per_block8B u64 LE
last_extraction_block8B u64 LE
  • xudt_amount16B u128 LE剩余 xUDT 租金remaining xUDT rent撮合时 = Order.xudt_amount= Order.xudt_amount on match26
  • shannons_per_block8B u64 LE每块租金rent per block永不改变never changes33
  • last_extraction_block8B u64 LE卖方上次提取的块高(0 = 从未)block of the seller’s last extraction (0 = never)撮合时 = 0= 0 on match28 / 30
xudt_amount0x00000000000000000000000000000000
shannons_per_block0xb169010000000000
last_extraction_block0x0000000000000000
cell_deps: [ opticrum code, 通道 cell 仍存活 ]cell_deps: [ opticrum code, channel cell still live ]
header_deps: [ tip 块(索引 0), Match cell 所在块(首次提取时) ]header_deps: [ tip block (index 0), Match cell block (first extraction) ]

限制Constraints

  • lock.args长度 65 → Order,133 → Match,其他 → 35length 65 → Order, 133 → Match, else → 3535
  • 输出 Opticrum celloutput Opticrum cell第一个 lock code_hash 相同、hash_type = type 的输出(不比较 args)the first output whose lock has the same code_hash and hash_type = type (args not compared)—
  • 路由routeMatch args 相同、xUDT(数量 + type)相同 → MatchUpdatesame Match args and xUDT (amount + type) → MatchUpdate40
  • Match args不变unchanged31
  • shannons_per_block不变unchanged33
  • 授权authorization恰好一方:seller 在输入中、buyer 不在;两者都在或都不在 → 38exactly one party: seller among inputs, buyer not; both or neither → 3838
  • channel_outpoint仍在 cell_deps 中(通道未关闭)still in cell_deps (channel not closed)22
  • 耗尽exhaustion旧状态未耗尽:剩余价值 > rate × (tip − base)(错误名 MatchNotExhausted,语义相反)old state not exhausted: remaining value > rate × (tip − base) (error named MatchNotExhausted, opposite meaning)32
  • 提取量extraction未占用 capacity 减少量 = rate × (tip − base);base = last_extraction_block,或为 0 时取 Match cell 所在块unoccupied capacity decrease = rate × (tip − base); base = last_extraction_block, or the Match cell block when it is 027
  • last_extraction_block= header_deps[0] 的块高= block number of header_deps[0]30
  • 犹豫期hesitation首次提取(base = 0):tip − Match cell 块高 > 3600first extraction (base = 0): tip − Match cell block > 360042
  • header_deps缺失 → 29missing → 2929

Match → Match (buyer)买方取回 / 注资Buyer withdraws / injects

Inputs 输入Inputs
Match cellCell{ lock: OPTICRUM{ args: order_args ‖ … }, data: xudt_amount ‖ rate ‖ last_extraction_block }
买方 cellbuyer cellCell{ lock: hash = buyer_lock_hash }
Outputs 输出Outputs
Match cellCell{ lock: OPTICRUM{ args: 不变 }, data: … ‖ last_extraction_block 不变 capacity: 全部取回 → 未占用 0 | 增加 }Cell{ lock: OPTICRUM{ args: unchanged }, data: … ‖ last_extraction_block unchanged capacity: dump all → 0 unoccupied | increase }
Match data(≥ 32B,只读前 32B)Match data (≥ 32B, only the first 32B are read)
xudt_amount16B u128 LE
shannons_per_block8B u64 LE
last_extraction_block8B u64 LE
  • xudt_amount16B u128 LE剩余 xUDT 租金remaining xUDT rent撮合时 = Order.xudt_amount= Order.xudt_amount on match26
  • shannons_per_block8B u64 LE每块租金rent per block永不改变never changes33
  • last_extraction_block8B u64 LE卖方上次提取的块高(0 = 从未)block of the seller’s last extraction (0 = never)撮合时 = 0= 0 on match28 / 30
xudt_amount0x00000000000000000000000000000000
shannons_per_block0xb169010000000000
last_extraction_block0x0000000000000000
header_deps: [ tip 块(索引 0), Match cell 所在块 ]header_deps: [ tip block (index 0), Match cell block ]

限制Constraints

  • lock.args长度 65 → Order,133 → Match,其他 → 35length 65 → Order, 133 → Match, else → 3535
  • 输出 Opticrum celloutput Opticrum cell第一个 lock code_hash 相同、hash_type = type 的输出(不比较 args)the first output whose lock has the same code_hash and hash_type = type (args not compared)—
  • Match args不变unchanged31
  • shannons_per_block不变unchanged33
  • 授权authorization恰好一方:buyer 在输入中、seller 不在exactly one party: buyer among inputs, seller not38
  • last_extraction_block不变unchanged30
  • xUDT type不变unchanged31
  • 取回withdraw价值减少时必须全部取出(未占用 capacity 与 xUDT 均为 0)a decrease must dump everything (unoccupied capacity and xUDT both 0)43
  • 取回窗口withdraw window仅在犹豫期内:卖方从未提取且 tip − Match cell 块高 ≤ 3600only inside the hesitation window: seller never extracted and tip − Match cell block ≤ 360041
  • 注资inject价值增加时不得处于犹豫期an increase must be outside the hesitation window44
  • 空操作no-op既不增也不减 → 31neither increase nor decrease → 3131

Match → 0卖方销毁耗尽的 MatchSeller destroys an exhausted Match

Inputs 输入Inputs
Match cellCell{ lock: OPTICRUM{ args: … ‖ seller_lock_hash }, data: xudt_amount ‖ rate ‖ base }
卖方 cellseller cellCell{ lock: hash = seller_lock_hash }
Outputs 输出Outputs
无 Opticrum 输出no Opticrum output剩余资金任意去向remaining funds go anywhere
header_deps: [ tip 块(索引 0), Match cell 所在块 ]header_deps: [ tip block (index 0), Match cell block ]

限制Constraints

  • lock.args长度 65 → Order,133 → Match,其他 → 35length 65 → Order, 133 → Match, else → 3535
  • 输出 Opticrum celloutput Opticrum cell第一个 lock code_hash 相同、hash_type = type 的输出(不比较 args)the first output whose lock has the same code_hash and hash_type = type (args not compared)—
  • 路由routeMatch 且无 Opticrum 输出 → MatchDestroyMatch with no Opticrum output → MatchDestroy40
  • 授权authorizationseller_lock_hash 在输入中seller_lock_hash among inputs37
  • 耗尽exhaustionrate × (tip − base) ≥ 剩余价值(xUDT 模式比较 xudt_amount,否则比较未占用 capacity)rate × (tip − base) ≥ remaining value (xudt_amount in xUDT mode, else unoccupied capacity)32
  • header_deps缺失 → 29missing → 2929
部署与来源Deployments & source 未核验unverified
源码source
Opticrum/ckb-contract-script@803523a4 · contracts/opticrum/src/main.rs · contracts/opticrum/src/error.rs · contracts/opticrum/src/state.rs · contracts/opticrum/src/utils.rs · contracts/opticrum/src/verifiers · opticrum-protocol/src/lib.rs · deployment/testnet/opticrum.json (commit read when writing this spec (default-branch HEAD 2026-11-12). Framework ckb-cinnabar-verifier pinned at ashuralyk/ckb-cinnabar@f50d8e9 (system error codes 1–11). Testnet code_hash is the type_id from deployment/testnet/opticrum.json; the live code cell is v0.3.2 (0x6a3319f8…:0). Older versions under the same type_id used different args/data layouts.)
二进制核验binary check
未核验unverified hash appears in the repo only as a deployment record or doc, which does not prove the recorded commit builds this binary (reproducible build not attempted)
0xdf7c875f63…22d0cdf2: listed in deployment/testnet/opticrum.json
网络networkcode_hashcell_deptype_id args数据哈希data hash部署交易deploy tx
testnet0x1f8452818672fbb60f39c72e2d30dadd1814f322dfc21bf552784d83054f0247 type0x6a3319f835…bd82f5c4:0 code0x3b009c195d…fb7feb490xdf7c875f63…22d0cdf20x6a3319f835…bd82f5c4 #22145886
链上数据核对于 on-chain data checked 2026-11-12
错误码:框架 ckb-cinnabar-verifier 1–5 为系统错误、10 NotFoundRootVerifier、11 NotFoundBranchVerifier;合约 error.rs 自 20 起:20 BadOrderCancel、21 BadOrderMatch、22 ChannelCellNotInDep、23 ChannelCapacityMismatch、24 ChannelCreatedBeforeOrder、25 OrderDataNotSet、26 BadXudtAmount、27 BadExtractionAmount、28 MatchDataNotSet、29 HeaderNotSet、30 BadMatchDataUpdate、31 BadMatchUpdate、32 MatchNotExhausted、33 RentPerBlockMismatch、34 MatchNotViable(未使用)、35 BadArgsLength、36 BuyerAuthMissing、37 SellerAuthMissing、38 AuthorizationMissing、39 UnexpectedBranch、40 UnknownState、41 WithdrawWindowExpired、42 HesitationNotElapsed、43 PartialWithdrawNotAllowed、44 InjectDuringHesitation。Error codes: framework ckb-cinnabar-verifier 1–5 are system errors, 10 NotFoundRootVerifier, 11 NotFoundBranchVerifier; contract error.rs starts at 20: 20 BadOrderCancel, 21 BadOrderMatch, 22 ChannelCellNotInDep, 23 ChannelCapacityMismatch, 24 ChannelCreatedBeforeOrder, 25 OrderDataNotSet, 26 BadXudtAmount, 27 BadExtractionAmount, 28 MatchDataNotSet, 29 HeaderNotSet, 30 BadMatchDataUpdate, 31 BadMatchUpdate, 32 MatchNotExhausted, 33 RentPerBlockMismatch, 34 MatchNotViable (unused), 35 BadArgsLength, 36 BuyerAuthMissing, 37 SellerAuthMissing, 38 AuthorizationMissing, 39 UnexpectedBranch, 40 UnknownState, 41 WithdrawWindowExpired, 42 HesitationNotElapsed, 43 PartialWithdrawNotAllowed, 44 InjectDuringHesitation.
注释与代码不一致:opticrum-protocol 注释写 “MatchData — 40-byte”,代码 MATCH_DATA_LEN = 32、from_slice 只要求 ≥ 32B。错误 32 MatchNotExhausted 在提取路径表示“已耗尽”(语义相反)。Comment/code discrepancies: opticrum-protocol says “MatchData — 40-byte”, but the code uses MATCH_DATA_LEN = 32 and from_slice only requires ≥ 32B. Error 32 MatchNotExhausted means “already exhausted” on the extraction path (opposite meaning).
路由比较的 xUDT 是 (数量, type) 元组(parse_xudt 读 data[0..16]),因此 xUDT 模式下任何改变 xUDT 数量的 Match 更新都会落入 40 UnknownState;提取 / 取回 / 注资实际只对 CKB 模式可达。这是代码行为,非本页推断的规则。Routing compares xUDT as an (amount, type) tuple (parse_xudt reads data[0..16]), so in xUDT mode any Match update that changes the xUDT amount routes to 40 UnknownState; extraction / withdraw / inject are in practice only reachable in CKB mode. This is code behaviour, not a rule inferred by this page.
输出 Opticrum cell 按 “第一个 lock code_hash 相同且 hash_type = type 的输出” 查找,不比较 args:同一交易中有多个 Opticrum cell 时可能取到别的 cell。lock 不检查任何签名,授权仅靠 “对方 lock hash 出现在输入中”。The output Opticrum cell is “the first output whose lock has the same code_hash and hash_type = type”; args are not compared, so with several Opticrum cells in one tx another cell may be picked. The lock checks no signature; authorization is only “that party’s lock hash appears among the inputs”.
部署:testnet 通过 type_id 0x1f84…0247 多次迁移(v0.1.1 → v0.3.2);早期版本的 args(166B)/ data(40–41B)布局不同,本页只解析当前布局。仓库未公布主网部署。Deployment: testnet migrated several times under type_id 0x1f84…0247 (v0.1.1 → v0.3.2); earlier versions used different args (166B) / data (40–41B) layouts and this page only decodes the current one. No mainnet deployment is published.