Skip to content

Commit

Permalink
fix: correct CKBRPC.createBatchRequest() types (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
twhy authored May 13, 2024
1 parent ed7c2fa commit 14ed633
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-turtles-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-lumos/rpc": patch
---

fix: correct CKBRPC.createBatchRequest() types
18 changes: 8 additions & 10 deletions packages/rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import AbortController from "abort-controller";
export const ParamsFormatter = paramsFormatter;
export const ResultFormatter = resultFormatter;

export const DEFAULT_RPC_TIMEOUT = 30000;

export class CKBRPC extends Base {
#config: RPCConfig;
#node: CKBComponents.Node = {
Expand All @@ -40,7 +42,7 @@ export class CKBRPC extends Base {
constructor(url: string, config: Partial<RPCConfig> = {}) {
super();
this.setNode({ url });
const { timeout = 30000, fetch = fetch_ } = config;
const { timeout = DEFAULT_RPC_TIMEOUT, fetch = fetch_ } = config;
this.#config = { timeout, fetch };

Object.defineProperties(this, {
Expand Down Expand Up @@ -86,14 +88,11 @@ export class CKBRPC extends Base {
P extends (string | number | object)[],
R = any[]
>(
// TODO fix me
// params: [method: N, ...rest: P][] = [],
params: any = []
params: [method: N, ...rest: P][] = []
) => {
const ctx = this;

// TODO fix me
const proxied: any = new Proxy([], {
const proxied: [method: N, ...rest: P][] = new Proxy([], {
set(...p) {
const methods = Object.keys(ctx);
if (p[1] !== "length") {
Expand Down Expand Up @@ -121,8 +120,7 @@ export class CKBRPC extends Base {
},
exec: {
async value() {
// TODO fix me
const payload = proxied.map(([f, ...p]: any, i: any) => {
const payload = proxied.map(([f, ...p], i) => {
try {
const method = new Method(ctx.node, {
...ctx.rpcProperties[f],
Expand Down Expand Up @@ -165,8 +163,8 @@ export class CKBRPC extends Base {
},
},
});
// TODO fix me
params.forEach((p: any) => proxied.push(p));

params.forEach((p) => proxied.push(p));

return proxied as typeof proxied & {
add: (n: N, ...p: P) => typeof proxied;
Expand Down
4 changes: 3 additions & 1 deletion packages/rpc/src/method.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IdNotMatchException, ResponseException } from "./exceptions";
import { CKBComponents } from "./types/api";
import { RPCConfig } from "./types/common";
import { DEFAULT_RPC_TIMEOUT } from ".";
import AbortController from "abort-controller";
import fetch_ from "cross-fetch";

Expand Down Expand Up @@ -29,7 +30,7 @@ export class Method {
this.#node = node;
this.#options = options;
this.#name = options.name;
const { timeout = 30000, fetch = fetch_ } = config;
const { timeout = DEFAULT_RPC_TIMEOUT, fetch = fetch_ } = config;
this.#config = { timeout, fetch };

Object.defineProperty(this.call, "name", {
Expand Down Expand Up @@ -78,6 +79,7 @@ export class Method {
this.#options.paramsFormatters[i](p)) ||
p
);
/* eslint-disable @typescript-eslint/no-magic-numbers */
const id = Math.round(Math.random() * 10000);
const payload = {
id,
Expand Down

1 comment on commit 14ed633

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 New canary release: 0.0.0-canary-14ed633-20240513054710

npm install @ckb-lumos/[email protected]

Please sign in to comment.