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 9, 2025
1 parent b6a73fa commit 8c97c85
Show file tree
Hide file tree
Showing 73 changed files with 2,123 additions and 210 deletions.
7 changes: 7 additions & 0 deletions .changeset/large-points-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@ckb-ccc/spore": minor
"@ckb-ccc/shell": minor
"@ckb-ccc/ccc": minor
---

feat: export backend packages in @ckb-ccc/shell
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": minor
"@ckb-ccc/ssri": minor
"@ckb-ccc/udt": minor
---

feat: SSRI & UDT SDK
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -131,7 +131,7 @@ Follow the prompts to select your preferred framework template and start buildin

We design CCC for both front-end and back-end developers. You need only one package to fulfil all your needs:

- [NodeJS](https://www.npmjs.com/package/@ckb-ccc/core): `npm install @ckb-ccc/core`
- [NodeJS](https://www.npmjs.com/package/@ckb-ccc/shell): `npm install @ckb-ccc/shell`
- [Custom UI](https://www.npmjs.com/package/@ckb-ccc/ccc): `npm install @ckb-ccc/ccc`
- [Web Component](https://www.npmjs.com/package/@ckb-ccc/connector): `npm install @ckb-ccc/connector`
- [React](https://www.npmjs.com/package/@ckb-ccc/connector-react) ([Docs](https://docs.ckbccc.com/modules/_ckb_ccc_connector_react.html)): `npm install @ckb-ccc/connector-react`
Expand Down
2 changes: 1 addition & 1 deletion packages/ccc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"access": "public"
},
"dependencies": {
"@ckb-ccc/core": "workspace:*",
"@ckb-ccc/shell": "workspace:*",
"@ckb-ccc/eip6963": "workspace:*",
"@ckb-ccc/joy-id": "workspace:*",
"@ckb-ccc/nip07": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/ccc/src/advancedBarrel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from "@ckb-ccc/core/advancedBarrel";
export * from "@ckb-ccc/eip6963/advanced";
export * from "@ckb-ccc/nip07/advanced";
export * from "@ckb-ccc/okx/advanced";
export * from "@ckb-ccc/shell/advancedBarrel";
export * from "@ckb-ccc/uni-sat/advanced";
export * from "@ckb-ccc/utxo-global/advanced";
export * from "@ckb-ccc/xverse/advanced";
2 changes: 1 addition & 1 deletion packages/ccc/src/barrel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export * from "@ckb-ccc/core/barrel";
export * from "@ckb-ccc/eip6963";
export * from "@ckb-ccc/joy-id";
export * from "@ckb-ccc/nip07";
export * from "@ckb-ccc/okx";
export * from "@ckb-ccc/rei";
export * from "@ckb-ccc/shell/barrel";
export * from "@ckb-ccc/uni-sat";
export * from "@ckb-ccc/utxo-global";
export * from "@ckb-ccc/xverse";
Expand Down
2 changes: 1 addition & 1 deletion packages/ccc/src/signersController.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ccc } from "@ckb-ccc/core";
import { Eip6963 } from "@ckb-ccc/eip6963";
import { JoyId } from "@ckb-ccc/joy-id";
import { Nip07 } from "@ckb-ccc/nip07";
import { Okx } from "@ckb-ccc/okx";
import { Rei } from "@ckb-ccc/rei";
import { ccc } from "@ckb-ccc/shell";
import { UniSat } from "@ckb-ccc/uni-sat";
import { UtxoGlobal } from "@ckb-ccc/utxo-global";
import { Xverse } from "@ckb-ccc/xverse";
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
5 changes: 4 additions & 1 deletion packages/core/src/jsonRpc/requestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ export class RequestorJsonRpc {
outTransformer,
);
} catch (err: unknown) {
await this.onError?.(err);
if (!this.onError) {
throw err;
}
await this.onError(err);
}
}

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
121 changes: 121 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,121 @@
"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";

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 udt = new ccc.udt.Udt(
{
txHash: udtTxHash,
index: udtIndex,
},
{
codeHash: udtCodeHash,
hashType: udtHashType,
args: udtArgs,
},
);

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

// 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
Loading

0 comments on commit 8c97c85

Please sign in to comment.