Skip to content

Commit dfdc721

Browse files
committed
JS Demo v0.1 for TTHW
Writing "Common Knowledge: Hello world!" into a cell on CKB testnet using Lumos
1 parent 8ef4a43 commit dfdc721

9 files changed

Lines changed: 66 additions & 11 deletions

File tree

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,3 @@ https://github.com/Flouse/ckb-tthw/blob/229f9332ceb1bf365e7e1ea131322001cf1b862c
2424
- Preview and interact with `simple transfer` code online through [codesandbox](https://codesandbox.io).
2525
https://codesandbox.io/s/github/ckb-js/lumos/tree/develop/examples/secp256k1-transfer?file=/lib.ts
2626
- etc.
27-
28-
- [Lumos docs site](https://cryptape.github.io/lumos-doc/)
29-
- [Hello Lumos](https://cryptape.github.io/lumos-doc/docs/preparation/hellolumos)
30-
- DApps on CKB Workshop
31-
- https://cryptape.github.io/lumos-doc/docs/guides/integratenft
32-
- etc.

js/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Writing "Common Knowledge: Hello world!" into a cell on CKB testnet using Lumos
2+
3+
In this tutorial, you will learn how to write "`Common Knowledge: Hello World!`" into a cell on CKB testnet using [Lumos](https://github.com/ckb-js/lumos), a JavaScript/TypeScript library for Nervos CKB. You will also learn how to check the transaction on CKB explorer.
4+
5+
## Prerequisites
6+
Before we begin, it is better that you have some basic knowledge of [Nervos CKB](https://ckbacademy.vercel.app/courses/basic-theory).
7+
But if you don't, there's no need to worry, just follow this tutorial step by step.
8+
9+
10+
## Only 3 Steps
11+
12+
Although some of the complexity is wrapped up, intuitively writing "Common Knowledge: Hello World!" into a cell on CKB testnet is really just `three steps`:
13+
14+
https://github.com/Flouse/ckb-tthw/blob/229f9332ceb1bf365e7e1ea131322001cf1b862c/tthw-tmp-demo/index.ts#L85-L96
15+
16+
### Talk is cheap. Run the code.
17+
18+
```bash
19+
git clone https://github.com/Flouse/ckb-tthw.git
20+
cd ckb-tthw/js
21+
22+
# Install dependences such as @ckb-lumos, etc.
23+
npm install
24+
25+
# Let's run it.
26+
npm run start
27+
# Result
28+
# Transaction 0xad66eb1d076cfef73a98a8b76e6bc6c21b2c564011c30e0436de2f5f89579c84 sent.
29+
# See https://pudge.explorer.nervos.org/transaction/0x57ff3a724b41808d1bae9a7d611956145542cd70cd2e1c6c43dab34ab28b9ea7
30+
```
31+
<!-- TODO: add result image -->
32+
Would you like to change `onChainMemo` string and re-run it again?
33+
34+
## Show me the code
35+
Let's dive into two functions that take up most of the code space. The [code and comments](./index.ts) are quite self-explained.
36+
37+
### Function `constructHelloWorldTx`
38+
This function creates a new transaction that adds a cell with the proposed on-chain message.
39+
40+
1. Create a transaction skeleton that serves as a blueprint for the final transaction.
41+
Define the output cell, which includes the capacity and lock script, and add it to the transaction skeleton, which is a mutable data structure used to construct a CKB transaction incrementally.
42+
2. Modify the transaction skeleton to include the necessary capacity to cover the output cell by injecting enough input cells.
43+
3. Pay the transaction fee by `payFeeByFeeRate` function, again, provided by Lumos.
44+
45+
### Function `signAndSendTx`
46+
This function is self-explanatory:
47+
1. Sign the transaction skeleton using a test private key.
48+
2. Send the signed transaction to CKB testnet.
49+
50+
### Check the message on CKB explorer
51+
![Check the message on CKB explorer](https://user-images.githubusercontent.com/1297478/236415697-c3a49e0d-eb8f-473e-a0c1-a587c20e5a42.png)
52+
The cell data is the hex format of "Common Knowledge: Hello world!".
53+
54+
55+
## Conclusion
56+
In this tutorial, you learned how to write a message into a cell on CKB testnet using Lumos. You also learned how to check the transaction on CKB explorer. Lumos provides a set of helper functions that make it easy to interact with the CKB blockchain. With Lumos, you can easily create, sign, and send transactions to the CKB blockchain.
57+
58+
## References
59+
- [CKB basic theoretical knowledge](https://ckbacademy.vercel.app/courses/basic-theory)
60+
- [CKB basic practical operation](https://ckbacademy.vercel.app/courses/basic-operation)
File renamed without changes.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const constructHelloWorldTx = async (
3333
},
3434
data: onChainMemoHex,
3535
};
36+
// push the target output cell into the transaction's outputs array
3637
txSkeleton = txSkeleton.update("outputs", (outputs) => outputs.push(targetOutput));
3738

3839
// FIXME: The data of the input cells should be empty => don't inject memo cells
@@ -71,8 +72,8 @@ const signAndSendTx = async (
7172
// create a new RPC instance pointing to CKB testnet
7273
const rpc = new RPC("https://testnet.ckb.dev/rpc");
7374

74-
// send the transaction to CKB node, null and passthrough mean skipping outputs validation
75-
const txHash = await rpc.sendTransaction(signedTx)
75+
// send the transaction to CKB node
76+
const txHash = await rpc.sendTransaction(signedTx);
7677
return txHash;
7778
}
7879

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ckb-tthw",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "Time to `Hello World` on CKB",
55
"main": "index.js",
66
"scripts": {
File renamed without changes.

0 commit comments

Comments
 (0)