Skip to content

Commit

Permalink
chore: support dev chain development
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Dec 28, 2023
1 parent a03ed8c commit bfb37e7
Show file tree
Hide file tree
Showing 21 changed files with 305 additions and 54 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
.env

# CKB
/ckb-miner.toml
/ckb.toml
/data
/default.db-options
/specs
/migrations/dev

# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
Expand Down
45 changes: 45 additions & 0 deletions bin/generate-blocks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

CKB_RPC_URL="${CKB_RPC_URL:-http://127.0.0.1:8114}"

function generate_one() {
curl --request POST \
--url "$CKB_RPC_URL" \
--data '{
"id": 42,
"jsonrpc": "2.0",
"method": "generate_block",
"params": []
}'
echo
}

function generate_n() {
if [ $# -eq 0 ]; then
generate_one
else
for i in $(seq "$@"); do
generate_one
done
fi
}

case "${1:-}" in
--url)
CKB_RPC_URL="$2"
shift
shift
generate_n "$@"
;;
--url=*)
CKB_RPC_URL="${1#*=}"
shift
generate_n "$@"
;;
--help)
echo 'usage: generate-blocks.sh [--help|--url CKB_RPC_URL] [count]'
;;
*)
generate_n "$@"
;;
esac
35 changes: 35 additions & 0 deletions bin/generate-epochs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

CKB_RPC_URL="${CKB_RPC_URL:-http://127.0.0.1:8114}"

function generate() {
local n="$(printf '%x' "${1:-1}")"
curl --request POST \
--url http://127.0.0.1:8114/ \
--data '{
"id": 42,
"jsonrpc": "2.0",
"method": "generate_epochs",
"params": ["0x'"$n"'"]
}'
}

case "${1:-}" in
--url)
CKB_RPC_URL="$2"
shift
shift
generate "$@"
;;
--url=*)
CKB_RPC_URL="${1#*=}"
shift
generate "$@"
;;
--help)
echo 'usage: generate-epochs.sh [--help|--url CKB_RPC_URL] [count]'
;;
*)
generate "$@"
;;
esac
23 changes: 23 additions & 0 deletions bin/use-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

case "${1:-}" in
--testnet)
shift
cat env.example
;;
--mainnet)
shift
echo 'NEXT_PUBLIC_CKB_CHAIN="LINA"'
echo 'NEXT_PUBLIC_CKB_RPC_URL=" https://mainnet.ckb.dev/"'
;;
*)
echo 'NEXT_PUBLIC_CKB_CHAIN="DEV"'
echo 'NEXT_PUBLIC_CKB_RPC_URL="http://127.0.0.1:8114/"'
;;
esac

sed -n \
-e 's/,$//' \
-e 's/^ *"type_id": /NEXT_PUBLIC_JOYID_COBUILD_POC_CODE_HASH=/p' \
-e 's/^ *"tx_hash": /NEXT_PUBLIC_JOYID_COBUILD_POC_TX_HASH=/p' \
"$@"
2 changes: 1 addition & 1 deletion deployment.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ location = { file = "build/release/joyid-cobuild-poc" }
# For example the secp256k1 lock
[lock]
code_hash = "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"
args = "0xe463d7c4cb28457b3a2f735d1d92a971b0f5a751"
args = "0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7"
hash_type = "type"
119 changes: 119 additions & 0 deletions docs/dev.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Development Docs

## Start Local Dev Chain

Init a dev chain using the test account that has 20 billions of CKB tokens in the genesis block.

```bash
ckb init -c dev --ba-arg 0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7
```

Edit `specs/dev.toml` to append `Indexer` and `IntegrationTest` to the `modules` option under the `[rpc]` section.

```toml
modules = ["Net", "Pool", "Miner", "Chain", "Stats", "Subscription", "Experiment", "Debug", "Indexer", "IntegrationTest"]
```

Import the test account into `ckb-cli` with empty password

```bash
echo 'd00c06bfd800d27397002dca6fb0993d5ba6399b4238b2f29ee9deb97593d2bc' > specs/miner.key
ckb-cli account import --privkey-path specs/miner.key </dev/null
```

