Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc): improvement RPC for ckb2023 #556

Merged
merged 9 commits into from
Sep 26, 2023
8 changes: 6 additions & 2 deletions examples/secp256k1-transfer/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { Indexer, helpers, Address, Script, RPC, hd, config, Cell, commons, Witn
import { values, blockchain } from "@ckb-lumos/base";
const { ScriptValue } = values;

export const { AGGRON4 } = config.predefined;

// export let { AGGRON4 } = config.predefined;
let AGGRON4: config.Config = config.predefined.AGGRON4;
// it can be changed to the devnet http://localhost:8114
const CKB_RPC_URL = "https://testnet.ckb.dev/rpc";
const rpc = new RPC(CKB_RPC_URL);
const indexer = new Indexer(CKB_RPC_URL);
Expand Down Expand Up @@ -52,6 +53,9 @@ interface Options {
}

export async function transfer(options: Options): Promise<string> {
const block = await rpc.getBlockByNumber("0x0");
AGGRON4 = { PREFIX: "ckt", SCRIPTS: config.generateGenesisScriptConfigs(block) };

let txSkeleton = helpers.TransactionSkeleton({});
const fromScript = helpers.parseAddress(options.from, { config: AGGRON4 });
const toScript = helpers.parseAddress(options.to, { config: AGGRON4 });
Expand Down
5 changes: 4 additions & 1 deletion packages/base/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ export interface Transaction {
witnesses: HexString[];
}

type Status = "pending" | "proposed" | "committed" | "unknown" | "rejected";

export interface TxStatus {
status: Status;
blockHash?: Hash;
status: string;
reason?: string;
}

export interface TransactionWithStatus<Tx = Transaction> {
Expand Down
208 changes: 208 additions & 0 deletions packages/rpc/__tests__/formatters/result.fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,214 @@
"transactions": [],
"uncles": []
}
},
{
"result": "0x0e02000018000000e8000000ec000000e6010000ea0100000000000000000120691e8ccb8a01000004000000000000000000000400e8030000fa775972159aba682a5bf6e1ad6aa2a989c1ad1af61f130da4a2639a6dbfd782b6ce096ba31efabe2c387cbad8b3a455f8f6d2ade3204bd215291c2df9c88500000000000000000000000000000000000000000000000000000000000000006fd0305920429076102ae14371a11a338c9f6d7b35d022bc3a4cea771d50190f14896308581fa12ed1f84c9bf2862300cbab61de3e0000000099f54b01fbfe06ed97c68a255c08adc3120210350aad1004000000fa00000008000000f20000000c0000006c000000600000001c000000200000002400000028000000580000005c0000000000000000000000000000000100000004000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffff040000000400000086000000080000007a0000007a0000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000c8328aabcd9b9e8e64fbc566c4385c3bdeb219d7210000000000000020302e3131312e3020286161613531353820323032332d30392d3134290000000020000000880b0d848373b0b419e66835223d3bd423c6f0f40a7a3864e10912a7230cc417",
"expected": "0x0e02000018000000e8000000ec000000e6010000ea0100000000000000000120691e8ccb8a01000004000000000000000000000400e8030000fa775972159aba682a5bf6e1ad6aa2a989c1ad1af61f130da4a2639a6dbfd782b6ce096ba31efabe2c387cbad8b3a455f8f6d2ade3204bd215291c2df9c88500000000000000000000000000000000000000000000000000000000000000006fd0305920429076102ae14371a11a338c9f6d7b35d022bc3a4cea771d50190f14896308581fa12ed1f84c9bf2862300cbab61de3e0000000099f54b01fbfe06ed97c68a255c08adc3120210350aad1004000000fa00000008000000f20000000c0000006c000000600000001c000000200000002400000028000000580000005c0000000000000000000000000000000100000004000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffff040000000400000086000000080000007a0000007a0000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000c8328aabcd9b9e8e64fbc566c4385c3bdeb219d7210000000000000020302e3131312e3020286161613531353820323032332d30392d3134290000000020000000880b0d848373b0b419e66835223d3bd423c6f0f40a7a3864e10912a7230cc417"
},
{
"result": {
"block": {
"extension": "0x7dc094baf449662901d17c12a6b03af16181537d83aff41689d65ebd90131d7b",
"header": {
"compact_target": "0x20010000",
"dao": "0x5c490f74f540a12eb334c0d0f8862300cac44a092c07000000755322c8fbfe06",
"epoch": "0x3e80096000000",
"extra_hash": "0xf6e4abc7a5f61ec64060e2956b972e3d8f450102fa677cb23ee49373c55f14d1",
"hash": "0xbfba299e8942c89f96357b811a795343c0db0f42dd913e22c4fbd74843ef4c5a",
"nonce": "0xa238c12f207efedc2c388a0f2f78fcb",
"number": "0x96",
"parent_hash": "0x03bb5df51f01aafa4aa3010f86909aaa7a90dc0681c264bb3712e5c02ac1935e",
"proposals_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x18ad0b37f4e",
"transactions_root": "0x4dbf22caeebbc7cd35e631689b94e6abb4bd10bf2c175b40d187f997b7acd02f",
"version": "0x0"
},
"proposals": [],
"transactions": [
{
"cell_deps": [],
"hash": "0x4efec35228be483cdf3d58d73829b129e7b9dc6f85fc715e9267f5adeb2326b9",
"header_deps": [],
"inputs": [
{
"previous_output": {
"index": "0xffffffff",
"tx_hash": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"since": "0x96"
}
],
"outputs": [
{
"capacity": "0x2ecbd6b44c",
"lock": {
"args": "0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7",
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hash_type": "type"
},
"type": null
}
],
"outputs_data": ["0x"],
"version": "0x0",
"witnesses": [
"0x7a0000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000c8328aabcd9b9e8e64fbc566c4385c3bdeb219d7210000000000000020302e3131312e3020286161613531353820323032332d30392d313429"
]
},
{
"cell_deps": [
{
"dep_type": "dep_group",
"out_point": {
"index": "0x0",
"tx_hash": "0x5ee6d047719f30ba35065a99a13fbbd69dcf94b56273b23e77e8d0068e6e3984"
}
}
],
"hash": "0x875371e951529d8b246736feea9033d09d3c5f1be4283644b2f49a43d5a5cdcd",
"header_deps": [],
"inputs": [
{
"previous_output": {
"index": "0x7",
"tx_hash": "0x78420da5665c6bfa57abfcb58f3723223d79d55a3e1c771f6b433c72ac776ec9"
},
"since": "0x0"
}
],
"outputs": [
{
"capacity": "0x174876e800",
"lock": {
"args": "0xed17669081a0a2d854d6cc30d2c1c1912680bbe3",
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hash_type": "type"
},
"type": null
},
{
"capacity": "0x1bc16d50064f9160",
"lock": {
"args": "0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7",
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hash_type": "type"
},
"type": null
}
],
"outputs_data": ["0x", "0x"],
"version": "0x0",
"witnesses": [
"0x5500000010000000550000005500000041000000b0e9ea8ed8a50a12d518c565d57cd9baaa9b5bd64e314b721c7c5140c3663fe25149aab99e63667fca71d96ba2c18c80bcbab2a33f40879e9c8ff47a8ca3894a00"
]
}
],
"uncles": []
},
"cycles": ["0x18d041"]
},
"expected": {
"cycles": ["0x18d041"],
"block": {
"header": {
"compactTarget": "0x20010000",
"parentHash": "0x03bb5df51f01aafa4aa3010f86909aaa7a90dc0681c264bb3712e5c02ac1935e",
"transactionsRoot": "0x4dbf22caeebbc7cd35e631689b94e6abb4bd10bf2c175b40d187f997b7acd02f",
"proposalsHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraHash": "0xf6e4abc7a5f61ec64060e2956b972e3d8f450102fa677cb23ee49373c55f14d1",
"dao": "0x5c490f74f540a12eb334c0d0f8862300cac44a092c07000000755322c8fbfe06",
"epoch": "0x3e80096000000",
"hash": "0xbfba299e8942c89f96357b811a795343c0db0f42dd913e22c4fbd74843ef4c5a",
"nonce": "0xa238c12f207efedc2c388a0f2f78fcb",
"number": "0x96",
"timestamp": "0x18ad0b37f4e",
"version": "0x0"
},
"uncles": [],
"transactions": [
{
"cellDeps": [],
"inputs": [
{
"previousOutput": {
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"index": "0xffffffff"
},
"since": "0x96"
}
],
"outputs": [
{
"lock": {
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType": "type",
"args": "0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7"
},
"type": null,
"capacity": "0x2ecbd6b44c"
}
],
"outputsData": ["0x"],
"headerDeps": [],
"hash": "0x4efec35228be483cdf3d58d73829b129e7b9dc6f85fc715e9267f5adeb2326b9",
"version": "0x0",
"witnesses": [
"0x7a0000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000c8328aabcd9b9e8e64fbc566c4385c3bdeb219d7210000000000000020302e3131312e3020286161613531353820323032332d30392d313429"
]
},
{
"cellDeps": [
{
"outPoint": {
"txHash": "0x5ee6d047719f30ba35065a99a13fbbd69dcf94b56273b23e77e8d0068e6e3984",
"index": "0x0"
},
"depType": "depGroup"
}
],
"inputs": [
{
"previousOutput": {
"txHash": "0x78420da5665c6bfa57abfcb58f3723223d79d55a3e1c771f6b433c72ac776ec9",
"index": "0x7"
},
"since": "0x0"
}
],
"outputs": [
{
"lock": {
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType": "type",
"args": "0xed17669081a0a2d854d6cc30d2c1c1912680bbe3"
},
"type": null,
"capacity": "0x174876e800"
},
{
"lock": {
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType": "type",
"args": "0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7"
},
"type": null,
"capacity": "0x1bc16d50064f9160"
}
],
"outputsData": ["0x", "0x"],
"headerDeps": [],
"hash": "0x875371e951529d8b246736feea9033d09d3c5f1be4283644b2f49a43d5a5cdcd",
"version": "0x0",
"witnesses": [
"0x5500000010000000550000005500000041000000b0e9ea8ed8a50a12d518c565d57cd9baaa9b5bd64e314b721c7c5140c3663fe25149aab99e63667fca71d96ba2c18c80bcbab2a33f40879e9c8ff47a8ca3894a00"
]
}
],
"extension": "0x7dc094baf449662901d17c12a6b03af16181537d83aff41689d65ebd90131d7b",
"proposals": []
}
}
}
],
"toAlertMessage": [
Expand Down
42 changes: 27 additions & 15 deletions packages/rpc/src/Base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export const rpcProperties: RpcPropertes = {
// skip subscription
};

// prettier-ignore
interface GetBlock<Q> {
(query:Q, verbosity?: '0x2', withCycle?: false): Promise<CKBComponents.BlockView>;
(query:Q, verbosity: '0x0', withCycle?: false): Promise<string>;
(query:Q, verbosity: '0x0', withCycle: true): Promise<{ block:string, cycles:CKBComponents.UInt64 }>;
(query:Q, verbosity: '0x2', withCycle: true): Promise<{ block: CKBComponents.BlockView; cycles: CKBComponents.UInt64 }>;
}

// prettier-ignore
export interface GetTransaction {
(hash: CKBComponents.Hash): Promise<CKBComponents.TransactionWithStatus>;
Expand All @@ -29,6 +37,12 @@ export interface GetTransaction {
(hash: CKBComponents.Hash, verbosity: "0x2", onlyCommitted?: boolean): Promise<CKBComponents.TransactionWithStatus>;
}

// prettier-ignore
export interface GetHeader<Q> {
(query: Q, verbosity?: "0x1"): Promise<CKBComponents.BlockHeader>;
(query: Q, verbosity: "0x0"): Promise<string>;
}

export interface Base {
/* Chain */

Expand Down Expand Up @@ -80,30 +94,28 @@ export interface Base {
* @method getBlock
* @memberof DefaultRPC
* @description rpc to get block by its hash
* @param {string} hash - the block hash of the target block
* @returns {Promise<object>} block object
* @param {string} hash
* @param {string} verbosity
* @param {boolean} withCycle
* @return {Promise<GetBlock | null>}
*/
getBlock: (hash: CKBComponents.Hash) => Promise<CKBComponents.Block>;
getBlock: GetBlock<CKBComponents.Hash>;

/**
* @method getHeader
* @memberof DefaultRPC
* @description Returns the information about a block header by hash.
* @params {Promise<string>} block hash
*/
getHeader: (
blockHash: CKBComponents.Hash
) => Promise<CKBComponents.BlockHeader>;
getHeader: GetHeader<CKBComponents.Hash>;

/**
* @method getHeaderByNumber
* @memberof DefaultRPC
* @description Returns the information about a block header by block number
* @params {Promise<string>} block number
*/
getHeaderByNumber: (
blockNumber: CKBComponents.BlockNumber | bigint
) => Promise<CKBComponents.BlockHeader>;
getHeaderByNumber: GetHeader<CKBComponents.BlockNumber | bigint>;

/**
* @method getLiveCell
Expand Down Expand Up @@ -189,13 +201,13 @@ export interface Base {
/**
* @method getBlockByNumber
* @memberof DefaultRPC
* @description rpc to get block by its number
* @param {string} number - the block number of the target block
* @returns {Promise<object>} block object
* @description rpc to get block by its hash
* @param {CKBComponents.BlockNumber | bigint} number
* @param {string} verbosity
* @param {boolean} withCycle
* @return {Promise<GetBlock | null>}
*/
getBlockByNumber: (
number: CKBComponents.BlockNumber | bigint
) => Promise<CKBComponents.Block>;
getBlockByNumber: GetBlock<CKBComponents.BlockNumber | bigint>;

/* Experimental */

Expand Down
Loading