Skip to content

Commit

Permalink
feat: SSRI & UDT SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Alive24 authored and Hanssen0 committed Jan 8, 2025
1 parent dc847dc commit b9cee9f
Show file tree
Hide file tree
Showing 44 changed files with 1,657 additions and 162 deletions.
7 changes: 7 additions & 0 deletions .changeset/swift-stingrays-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@ckb-ccc/core": patch
"@ckb-ccc/ssri": patch
"@ckb-ccc/udt": patch
---

feat: SSRI & UDT SDK
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ For non-developers, you can [try CCC's app now here](https://app.ckbccc.com/) ([

- [Sign and verify any message.](<https://github.com/ckb-devrel/ccc/tree/master/packages/demo/src/app/connected/(tools)/Sign/page.tsx>) ([Playground](https://live.ckbccc.com/?src=https://raw.githubusercontent.com/ckb-devrel/ccc/refs/heads/master/packages/examples/src/sign.ts))
- [Transfer native CKB token.](<https://github.com/ckb-devrel/ccc/tree/master/packages/demo/src/app/connected/(tools)/Transfer/page.tsx>) ([Playground](https://live.ckbccc.com/?src=https://raw.githubusercontent.com/ckb-devrel/ccc/refs/heads/master/packages/examples/src/transfer.ts))
- [Transfer xUDT token.](<https://github.com/ckb-devrel/ccc/tree/master/packages/demo/src/app/connected/(tools)/TransferXUdt/page.tsx>) ([Playground](https://live.ckbccc.com/?src=https://raw.githubusercontent.com/ckb-devrel/ccc/refs/heads/master/packages/examples/src/transferUdt.ts))
- [Transfer UDT token.](<https://github.com/ckb-devrel/ccc/tree/master/packages/demo/src/app/connected/(tools)/TransferUdt/page.tsx>) ([Playground](https://live.ckbccc.com/?src=https://raw.githubusercontent.com/ckb-devrel/ccc/refs/heads/master/packages/examples/src/transferUdt.ts))
- See [Misc: Single-Use-Seals](https://talk.nervos.org/t/en-cn-misc-single-use-seals/8279) to learn how token issuing works in the cell model.
- [Issue xUDT token with the Single-Use Lock.](<https://github.com/ckb-devrel/ccc/tree/master/packages/demo/src/app/connected/(tools)/IssueXUdtSus/page.tsx>)
- [Issue xUDT token controlled by a Type ID cell.](<https://github.com/ckb-devrel/ccc/tree/master/packages/demo/src/app/connected/(tools)/IssueXUdtTypeId/page.tsx>)
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/ckb/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1487,8 +1487,13 @@ export class Transaction extends mol.Entity.Base<
return addedCount;
}

async completeInputsByUdt(from: Signer, type: ScriptLike): Promise<number> {
const exceptedBalance = this.getOutputsUdtBalance(type);
async completeInputsByUdt(
from: Signer,
type: ScriptLike,
balanceTweak?: NumLike,
): Promise<number> {
const exceptedBalance =
this.getOutputsUdtBalance(type) + numFrom(balanceTweak ?? 0);
const inputsBalance = await this.getInputsUdtBalance(from.client, type);
if (inputsBalance >= exceptedBalance) {
return 0;
Expand Down
1 change: 1 addition & 0 deletions packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"devDependencies": {
"@ckb-ccc/connector-react": "workspace:*",
"@ckb-ccc/lumos-patches": "workspace:*",
"@ckb-ccc/udt": "workspace:*",
"@ckb-lumos/ckb-indexer": "^0.24.0-next.1",
"@ckb-lumos/common-scripts": "^0.24.0-next.1",
"@ckb-lumos/config-manager": "^0.24.0-next.1",
Expand Down
130 changes: 130 additions & 0 deletions packages/demo/src/app/connected/(tools)/TransferUdt/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"use client";

import React, { useEffect, useState } from "react";
import { TextInput } from "@/src/components/Input";
import { Button } from "@/src/components/Button";
import { ccc } from "@ckb-ccc/connector-react";
import { Textarea } from "@/src/components/Textarea";
import { useGetExplorerLink } from "@/src/utils";
import { useApp } from "@/src/context";
import { ButtonsPanel } from "@/src/components/ButtonsPanel";
import { Udt } from "@ckb-ccc/udt";

export default function TransferUdt() {
const { client } = ccc.useCcc();
const { signer, createSender } = useApp();
const { log } = createSender("Transfer xUDT");

const { explorerTransaction } = useGetExplorerLink();

const [udtTxHash, setUdtTxHash] = useState<string>("");
const [udtIndex, setUdtIndex] = useState<string>("");
const [udtCodeHash, setUdtCodeHash] = useState<string>("");
const [udtHashType, setUdtHashType] = useState<string>("");
const [udtArgs, setUdtArgs] = useState<string>("");
const [transferTo, setTransferTo] = useState<string>("");
const [amount, setAmount] = useState<string>("");

useEffect(() => {
(async () => {
const script = await client.getKnownScript(ccc.KnownScript.XUdt);
setUdtCodeHash(script.codeHash);
setUdtHashType(script.hashType);
setUdtTxHash(script.cellDeps[0].cellDep.outPoint.txHash);
setUdtIndex(script.cellDeps[0].cellDep.outPoint.index.toString());
})();
}, [client]);

return (
<div className="flex w-full flex-col items-stretch">
<Textarea
label="Address"
placeholder="Addresses to transfer to, separated by lines"
state={[transferTo, setTransferTo]}
/>
<TextInput
label="Amount"
placeholder="Amount to transfer for each"
state={[amount, setAmount]}
/>
<TextInput
label="Args"
placeholder="UDT args to transfer"
state={[udtArgs, setUdtArgs]}
/>
<TextInput
label="Code Hash"
placeholder="UDT args to transfer"
state={[udtCodeHash, setUdtCodeHash]}
/>
<TextInput
label="Hash Type"
placeholder="UDT hash type to transfer"
state={[udtHashType, setUdtHashType]}
/>
<TextInput
label="Script Code Tx Hash"
placeholder="Tx hash of the script code"
state={[udtTxHash, setUdtTxHash]}
/>
<TextInput
label="Script Code index"
placeholder="Index of the script code"
state={[udtIndex, setUdtIndex]}
/>
<ButtonsPanel>
<Button
className="self-center"
onClick={async () => {
if (!signer) {
return;
}
const toAddresses = await Promise.all(
transferTo
.split("\n")
.map((addr) => ccc.Address.fromString(addr, signer.client)),
);
const { script: change } = await signer.getRecommendedAddressObj();

const udtType = ccc.Script.from({
codeHash: udtCodeHash,
hashType: udtHashType,
args: udtArgs,
});
const udt = new Udt(
{
txHash: udtTxHash,
index: udtIndex,
},
{
codeHash: udtCodeHash,
hashType: udtHashType,
args: udtArgs,
},
);

const tx = await udt.transfer(
toAddresses.map(({ script }) => ({
to: script,
amount: amount,
})),
);
const completedTx = await udt.completeUdtBy(tx, signer);
await completedTx.completeInputsByCapacity(signer);
await completedTx.completeFeeBy(signer);

console.log(completedTx);

// Sign and send the transaction
const txHash = await signer.sendTransaction(completedTx);
log("Transaction sent:", explorerTransaction(txHash));
await signer.client.waitTransaction(txHash);
log("Transaction committed:", explorerTransaction(txHash));
}}
>
Transfer
</Button>
</ButtonsPanel>
</div>
);
}
100 changes: 0 additions & 100 deletions packages/demo/src/app/connected/(tools)/TransferXUdt/page.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion packages/demo/src/app/connected/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const TABS: [ReactNode, string, keyof typeof icons, string][] = [
"Clock",
"text-amber-500",
],
["Transfer xUDT", "/connected/TransferXUdt", "BadgeCent", "text-emerald-500"],
["Transfer UDT", "/connected/TransferUdt", "BadgeCent", "text-emerald-500"],
["Issue xUDT (SUS)", "/connected/IssueXUdtSus", "Rss", "text-sky-500"],
[
<div className="flex flex-col">
Expand Down
21 changes: 21 additions & 0 deletions packages/ssri/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
node_modules/
misc/

*test.js
*test.ts
*test.d.ts
*test.d.ts.map
*spec.js
*spec.ts
*spec.d.ts
*spec.d.ts.map

tsconfig.json
tsconfig.*.json
eslint.config.mjs
.prettierrc
.prettierignore

tsconfig.tsbuildinfo
tsconfig.*.tsbuildinfo
.github/
13 changes: 13 additions & 0 deletions packages/ssri/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules/

dist/
dist.commonjs/

.npmignore
.prettierrc
tsconfig.json
eslint.config.mjs
.prettierrc

tsconfig.tsbuildinfo
.github/
5 changes: 5 additions & 0 deletions packages/ssri/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": false,
"trailingComma": "all",
"plugins": ["prettier-plugin-organize-imports"]
}
54 changes: 54 additions & 0 deletions packages/ssri/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<p align="center">
<a href="https://app.ckbccc.com/">
<img alt="Logo" src="https://raw.githubusercontent.com/ckb-devrel/ccc/master/assets/logoAndText.svg" style="height: 8rem; max-width: 90%; padding: 0.5rem 0;" />
</a>
</p>

<h1 align="center" style="font-size: 48px;">
CCC's Support for SSRI
</h1>

<p align="center">
<a href="https://www.npmjs.com/package/@ckb-ccc/ssri"><img
alt="NPM Version" src="https://img.shields.io/npm/v/%40ckb-ccc%2Fssri"
/></a>
<img alt="GitHub commit activity" src="https://img.shields.io/github/commit-activity/m/ckb-devrel/ccc" />
<img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/ckb-devrel/ccc/master" />
<img alt="GitHub branch check runs" src="https://img.shields.io/github/check-runs/ckb-devrel/ccc/master" />
<a href="https://live.ckbccc.com/"><img
alt="Playground" src="https://img.shields.io/website?url=https%3A%2F%2Flive.ckbccc.com%2F&label=Playground"
/></a>
<a href="https://app.ckbccc.com/"><img
alt="App" src="https://img.shields.io/website?url=https%3A%2F%2Fapp.ckbccc.com%2F&label=App"
/></a>
<a href="https://docs.ckbccc.com/"><img
alt="Docs" src="https://img.shields.io/website?url=https%3A%2F%2Fdocs.ckbccc.com%2F&label=Docs"
/></a>
</p>

<p align="center">
CCC - CKBers' Codebase is a one-stop solution for your CKB JS/TS ecosystem development.
<br />
Empower yourself with CCC to discover the unlimited potential of CKB.
<br />
Interoperate with wallets from different chain ecosystems.
<br />
Fully enabling CKB's Turing completeness and cryptographic freedom power.
</p>

### Script-Sourced Rich Information

Read more about SSRI on <a href="https://talk.nervos.org/t/en-cn-script-sourced-rich-information-script/8256">[EN/CN] Script-Sourced Rich Information - 来源于 Script 的富信息</a>.

NOTE: This is the base package for interaction with SSRI-Compliant scripts.

If you are looking for UDT support, please refer directly to <a href="https://www.npmjs.com/package/@ckb-ccc/udt">@ckb-ccc/udt</a> which supports both SSRI-compliant UDT and falling back to xUDT.

### Related Projects

- [`ssri-server`](https://github.com/ckb-devrel/ssri-server): Server for calling SSRI methods.
- [`ckb_ssri_sdk`](https://github.com/ckb-devrel/ckb_ssri_sdk): A toolkit to help developers build SSRI-Compliant scripts on CKB with production level example script `pausable-udt` for reference.

<h3 align="center">
Read more about CCC on <a href="https://docs.ckbccc.com">our website</a> or <a href="https://github.com/ckb-devrel/ccc">GitHub Repo</a>.
</h3>
Loading

0 comments on commit b9cee9f

Please sign in to comment.