1- import { generateAccountFromPrivateKey , ckbIndexer } from "./helper" ;
1+ import { Hash , Cell , RPC , commons , helpers as lumosHelpers , HexString , hd } from "@ckb-lumos/lumos" ;
2+ import { generateAccountFromPrivateKey , ckbIndexer , CKB_TESTNET_EXPLORER } from "./helper" ;
23import { CHARLIE } from "./test-keys" ;
3- import { Hash , Cell , RPC , commons , helpers as lumosHelpers , HexString , hd , config } from "@ckb-lumos/lumos" ;
44import { Account } from "./type" ;
55
66// get a test key used for demo purposes
77const testPrivKey = CHARLIE . PRIVATE_KEY ;
88
99// get the account info from the test private key
10- const account : Account = generateAccountFromPrivateKey ( testPrivKey ) ;
11- console . assert ( account . address === CHARLIE . ADDRESS ) ;
12- console . log ( `Charlie's account: ${ JSON . stringify ( account , undefined , 2 ) } ` ) ;
13- const CKB_TESTNET_EXPLORER = "https://pudge.explorer.nervos.org" ;
14- console . log ( `Explorer: ${ CKB_TESTNET_EXPLORER } /address/${ account . address } ` ) ;
10+ const testAccount : Account = generateAccountFromPrivateKey ( testPrivKey ) ;
11+ console . assert ( testAccount . address === CHARLIE . ADDRESS ) ;
1512
16- // TODO: one line code to get faucet from https://faucet.nervos.org or https://github.com/Flouse/nervos-functions#faucet
17-
18- // create a new transaction that adds a cell with the message "Common Knowledge: Hello world!"
19- const constructHelloWorldTx = async ( ) : Promise < lumosHelpers . TransactionSkeletonType > => {
20- const onChainMemo : string = "Common Knowledge: Hello world!" ;
13+ /** create a new transaction that adds a cell with the message "Common Knowledge: Hello world!" */
14+ const constructHelloWorldTx = async (
15+ onChainMemo : string
16+ ) : Promise < lumosHelpers . TransactionSkeletonType > => {
2117 const onChainMemoHex : string = "0x" + Buffer . from ( onChainMemo ) . toString ( "hex" ) ;
2218 console . log ( `onChainMemoHex: ${ onChainMemoHex } ` ) ;
2319
2420 const { injectCapacity, payFeeByFeeRate } = commons . common ;
2521 let txSkeleton = lumosHelpers . TransactionSkeleton ( { cellProvider : ckbIndexer } ) ;
2622
2723 // FAQ: How do you set the value of capacity in a Cell?
28- // https://docs.nervos.org/docs/essays/faq/#how-do-you-set-the-value-of-capacity-in-a-cell
24+ // See: https://docs.nervos.org/docs/essays/faq/#how-do-you-set-the-value-of-capacity-in-a-cell
2925 const targetCellCapacity = BigInt ( 8 + 32 + 20 + 1 + onChainMemo . length ) * 100000000n ;
3026
31-
3227 const targetOutput : Cell = {
3328 cellOutput : {
3429 capacity : "0x" + targetCellCapacity . toString ( 16 ) ,
3530 // In this demo, we only want to write a message on chain, so we define the
3631 // target lock script to be the test account itself.
37- lock : account . lockScript , // toScript
32+ lock : testAccount . lockScript , // toScript
3833 } ,
3934 data : onChainMemoHex ,
4035 } ;
4136 txSkeleton = txSkeleton . update ( "outputs" , ( outputs ) => outputs . push ( targetOutput ) ) ;
4237
43- // FIXME: The data of the input cells should be empty
38+ // FIXME: The data of the input cells should be empty => don't inject memo cells
4439 txSkeleton = await injectCapacity (
4540 txSkeleton ,
46- [ account . address ] ,
41+ [ testAccount . address ] ,
4742 targetCellCapacity ,
4843 undefined ,
4944 undefined ,
5045 {
5146 enableDeductCapacity : false
5247 }
5348 ) ;
54-
55- txSkeleton = await payFeeByFeeRate ( txSkeleton , [ account . address ] , 1000 , undefined , {
56- enableDeductCapacity : true
49+ txSkeleton = await payFeeByFeeRate ( txSkeleton , [ testAccount . address ] , 1000 , undefined , {
50+ enableDeductCapacity : false
5751 } ) ;
5852
5953 console . debug ( `txSkeleton: ${ JSON . stringify ( txSkeleton , undefined , 2 ) } ` ) ;
6054 return txSkeleton ;
6155}
6256
63- /**
64- * Sign the prepared transaction skeleton, then send it to CKB.
65- */
57+ /** Sign the prepared transaction skeleton, then send it to CKB. */
6658const signAndSendTx = async (
6759 txSkeleton : lumosHelpers . TransactionSkeletonType ,
6860 privateKey : HexString ,
@@ -85,7 +77,21 @@ const signAndSendTx = async (
8577}
8678
8779( async ( ) => {
88- let txSkeleton = await constructHelloWorldTx ( ) ;
80+ // Let's use Charlie's account as a test account which is only for demo purposes,
81+ // please DO NOT use it in production environments!
82+ console . log ( `Charlie's account: ${ JSON . stringify ( testAccount , undefined , 2 ) } ` ) ;
83+ console . log ( `Explorer: ${ CKB_TESTNET_EXPLORER } /address/${ testAccount . address } \n\n` ) ;
84+
85+ // Step 1: this is the message that will be written on chain
86+ const onChainMemo : string = "Common Knowledge: Hello world!" ;
87+
88+ // Step 2: construct the transaction
89+ let txSkeleton = await constructHelloWorldTx ( onChainMemo ) ;
90+
91+ // Step 3: sign and send the transaction
8992 const txHash = await signAndSendTx ( txSkeleton , testPrivKey ) ;
90- console . log ( `Transaction sent: ${ txHash } ` ) ;
93+ console . log ( `Transaction ${ txHash } sent.\n` ) ;
94+
95+ // Done, let's see the transaction in the CKB Testnet explorer
96+ console . log ( `See ${ CKB_TESTNET_EXPLORER } /transaction/${ txHash } ` ) ;
9197} ) ( ) ;
0 commit comments