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

fix: correct CKBRPC.createBatchRequest() types #683

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading