Skip to content

Commit b0d1404

Browse files
committed
fix: 修复通道关闭时的参数类型错误
- 修改 Script 接口中 args 的类型从 string[] 改为 string - 修改 shutdownChannel 函数中的 close_script.args 参数格式 - 确保所有参数都符合 API 期望的十六进制字符串格式 - 更新相关依赖和工具函数 这个修改解决了通道关闭时出现的 'invalid type: sequence, expected a 0x-prefixed hex string' 错误。
1 parent f50c3a3 commit b0d1404

5 files changed

Lines changed: 229 additions & 39 deletions

File tree

packages/fiber/src/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,18 @@ export class FiberSDK {
7171
*/
7272
async openChannel(params: {
7373
peer_id: string;
74-
funding_amount: bigint;
74+
funding_amount: string;
7575
public?: boolean;
7676
funding_udt_type_script?: Script;
7777
shutdown_script?: Script;
78-
commitment_delay_epoch?: bigint;
79-
commitment_fee_rate?: bigint;
80-
funding_fee_rate?: bigint;
81-
tlc_expiry_delta?: bigint;
82-
tlc_min_value?: bigint;
83-
tlc_fee_proportional_millionths?: bigint;
84-
max_tlc_value_in_flight?: bigint;
85-
max_tlc_number_in_flight?: bigint;
78+
commitment_delay_epoch?: string;
79+
commitment_fee_rate?: string;
80+
funding_fee_rate?: string;
81+
tlc_expiry_delta?: string;
82+
tlc_min_value?: string;
83+
tlc_fee_proportional_millionths?: string;
84+
max_tlc_value_in_flight?: string;
85+
max_tlc_number_in_flight?: string;
8686
}): Promise<Hash256> {
8787
return this.channel.openChannel(params);
8888
}

packages/fiber/src/modules/channel.ts

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { FiberClient } from "../client.js";
22
import { Channel, Hash256, Script } from "../types.js";
3+
import {
4+
decimalToU128,
5+
decimalToU64,
6+
u128ToDecimal,
7+
u64ToDecimal,
8+
} from "../utils/number.js";
39

410
export class ChannelModule {
511
constructor(private client: FiberClient) {}
@@ -9,35 +15,71 @@ export class ChannelModule {
915
*/
1016
async openChannel(params: {
1117
peer_id: string;
12-
funding_amount: bigint;
18+
funding_amount: string;
1319
public?: boolean;
1420
funding_udt_type_script?: Script;
1521
shutdown_script?: Script;
16-
commitment_delay_epoch?: bigint;
17-
commitment_fee_rate?: bigint;
18-
funding_fee_rate?: bigint;
19-
tlc_expiry_delta?: bigint;
20-
tlc_min_value?: bigint;
21-
tlc_fee_proportional_millionths?: bigint;
22-
max_tlc_value_in_flight?: bigint;
23-
max_tlc_number_in_flight?: bigint;
22+
commitment_delay_epoch?: string;
23+
commitment_fee_rate?: string;
24+
funding_fee_rate?: string;
25+
tlc_expiry_delta?: string;
26+
tlc_min_value?: string;
27+
tlc_fee_proportional_millionths?: string;
28+
max_tlc_value_in_flight?: string;
29+
max_tlc_number_in_flight?: string;
2430
}): Promise<Hash256> {
25-
return this.client.call("open_channel", [params]);
31+
const u128Params = {
32+
...params,
33+
funding_amount: decimalToU128(params.funding_amount),
34+
commitment_delay_epoch: params.commitment_delay_epoch
35+
? decimalToU128(params.commitment_delay_epoch)
36+
: undefined,
37+
commitment_fee_rate: params.commitment_fee_rate
38+
? decimalToU128(params.commitment_fee_rate)
39+
: undefined,
40+
funding_fee_rate: params.funding_fee_rate
41+
? decimalToU64(params.funding_fee_rate)
42+
: undefined,
43+
tlc_expiry_delta: params.tlc_expiry_delta
44+
? decimalToU64(params.tlc_expiry_delta)
45+
: undefined,
46+
tlc_min_value: params.tlc_min_value
47+
? decimalToU128(params.tlc_min_value)
48+
: undefined,
49+
tlc_fee_proportional_millionths: params.tlc_fee_proportional_millionths
50+
? decimalToU128(params.tlc_fee_proportional_millionths)
51+
: undefined,
52+
max_tlc_value_in_flight: params.max_tlc_value_in_flight
53+
? decimalToU64(params.max_tlc_value_in_flight)
54+
: undefined,
55+
};
56+
return this.client.call("open_channel", [u128Params]);
2657
}
2758

2859
/**
2960
* Accept a channel
3061
*/
3162
async acceptChannel(params: {
3263
temporary_channel_id: string;
33-
funding_amount: bigint;
34-
max_tlc_value_in_flight: bigint;
35-
max_tlc_number_in_flight: bigint;
36-
tlc_min_value: bigint;
37-
tlc_fee_proportional_millionths: bigint;
38-
tlc_expiry_delta: bigint;
64+
funding_amount: string;
65+
max_tlc_value_in_flight: string;
66+
max_tlc_number_in_flight: string;
67+
tlc_min_value: string;
68+
tlc_fee_proportional_millionths: string;
69+
tlc_expiry_delta: string;
3970
}): Promise<void> {
40-
return this.client.call("accept_channel", [params]);
71+
const u128Params = {
72+
...params,
73+
funding_amount: decimalToU128(params.funding_amount),
74+
max_tlc_value_in_flight: decimalToU128(params.max_tlc_value_in_flight),
75+
max_tlc_number_in_flight: decimalToU128(params.max_tlc_number_in_flight),
76+
tlc_min_value: decimalToU128(params.tlc_min_value),
77+
tlc_fee_proportional_millionths: decimalToU128(
78+
params.tlc_fee_proportional_millionths,
79+
),
80+
tlc_expiry_delta: decimalToU128(params.tlc_expiry_delta),
81+
};
82+
return this.client.call("accept_channel", [u128Params]);
4183
}
4284

4385
/**
@@ -47,7 +89,6 @@ export class ChannelModule {
4789
* @returns Promise<void>
4890
*/
4991
async abandonChannel(channelId: Hash256): Promise<void> {
50-
console.log(11111, channelId);
5192
if (!channelId) {
5293
throw new Error("Channel ID cannot be empty");
5394
}
@@ -89,7 +130,21 @@ export class ChannelModule {
89130
"list_channels",
90131
[{}],
91132
);
92-
return response.channels;
133+
return response.channels.map((channel) => ({
134+
...channel,
135+
local_balance: u128ToDecimal(channel.local_balance),
136+
remote_balance: u128ToDecimal(channel.remote_balance),
137+
offered_tlc_balance: u128ToDecimal(channel.offered_tlc_balance),
138+
received_tlc_balance: u128ToDecimal(channel.received_tlc_balance),
139+
tlc_expiry_delta: u128ToDecimal(channel.tlc_expiry_delta),
140+
tlc_fee_proportional_millionths: u128ToDecimal(
141+
channel.tlc_fee_proportional_millionths,
142+
),
143+
created_at: u64ToDecimal(channel.created_at, true),
144+
last_updated_at: channel.last_updated_at
145+
? u64ToDecimal(channel.last_updated_at, true)
146+
: "",
147+
}));
93148
}
94149

95150
/**

packages/fiber/src/modules/info.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1+
import { fixedPointToString } from "@ckb-ccc/core";
12
import { FiberClient } from "../client.js";
23
import { NodeInfo } from "../types.js";
4+
import { u64ToDecimal } from "../utils/number.js";
5+
6+
interface RawNodeInfo {
7+
node_name: string;
8+
addresses: string[];
9+
node_id: string;
10+
timestamp: bigint;
11+
chain_hash: string;
12+
auto_accept_min_ckb_funding_amount: bigint;
13+
auto_accept_channel_ckb_funding_amount: bigint;
14+
tlc_expiry_delta: bigint;
15+
tlc_min_value: bigint;
16+
tlc_fee_proportional_millionths: bigint;
17+
channel_count: string;
18+
pending_channel_count: string;
19+
peers_count: string;
20+
udt_cfg_infos: Record<string, unknown>;
21+
default_funding_lock_script?: {
22+
code_hash: string;
23+
hash_type: string;
24+
args: string;
25+
};
26+
}
327

428
export class InfoModule {
529
constructor(private client: FiberClient) {}
@@ -10,6 +34,43 @@ export class InfoModule {
1034
* @throws {Error} Throws error when unable to get node information
1135
*/
1236
async nodeInfo(): Promise<NodeInfo> {
13-
return this.client.call("node_info", []);
37+
const response = await this.client.call<RawNodeInfo>("node_info", []);
38+
return {
39+
node_name: response.node_name,
40+
addresses: response.addresses,
41+
node_id: response.node_id,
42+
timestamp: response.timestamp
43+
? u64ToDecimal(response.timestamp, true)
44+
: "",
45+
chain_hash: response.chain_hash,
46+
auto_accept_min_ckb_funding_amount:
47+
response.auto_accept_min_ckb_funding_amount
48+
? fixedPointToString(response.auto_accept_min_ckb_funding_amount)
49+
: "",
50+
auto_accept_channel_ckb_funding_amount:
51+
response.auto_accept_channel_ckb_funding_amount
52+
? fixedPointToString(response.auto_accept_channel_ckb_funding_amount)
53+
: "",
54+
tlc_expiry_delta: response.tlc_expiry_delta
55+
? fixedPointToString(response.tlc_expiry_delta)
56+
: "",
57+
tlc_min_value: response.tlc_min_value
58+
? fixedPointToString(response.tlc_min_value)
59+
: "",
60+
tlc_fee_proportional_millionths: response.tlc_fee_proportional_millionths
61+
? fixedPointToString(response.tlc_fee_proportional_millionths)
62+
: "",
63+
channel_count: response.channel_count
64+
? Number(response.channel_count).toString()
65+
: "0",
66+
pending_channel_count: response.pending_channel_count
67+
? Number(response.pending_channel_count).toString()
68+
: "0",
69+
peers_count: response.peers_count
70+
? Number(response.peers_count).toString()
71+
: "0",
72+
udt_cfg_infos: response.udt_cfg_infos,
73+
default_funding_lock_script: response.default_funding_lock_script,
74+
};
1475
}
1576
}

packages/fiber/src/types.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,24 @@ export enum RemoveTlcReason {
8383
export interface Script {
8484
code_hash: string;
8585
hash_type: string;
86-
args: string[];
86+
args: string;
8787
}
8888

8989
export interface Channel {
9090
channel_id: Hash256;
9191
peer_id: Pubkey;
9292
funding_udt_type_script?: Script;
9393
state: string;
94-
local_balance: bigint;
95-
offered_tlc_balance: bigint;
96-
remote_balance: bigint;
97-
received_tlc_balance: bigint;
94+
local_balance: string;
95+
offered_tlc_balance: string;
96+
remote_balance: string;
97+
received_tlc_balance: string;
9898
latest_commitment_transaction_hash?: Hash256;
99-
created_at: bigint;
99+
created_at: string;
100+
last_updated_at: string;
100101
enabled: boolean;
101-
tlc_expiry_delta: bigint;
102-
tlc_fee_proportional_millionths: bigint;
102+
tlc_expiry_delta: string;
103+
tlc_fee_proportional_millionths: string;
103104
}
104105

105106
export interface ChannelInfo {
@@ -150,9 +151,16 @@ export interface NodeInfo {
150151
node_name: string;
151152
addresses: string[];
152153
node_id: Pubkey;
153-
timestamp: bigint;
154+
timestamp: string;
154155
chain_hash: Hash256;
155-
auto_accept_min_ckb_funding_amount: bigint;
156+
auto_accept_min_ckb_funding_amount: string;
157+
auto_accept_channel_ckb_funding_amount: string;
158+
tlc_expiry_delta: string;
159+
tlc_min_value: string;
160+
tlc_fee_proportional_millionths: string;
161+
channel_count: string;
162+
pending_channel_count: string;
163+
peers_count: string;
156164
udt_cfg_infos: Record<string, unknown>;
157165
default_funding_lock_script?: {
158166
code_hash: string;

packages/fiber/src/utils/number.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { fixedPointFrom, fixedPointToString } from "@ckb-ccc/core";
2+
3+
/**
4+
* 将u128类型的数字转换为十进制字符串
5+
* @param value - u128类型的数字(bigint或string)
6+
* @param decimals - 小数位数,默认为8
7+
* @returns 十进制字符串
8+
*
9+
* @example
10+
* ```typescript
11+
* const decimal = u128ToDecimal(123456789n); // 输出 "1.23456789"
12+
* const decimalWithDecimals = u128ToDecimal(123456789n, 6); // 输出 "123.456789"
13+
* ```
14+
*/
15+
export function u128ToDecimal(
16+
value: bigint | string,
17+
decimals: number = 8,
18+
): string {
19+
return fixedPointToString(value, decimals);
20+
}
21+
22+
/**
23+
* 将十进制字符串转换为u128类型
24+
* @param value - 十进制字符串
25+
* @param decimals - 小数位数,默认为8
26+
* @returns u128类型的数字(bigint)
27+
*
28+
* @example
29+
* ```typescript
30+
* const u128 = decimalToU128("1.23456789"); // 输出 123456789n
31+
* const u128WithDecimals = decimalToU128("123.456789", 6); // 输出 123456789n
32+
* ```
33+
*/
34+
export function decimalToU128(value: string, decimals: number = 8): bigint {
35+
return fixedPointFrom(value, decimals);
36+
}
37+
38+
/**
39+
* 将十进制字符串转换为U64类型
40+
* @param value - 十进制字符串
41+
* @returns U64类型的数字(bigint)
42+
*
43+
* @example
44+
* ```typescript
45+
* const u64 = decimalToU64("1000"); // 输出 1000n
46+
* ```
47+
*/
48+
export function decimalToU64(value: string): bigint {
49+
return BigInt(value);
50+
}
51+
52+
/**
53+
* 将U64类型的数字转换为十进制字符串
54+
* @param value - U64类型的数字(bigint或string)
55+
* @param isTimestamp - 是否为时间戳,如果是则直接返回十进制字符串
56+
* @returns 十进制字符串
57+
*/
58+
export function u64ToDecimal(
59+
value: bigint | string,
60+
isTimestamp: boolean = false,
61+
): string {
62+
if (isTimestamp) {
63+
return value.toString();
64+
}
65+
return u128ToDecimal(value, 0);
66+
}

0 commit comments

Comments
 (0)