|
| 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 | + |
| 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) |
0 commit comments