Skip to content

Commit 85bc876

Browse files
authored
feat: merge V0.3.x into master (#236)
* feat(template): add js-script-next-js template (#231) * feat(template): add js-script-next-js template * fix add packages/* in pnpm-workspace * fix(template): name js script to hello-world * fix js-script tempalte package.json * fix(template): js-script isScriptDeployed * fix(template): script info of hello-world.bc * docs(template): update script descript in js-scrip template * feat(create): add create dapp only (#232) * feat(create): add create dapp only * ask for inject config after create dapp only project * fix: inject config with file path target * chore: update readme for create dapp-only * fix(readme): section link * update default CKB version and debugger minimal version (#234) * build(deps-dev): bump the npm_and_yarn group with 1 update (#235)
2 parents de05914 + 798573e commit 85bc876

46 files changed

Lines changed: 8359 additions & 43 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ There are BREAKING CHANGES between v0.2.x and v0.3.x, make sure to read the [mig
3030
- [Tweak Devnet Config](#tweak-devnet-config)
3131
- [Create a full-stack Project](#create-a-full-stack-project)
3232
- [Create a script-only Project](#create-a-script-only-project)
33+
- [Create a dApp-only Project](#create-a-dapp-only-project)
3334
- [Build and Deploy a script](#build-and-deploy-a-script)
3435
- [Start the frontend project](#start-the-frontend-project)
3536
- [Debug a transaction](#debug-a-transaction)
@@ -208,6 +209,14 @@ offckb create <your-project-name> --script
208209

209210
Note: you need to have rust/cargo/cargo-generate/clang 16+ installed in your environment to use this command. offckb doesn't do anything really, it just call [ckb-script-template](https://github.com/cryptape/ckb-script-templates) to do all the magic.
210211

212+
### Create a dApp-only Project
213+
214+
You can create a new dApp project without the script(CKB Smart Contract). This is useful when you don't need to create your own on-chain script logic.
215+
216+
```sh
217+
offckb create <your-project-name> --dapp # or -d
218+
```
219+
211220
### Build and Deploy a script
212221

213222
The fullstack boilerplate project is a monorepo, which contains a script project and a frontend project.

pnpm-workspace.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
onlyBuiltDependencies:
22
- cpu-features
33
- secp256k1
4+
5+
packages:
6+
- "packages/*"

src/cfg/setting.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ export interface Settings {
6868
cachePath: string;
6969
binFolder: string;
7070
};
71+
ckbDebugger: {
72+
minVersion: string;
73+
};
7174
};
7275
}
7376

@@ -78,7 +81,7 @@ export const defaultSettings: Settings = {
7881
},
7982
bins: {
8083
rootFolder: path.resolve(dataPath, 'bins'),
81-
defaultCKBVersion: '0.113.1',
84+
defaultCKBVersion: '0.201.0',
8285
downloadPath: path.resolve(cachePath, 'download'),
8386
},
8487
devnet: {
@@ -118,6 +121,9 @@ export const defaultSettings: Settings = {
118121
cachePath: path.resolve(cachePath, 'tools', 'moleculec-es'),
119122
binFolder: path.resolve(dataPath, 'tools', 'moleculec-es'),
120123
},
124+
ckbDebugger: {
125+
minVersion: '0.200.0',
126+
},
121127
},
122128
};
123129

src/cli.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { DeployOptions, deploy } from './cmd/deploy';
1111
import { syncScripts } from './cmd/sync-scripts';
1212
import { TransferOptions, transfer } from './cmd/transfer';
1313
import { BalanceOption, balanceOf } from './cmd/balance';
14-
import { create, selectBareTemplate, CreateOption, createScriptProject } from './cmd/create';
14+
import { create, selectBareTemplate, CreateOption, createScriptProject, createDappProject } from './cmd/create';
1515
import { printMyScripts, DeployedScriptOption } from './cmd/my-scripts';
1616
import { Config, ConfigItem } from './cmd/config';
1717
import { debugSingleScript, debugTransaction, parseSingleScriptOption } from './cmd/debug';
@@ -35,12 +35,17 @@ program
3535
.command('create [your-project-name]')
3636
.description('Create a new dApp from bare templates')
3737
.option('-s, --script', 'Only create the script project')
38+
.option('-d, --dapp', 'Only create the ccc dapp project')
3839
.action(async (projectName: string, option: CreateOption) => {
3940
const name = projectName ?? 'my-first-ckb-project';
4041
if (option.script) {
4142
return await createScriptProject(name);
4243
}
4344

45+
if (option.dapp) {
46+
return await createDappProject(name);
47+
}
48+
4449
const template = await selectBareTemplate();
4550
return create(name, template);
4651
});

src/cmd/create.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
import path from 'path';
22
import { findFileInFolder } from '../util/fs';
33
import { gitCloneAndDownloadFolderSync } from '../util/git';
4-
import { select } from '@inquirer/prompts';
4+
import { injectConfig } from './inject-config';
5+
import { select, confirm } from '@inquirer/prompts';
56
import { execSync } from 'child_process';
67
import { genMyScriptsJsonFile, genSystemScriptsJsonFile } from '../scripts/gen';
78
import { readSettings } from '../cfg/setting';
89
import { BareTemplateOption, loadBareTemplateOpts } from '../template/option';
910
import { OffCKBConfigFile } from '../template/offckb-config';
1011
const version = require('../../package.json').version;
1112

12-
export interface CreateOption {
13-
script: boolean;
14-
}
13+
export type ScriptOnly = {
14+
script: true;
15+
dapp?: false;
16+
};
17+
18+
export type DappOnly = {
19+
dapp: true;
20+
script?: false;
21+
};
22+
23+
export type CreateOption = ScriptOnly | DappOnly;
1524

1625
export function createScriptProject(name: string) {
1726
const cmd = `cargo generate gh:cryptape/ckb-script-templates workspace --name ${name}`;
@@ -22,6 +31,17 @@ export function createScriptProject(name: string) {
2231
}
2332
}
2433

34+
export async function createDappProject(name: string) {
35+
const cmd = `npx create-ccc-app@latest ${name} --ts --}`;
36+
try {
37+
execSync(cmd, { encoding: 'utf-8', stdio: 'inherit' });
38+
const dappFolderPath = path.resolve(process.cwd(), name);
39+
await askForInjectOffckbConfig(dappFolderPath);
40+
} catch (error: unknown) {
41+
console.error('create ccc-appp project failed, ', (error as Error).message);
42+
}
43+
}
44+
2545
export async function create(name: string, template: BareTemplateOption) {
2646
const targetPath = path.resolve(process.cwd(), name);
2747
const settings = readSettings();
@@ -69,3 +89,13 @@ export async function selectBareTemplate() {
6989

7090
return opts.find((opt) => opt.value === answer)!;
7191
}
92+
93+
export async function askForInjectOffckbConfig(dappFolderPath: string) {
94+
const answer = await confirm({
95+
message: 'Do you want to inject offckb configs in your project so that it can work with local blockchain info?',
96+
});
97+
if (answer) {
98+
const target = path.resolve(dappFolderPath, 'offckb.config.ts');
99+
injectConfig({ target });
100+
}
101+
}

src/cmd/debug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export function buildDebugFullTransactionFilePath(network: Network, txHash: stri
119119
}
120120

121121
export function debugRaw(options: string) {
122-
if (!CKBDebugger.isBinaryInstalled()) {
122+
if (!CKBDebugger.isBinaryInstalled() || !CKBDebugger.isBinaryVersionValid()) {
123123
CKBDebugger.installCKBDebugger();
124124
}
125125
return CKBDebugger.runRaw(options);

src/cmd/inject-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function injectConfig({ target }: InjectConfigProp) {
4040
const myScriptCodeHash = offCKB.myScripts['script-name'].codeHash;
4141
const omnilockScriptCodeHash = offCKB.systemScripts['omnilock'].codeHash;
4242
43-
Check example at https://github.com/nervosnetwork/docs.nervos.org/tree/develop/examples/simple-transfer
43+
Check more on how to integrate offckb config to develop your app at https://docs.nervos.org/docs/getting-started/quick-start#different-frontend-framework
44+
A Full example can also be found at https://github.com/nervosnetwork/docs.nervos.org/tree/develop/examples/simple-transfer
4445
`);
4546
}

src/template/option.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ export interface BareTemplateOption {
88

99
const templates: Array<BareTemplateOption> = [
1010
{
11-
name: 'Remix-Vite Bare Templates',
11+
name: 'JS Script with Next.js fullstack template',
12+
value: 'js-script-next-js',
13+
description: 'A full-stack template with Next-js and ckb-js-vm script',
14+
tag: ['next.js', 'tailwindcss', 'ckb-js-vm', 'typescript'],
15+
author: 'retric@cryptape.com',
16+
},
17+
{
18+
name: 'Rust Script with Remix-Vite fullstack template',
1219
value: 'remix-vite-template',
1320
description: 'A full-stack template with Remix-vite and ckb-script-templates',
1421
tag: ['remix', 'vite', 'tailwindcss', 'ckb-script-templates', 'typescript', 'rust'],
1522
author: 'retric@cryptape.com',
1623
},
1724
{
18-
name: 'Next.js Bare Templates',
25+
name: 'Rust Script with Next.js fullstack template',
1926
value: 'next-js-template',
2027
description: 'A full-stack template with Next.js framework and ckb-script-templates',
2128
tag: ['next.js', 'tailwindcss', 'ckb-script-templates', 'typescript', 'rust'],

src/tools/ckb-debugger.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { spawnSync, execSync } from 'child_process';
2+
import { readSettings } from '../cfg/setting';
23

34
export interface DebugOption {
45
fullTxJsonFilePath: string;
@@ -23,6 +24,21 @@ export class CKBDebugger {
2324
return result.status === 0;
2425
}
2526

27+
static isBinaryVersionValid() {
28+
const result = spawnSync('ckb-debugger', ['--version']);
29+
if (result.status !== 0) {
30+
console.error('ckb-debugger is not installed');
31+
return false;
32+
}
33+
const version = result.stdout.toString().split(' ')[1];
34+
const settings = readSettings();
35+
if (version < settings.tools.ckbDebugger.minVersion) {
36+
console.error(`ckb-debugger version ${version} is less than ${settings.tools.ckbDebugger.minVersion}`);
37+
return false;
38+
}
39+
return true;
40+
}
41+
2642
static installCKBDebugger() {
2743
const command = `cargo install --git https://github.com/nervosnetwork/ckb-standalone-debugger ckb-debugger`;
2844
try {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# offckb-template
2+
3+
This is a Minimal Template for CKB Full-Stack Dapps generated by [offckb](https://github.com/RetricSu/offckb).
4+
5+
Offckb does not do the magic. It just wraps the new CKB smart contract template and the CKB javascript Dapp framework into one mono-repo. Under the hook, it uses:
6+
7+
- [ckb-js-vm](https://github.com/nervosnetwork/ckb-js-vm) for smart contract development
8+
- [next-js](https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app) and [ccc](https://github.com/ckb-devrel/ccc) for frontend development
9+
10+
To start the project, install the dependecies:
11+
12+
```bash
13+
pnpm i
14+
```
15+
16+
## Script(Smart Contract)
17+
18+
The on-chain script project is powered by [ckb-js-vm](https://github.com/nervosnetwork/ckb-js-vm) bootstrapped with [`create-ckb-js-vm-app`](https://github.com/nervosnetwork/ckb-js-vm).
19+
20+
Build on-chain script:
21+
22+
```bash
23+
pnpm build
24+
```
25+
26+
Test on-chain script:
27+
28+
```bash
29+
pnpm test
30+
```
31+
32+
## dApp frontend development
33+
34+
first, enter the frontend workspace:
35+
36+
```sh
37+
cd frontend
38+
```
39+
40+
start the app:
41+
42+
```sh
43+
pnpm dev
44+
```
45+
46+
change the CKB blockchain network:
47+
48+
edit `.env` file:
49+
50+
```bash
51+
NEXT_PUBLIC_NETWORK=devnet # devnet, testnet or mainnet
52+
```
53+
54+
## Deploy to devnet/testnet with offckb
55+
56+
Once you build your smart contracts, you can deploy them to CKB blockchain with [ckb-cli](https://github.com/nervosnetwork/ckb-cli) or any other tools.
57+
58+
If you want to test them in devnet/testnet blockchain, then `offckb` might be the ideal selection.
59+
60+
`offckb` will look for the `offckb.config.ts` file to read config information. so you will need to enter the frontend workspace to do the instruction:
61+
62+
```sh
63+
cd frontend
64+
offckb deploy --network devnet
65+
```
66+
67+
If successfully deployed, you will see the deploy script info for your smart contract recorded in the path recorded in the `offckb.config.ts` file.
68+
69+
Every time you deploy a new version of your smart contracts, those script infos will be updated by `offckb` in the place recorded in `offckb.config.ts` and work out-of-box in your frontend.
70+
71+
You can also deploy smart contracts to the CKB Testnet like this:
72+
73+
```sh
74+
cd frontend
75+
offckb deploy --network testnet
76+
```
77+
78+
and start your frontend Dapp targeting Testnet:
79+
80+
edit `.env` file:
81+
82+
```bash
83+
NEXT_PUBLIC_NETWORK=testnet # devnet, testnet or mainnet
84+
```
85+
86+
```bash
87+
cd frontend
88+
pnpm dev
89+
```
90+
91+
Note that the `mainnet` network is not supported in `offckb` since `offckb` is focusing on building a friendly development environment for CKB. To gain better security, it is recommended to use production tools like [ckb-cli](https://github.com/nervosnetwork/ckb-cli) to deploy smart contracts and do transactions for the CKB mainnet.

0 commit comments

Comments
 (0)