Skip to content

Commit 229f933

Browse files
committed
Refactor the steps of tthw's main function
1 parent 9ce630c commit 229f933

3 files changed

Lines changed: 42 additions & 27 deletions

File tree

tthw-tmp-demo/helper.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { hd, config, helpers, HexString, BI, Indexer, Address } from '@ckb-lumos/lumos';
22
import { Account } from './type';
33

4-
const CKB_TESTNET_RPC = "https://testnet.ckb.dev/rpc";
4+
export const CKB_TESTNET_EXPLORER = "https://pudge.explorer.nervos.org";
5+
export const CKB_TESTNET_RPC = "https://testnet.ckb.dev/rpc";
56
export const ckbIndexer = new Indexer(CKB_TESTNET_RPC);
67

78
// This tutorial uses CKB testnet.
@@ -60,3 +61,11 @@ export async function getCapacities(address: string): Promise<BI> {
6061

6162
return capacities;
6263
}
64+
65+
/**
66+
* Get faucet from https://github.com/Flouse/nervos-functions#faucet
67+
*/
68+
export async function getFaucet() {
69+
// TODO: one line code to get faucet from https://faucet.nervos.org or https://github.com/Flouse/nervos-functions#faucet
70+
throw new Error("TODO: get faucet from https://faucet.nervos.org or https://github.com/Flouse/nervos-functions#faucet");
71+
}

tthw-tmp-demo/index.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,60 @@
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";
23
import { CHARLIE } from "./test-keys";
3-
import { Hash, Cell, RPC, commons, helpers as lumosHelpers, HexString, hd, config } from "@ckb-lumos/lumos";
44
import { Account } from "./type";
55

66
// get a test key used for demo purposes
77
const 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. */
6658
const 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
})();

tthw-tmp-demo/package.json

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.1",
3+
"version": "0.0.2",
44
"description": "Time to `Hello World` on CKB",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)