Start the chain.

```bash
ckb run
```

Mine some blocks to make CKB tokens available to deploy contracts

```bash
bin/generate-blocks.sh 20
```

## Deploy Contracts to the Local Dev Chain

Build contracts

```bash
capsule build --release
```

Deploy using ckb-cli

Step 1: Call `gen-txs`. Notice the from-address is the test miner account.

```bash
rm -rf migrations/dev && mkdir -p migrations/dev
ckb-cli deploy gen-txs --from-address ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqwgx292hnvmn68xf779vmzrshpmm6epn4c0cgwga \
--fee-rate 1000 --deployment-config deployment.toml --info-file migrations/dev/deployment.json --migration-dir migrations/dev
```

Step 2: Sign txs

```bash
ckb-cli deploy sign-txs --info-file migrations/dev/deployment.json --privkey-path specs/miner.key --output-format json
# => {
# => "cell_tx_signatures": {
# => "0xc8328...": "0x92d7..."
# => }
# => }
```

Add the signatures to the info file `migrations/dev/deployment.json` manually. Attention that wrap the value in an array.

```json
"cell_tx_signatures": {
"0xc8328...": ["0x92d7..."]
},
```

Step 3: Send txs

```bash
ckb-cli deploy apply-txs --info-file migrations/dev/deployment.json --migration-dir migrations/dev
```

Step 4: Mine 3 blocks to commit txs

```bash
bin/generate-blocks.sh 3
```

## Configure The Web App

Generate .env file form the deployment.

```bash
bin/use-env.sh migrations/dev/20*.json > .env
```

Start the local development server.

```bash
pnpm dev
```

Connect JoyID and copy the address displayed at the top of the page.

Transfer some CKB tokens from the miner account to the copied address. Replace `ckbt1qz...` with the real address in the following command.

```bash
ckb-cli wallet transfer --skip-check-to-address --to-address ckt1qz... --capacity 300000 --privkey-path specs/miner.key
```

Mine some blocks to commit the transfer transaction.

```bash
bin/generate-blocks.sh 3
```

Now it's OK to run the web app with the local dev chain. You can run a miner process to generate blocks automatically in another terminal, or just run `bin/generate-blocks.sh 3` to manually commit transactions in the memory pool.

To test the phase 2 withdraw, use the command `bin/generate-epochs.sh 180` to generate 180 epochs, which is a full DAO cycle.

```bash
bin/generate_epochs.sh 180
```
4 changes: 2 additions & 2 deletions src/actions/deposit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import { parseUnit } from "@ckb-lumos/bi";
import { depositDao } from "@/lib/cobuild/publishers";
import { configFromEnv } from "@/lib/config";
import { useConfig } from "@/lib/config";

