Skip to content

Commit fbbeba4

Browse files
authored
Merge pull request #196 from ckb-devrel/v0.3.x
feat: merge v0.3.2 into master
2 parents d63fca8 + 2fceebc commit fbbeba4

4 files changed

Lines changed: 66 additions & 8 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"@types/http-proxy": "^1.17.15",
6868
"adm-zip": "^0.5.10",
6969
"child_process": "^1.0.2",
70-
"ckb-transaction-dumper": "^0.4.0",
70+
"ckb-transaction-dumper": "^0.4.2",
7171
"commander": "^12.0.0",
7272
"cpu-features": "^0.0.10",
7373
"http-proxy": "^1.18.1",

src/cmd/repl.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { Network, NetworkOption } from '../type/base';
66
import { buildCCCDevnetKnownScripts } from '../scripts/private';
77
import accounts from '../../account/account.json';
88
import { validateNetworkOpt } from '../util/validator';
9+
import { genSystemScripts } from '../scripts/gen';
10+
import { readUserDeployedScriptsInfo } from '../scripts/util';
911

1012
export interface ReplProp extends NetworkOption {
1113
proxyRpc?: boolean;
@@ -28,15 +30,28 @@ export function repl({ network = Network.devnet, proxyRpc = false }: ReplProp) {
2830
context.ccc = ccc;
2931
context.cccA = cccA;
3032
context.networks = networks;
31-
context.Client = initGlobalClientBuilder();
32-
context.client = initGlobalClientBuilder().new(network);
33+
context.Client = initGlobalClientBuilder(proxyRpc);
34+
context.client = initGlobalClientBuilder(proxyRpc).new(network);
3335
context.accounts = accounts;
36+
context.myScripts = buildMyScripts().new(network);
37+
context.systemScripts = buildSystemScripts().new(network);
38+
3439
context.help = printHelpText;
3540
}
3641

37-
export function initGlobalClientBuilder() {
42+
export function initGlobalClientBuilder(proxyRpc: boolean) {
3843
return {
3944
new: (network: Network) => {
45+
if (proxyRpc) {
46+
return network === 'mainnet'
47+
? new ccc.ClientPublicMainnet({ url: networks.mainnet.proxy_rpc_url })
48+
: network === 'testnet'
49+
? new ccc.ClientPublicTestnet({ url: networks.testnet.proxy_rpc_url })
50+
: new ccc.ClientPublicTestnet({
51+
url: networks.devnet.proxy_rpc_url,
52+
scripts: buildCCCDevnetKnownScripts(),
53+
});
54+
}
4055
return network === 'mainnet'
4156
? new ccc.ClientPublicMainnet()
4257
: network === 'testnet'
@@ -72,6 +87,29 @@ Global Variables to use:
7287
const myClient = Client.fromUrl('<your rpc url>', 'devnet' | 'testnet' | 'mainnet');
7388
- accounts, test accounts array from OffCKB
7489
- networks, network information configs
90+
- myScripts, user-deployed scripts information via offckb deploy
91+
- systemScripts, built-in scripts information in the blockchain
7592
- help, print this help message
7693
`);
7794
}
95+
96+
export function buildSystemScripts() {
97+
return {
98+
new: (network: Network) => {
99+
const systemScripts = genSystemScripts();
100+
return network === Network.devnet
101+
? systemScripts?.devnet
102+
: network === Network.testnet
103+
? systemScripts?.testnet
104+
: systemScripts?.mainnet;
105+
},
106+
};
107+
}
108+
109+
export function buildMyScripts() {
110+
return {
111+
new: (network: Network) => {
112+
return readUserDeployedScriptsInfo(network);
113+
},
114+
};
115+
}

src/tools/rpc-proxy.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function createRPCProxy(network: Network, targetRpcUrl: string, port: num
3737
}
3838
const txFile = path.resolve(settings.devnet.transactionsPath, `${txHash}.json`);
3939
fs.writeFileSync(txFile, JSON.stringify(tx, null, 2));
40+
console.debug(`RPC Req: store tx ${txHash}`);
4041
}
4142
}
4243
} catch (err) {
@@ -45,6 +46,25 @@ export function createRPCProxy(network: Network, targetRpcUrl: string, port: num
4546
});
4647
});
4748

49+
proxy.on('proxyRes', function (proxyRes, _req, _res) {
50+
const body: Buffer[] = [];
51+
proxyRes.on('data', function (chunk) {
52+
body.push(chunk);
53+
});
54+
proxyRes.on('end', function () {
55+
const res = Buffer.concat(body).toString();
56+
try {
57+
const jsonRpcResponse = JSON.parse(res);
58+
const error = jsonRpcResponse.error;
59+
if (error) {
60+
console.debug('RPC Response: ', jsonRpcResponse);
61+
}
62+
} catch (err) {
63+
console.error('Error parsing JSON-RPC content:', err);
64+
}
65+
});
66+
});
67+
4868
const server = http.createServer((req, res) => {
4969
proxy.web(req, res, {}, (err) => {
5070
if (err) {

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -832,10 +832,10 @@ cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3:
832832
inherits "^2.0.1"
833833
safe-buffer "^5.0.1"
834834

835-
ckb-transaction-dumper@^0.4.0:
836-
version "0.4.0"
837-
resolved "https://registry.yarnpkg.com/ckb-transaction-dumper/-/ckb-transaction-dumper-0.4.0.tgz#21c248b58883779219f43d3e5002caa319c97129"
838-
integrity sha512-/Ue1s41BRgrHAYw1LEVcVetyEOLzSGx2Qrn2zPh8TuI5PZ3FO/TtDMEnDJQNu/wEE+vCYEsr0rDAFNY2NiFgLw==
835+
ckb-transaction-dumper@^0.4.2:
836+
version "0.4.2"
837+
resolved "https://registry.yarnpkg.com/ckb-transaction-dumper/-/ckb-transaction-dumper-0.4.2.tgz#bceadef621a8ea793b4f698d606cbf4f5c874ec8"
838+
integrity sha512-0frB1FYY3dlKLlef6ps8dfuwTXitdm4myYTc/+hAP3RCo/OrC1/MYXEedUCLXgqcXBnlOQ8VbK5PseNZovWPHQ==
839839

840840
cli-cursor@^4.0.0:
841841
version "4.0.0"

0 commit comments

Comments
 (0)