-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add UDT balance and transfer CLI commands #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RetricSu
merged 5 commits into
ckb-devrel:develop
from
humble-little-bear:feature/udt-cli-operations
Jul 8, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0cc645f
feat: add UDT balance and transfer CLI commands
humble-little-bear f646dba
refactor: reuse balance/transfer for UDT and add issue/destroy commands
humble-little-bear 25ec278
chore: add changeset and fix formatting for UDT CLI refactor
humble-little-bear 5ba6515
fix(udt): address PR review comments
humble-little-bear 288a8b0
fix(udt): address code review comments from review squad
humble-little-bear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@offckb/cli": minor | ||
| --- | ||
|
|
||
| Refactor UDT CLI support: reuse `balance` and `transfer` commands for CKB and UDT queries, and add `offckb udt issue` / `offckb udt destroy` subcommands. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,46 @@ | ||
| import { CKB } from '../sdk/ckb'; | ||
| import { CKB, UdtBalanceInfo } from '../sdk/ckb'; | ||
| import { validateNetworkOpt } from '../util/validator'; | ||
| import { NetworkOption, Network } from '../type/base'; | ||
| import { NetworkOption, Network, UdtKind } from '../type/base'; | ||
| import { logger } from '../util/logger'; | ||
|
|
||
| export interface BalanceOption extends NetworkOption {} | ||
| export interface BalanceOption extends NetworkOption { | ||
| udtKind?: UdtKind; | ||
| udtTypeArgs?: string; | ||
| udt?: boolean; | ||
| } | ||
|
|
||
| export async function balanceOf(address: string, opt: BalanceOption = { network: Network.devnet }) { | ||
| const network = opt.network; | ||
| validateNetworkOpt(network); | ||
|
|
||
| const ckb = new CKB({ network }); | ||
|
|
||
| const balanceInCKB = await ckb.balance(address); | ||
| logger.info(`Balance: ${balanceInCKB} CKB`); | ||
| const [balanceInCKB, udtBalances] = await Promise.all([ | ||
| ckb.balance(address), | ||
| opt.udt !== false ? ckb.detectUdtBalances(address) : Promise.resolve([]), | ||
| ]); | ||
| logger.info(`CKB: ${balanceInCKB}`); | ||
|
|
||
| const filtered = filterUdtBalances(udtBalances, opt); | ||
|
|
||
| if (filtered.length > 0) { | ||
| logger.info('UDT:'); | ||
| for (const udt of filtered) { | ||
| logger.info(` ${udt.kind} (args=${udt.args}): ${udt.balance}`); | ||
| } | ||
| } | ||
|
|
||
| process.exit(0); | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| function filterUdtBalances(balances: UdtBalanceInfo[], opt: BalanceOption): UdtBalanceInfo[] { | ||
| if (!opt.udtKind && !opt.udtTypeArgs) { | ||
| return balances; | ||
| } | ||
|
|
||
| return balances.filter((udt) => { | ||
| const kindMatch = opt.udtKind ? udt.kind === opt.udtKind : true; | ||
| const argsMatch = opt.udtTypeArgs ? udt.args === opt.udtTypeArgs : true; | ||
| return kindMatch && argsMatch; | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { CKB } from '../sdk/ckb'; | ||
| import { NetworkOption, Network, UdtKind } from '../type/base'; | ||
| import { logTxSuccess } from '../util/link'; | ||
| import { validateNetworkOpt, validateUdtKind, validateUdtTypeArgs } from '../util/validator'; | ||
|
|
||
| export interface UdtIssueOption extends NetworkOption { | ||
| udtKind: UdtKind; | ||
| typeArgs?: string; | ||
| to?: string; | ||
| privkey: string; | ||
| } | ||
|
|
||
| export interface UdtDestroyOption extends NetworkOption { | ||
| udtKind: UdtKind; | ||
| typeArgs: string; | ||
| privkey: string; | ||
| } | ||
|
|
||
| export async function udtIssue( | ||
| amount: string, | ||
| opt: UdtIssueOption = { network: Network.devnet, udtKind: 'sudt', privkey: '' }, | ||
| ) { | ||
| const network = opt.network; | ||
| validateNetworkOpt(network); | ||
| validateUdtKind(opt.udtKind); | ||
|
|
||
| if (!opt.privkey) { | ||
| throw new Error('--privkey is required!'); | ||
| } | ||
|
|
||
| const ckb = new CKB({ network }); | ||
| const txHash = await ckb.udtIssue({ | ||
| privateKey: opt.privkey, | ||
| kind: opt.udtKind, | ||
| amount, | ||
| typeArgs: opt.typeArgs ? validateUdtTypeArgs(opt.udtKind, opt.typeArgs) : undefined, | ||
| toAddress: opt.to, | ||
| }); | ||
|
|
||
| logTxSuccess(network, txHash, 'issued UDT'); | ||
| } | ||
|
|
||
| export async function udtDestroy( | ||
| amount: string, | ||
| opt: UdtDestroyOption = { network: Network.devnet, udtKind: 'sudt', typeArgs: '', privkey: '' }, | ||
| ) { | ||
| const network = opt.network; | ||
| validateNetworkOpt(network); | ||
| validateUdtKind(opt.udtKind); | ||
|
|
||
| if (!opt.privkey) { | ||
| throw new Error('--privkey is required!'); | ||
| } | ||
|
|
||
| const ckb = new CKB({ network }); | ||
| const txHash = await ckb.udtDestroy({ | ||
| privateKey: opt.privkey, | ||
| kind: opt.udtKind, | ||
| amount, | ||
| typeArgs: validateUdtTypeArgs(opt.udtKind, opt.typeArgs), | ||
| }); | ||
|
|
||
| logTxSuccess(network, txHash, 'destroyed UDT'); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛑 [CRITICAL / Security] Accepting the private key via
--privkeyexposes it to any process on the same host (ps,/proc/<pid>/cmdline). The same risk exists at line 192 forudt destroy. Please read the key from an environment variable (OFFCKB_PRIVATE_KEY) or a file (--privkey-file) instead.