export default async function deposit(_prevState, formData, config) {
config = config ?? configFromEnv(process.env);
config = config ?? useConfig();

const from = formData.get("from");
const amount = parseUnit(formData.get("amount"), "ckb");
Expand Down
4 changes: 2 additions & 2 deletions src/actions/fetch-assets.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use server";

import { cache } from "react";
import { configFromEnv } from "@/lib/config";
import { useConfig } from "@/lib/config";
import { fetchAssets as fetchAssetsWithConfig } from "@/lib/cobuild/assets-manager";

export async function fetchAssetsWithoutCache(address, config) {
config = config ?? configFromEnv(process.env);
config = config ?? useConfig();
return await fetchAssetsWithConfig(address, config);
}

Expand Down
4 changes: 2 additions & 2 deletions src/actions/get-cell.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use server";

import { cache } from "react";
import { configFromEnv } from "@/lib/config";
import { useConfig } from "@/lib/config";
import { RPC } from "@ckb-lumos/rpc";

export async function getCellWithoutCache(outPoint, config) {
const { ckbRpcUrl } = config ?? configFromEnv(process.env);
const { ckbRpcUrl } = config ?? useConfig();

const rpc = new RPC(ckbRpcUrl);

Expand Down
4 changes: 2 additions & 2 deletions src/actions/get-header-by-number.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use server";

import { cache } from "react";
import { configFromEnv } from "@/lib/config";
import { useConfig } from "@/lib/config";
import { RPC } from "@ckb-lumos/rpc";

export async function getHeaderByNumberWithoutCache(blockNumber, config) {
const { ckbRpcUrl } = config ?? configFromEnv(process.env);
const { ckbRpcUrl } = config ?? useConfig();
const rpc = new RPC(ckbRpcUrl);

return await rpc.getHeaderByNumber(blockNumber);
Expand Down
4 changes: 2 additions & 2 deletions src/actions/get-header.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use server";

import { cache } from "react";
import { configFromEnv } from "@/lib/config";
import { useConfig } from "@/lib/config";
import { RPC } from "@ckb-lumos/rpc";

export async function getHeaderWithoutCache(blockHash, config) {
const { ckbRpcUrl } = config ?? configFromEnv(process.env);
const { ckbRpcUrl } = config ?? useConfig();
const rpc = new RPC(ckbRpcUrl);

return await rpc.getHeader(blockHash);
Expand Down
4 changes: 2 additions & 2 deletions src/actions/get-tip-header.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use server";

import { cache } from "react";
import { configFromEnv } from "@/lib/config";
import { useConfig } from "@/lib/config";
import { RPC } from "@ckb-lumos/rpc";

export async function getTipHeaderWithoutCache(config) {
const { ckbRpcUrl } = config ?? configFromEnv(process.env);
const { ckbRpcUrl } = config ?? useConfig();
const rpc = new RPC(ckbRpcUrl);

return await rpc.getTipHeader();
Expand Down
4 changes: 2 additions & 2 deletions src/actions/send-tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { revalidatePath } from "next/cache";
import { RPC } from "@ckb-lumos/rpc";
import { blockchain, utils as lumosBaseUtils } from "@ckb-lumos/base";

import { configFromEnv } from "@/lib/config";
import { useConfig } from "@/lib/config";

const { ckbHash } = lumosBaseUtils;

Expand Down Expand Up @@ -37,7 +37,7 @@ async function sendTxInner(tx, txHash, { ckbRpcUrl }) {
}

export default async function sendTx(tx, config) {
config = config ?? configFromEnv(process.env);
config = config ?? useConfig();
const txHash = ckbHash(blockchain.RawTransaction.pack(tx));

try {
Expand Down
4 changes: 2 additions & 2 deletions src/actions/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import { parseUnit } from "@ckb-lumos/bi";
import { transferCkb } from "@/lib/cobuild/publishers";
import { configFromEnv } from "@/lib/config";
import { useConfig } from "@/lib/config";

export default async function transfer(_prevState, formData, config) {
config = config ?? configFromEnv(process.env);
config = config ?? useConfig();

const from = formData.get("from");
const to = formData.get("to");
Expand Down
4 changes: 2 additions & 2 deletions src/actions/withdraw.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use server";

import { withdrawDao } from "@/lib/cobuild/publishers";
import { configFromEnv } from "@/lib/config";
import { useConfig } from "@/lib/config";

export default async function withdraw(from, cell, config) {
config = config ?? configFromEnv(process.env);
config = config ?? useConfig();

try {
const buildingPacket = await withdrawDao(config)({ from, cell });
Expand Down
4 changes: 2 additions & 2 deletions src/app/accounts/[address]/deposit/page.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { configFromEnv } from "@/lib/config";
import { useConfig } from "@/lib/config";

import DepositForm from "./form";

export default function Deposit({ params: { address }, config }) {
config = config ?? configFromEnv(process.env);
config = config ?? useConfig();

return (
<main>
Expand Down
4 changes: 2 additions & 2 deletions src/app/accounts/[address]/transfer/page.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { configFromEnv } from "@/lib/config";
import { useConfig } from "@/lib/config";

import TransferForm from "./form";

export default function Transfer({ params: { address }, config }) {
config = config ?? configFromEnv(process.env);
config = config ?? useConfig();

return (
<main>
Expand Down
Loading

0 comments on commit bfb37e7

Please sign in to comment.