2023-07-05 08:01:12 +00:00
|
|
|
import { ErrTuple } from "@lumeweb/libkernel";
|
2023-03-23 16:42:21 +00:00
|
|
|
import type {
|
|
|
|
RPCRequest,
|
|
|
|
RPCResponse,
|
|
|
|
ClientRPCRequest,
|
|
|
|
} from "@lumeweb/interface-relay";
|
|
|
|
import { RpcQueryOptions } from "@lumeweb/rpc-client";
|
2022-08-31 01:39:26 +00:00
|
|
|
import { Buffer } from "buffer";
|
2023-07-05 08:01:12 +00:00
|
|
|
import { Client, factory } from "@lumeweb/libkernel/module";
|
2022-07-20 07:35:58 +00:00
|
|
|
|
2023-07-24 18:11:00 +00:00
|
|
|
const MODULE = "zduQ2tjJYfQQmBh7P5eVJp3ajMV3XgVpgeuEwvFAXGzG3dJ4M8CbXL5SDu";
|
2022-07-20 07:35:58 +00:00
|
|
|
|
2023-03-23 16:42:21 +00:00
|
|
|
export class RpcNetwork extends Client {
|
2022-08-31 18:42:26 +00:00
|
|
|
private _def: boolean;
|
|
|
|
|
2023-07-05 08:01:12 +00:00
|
|
|
constructor(module: string, def: boolean = true) {
|
|
|
|
super(module);
|
2022-08-31 18:42:26 +00:00
|
|
|
this._def = def;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _networkId: number = 0;
|
|
|
|
|
|
|
|
get networkId(): number {
|
|
|
|
return this._networkId;
|
|
|
|
}
|
2022-07-20 07:35:58 +00:00
|
|
|
|
|
|
|
get ready(): Promise<ErrTuple> {
|
2022-08-31 18:42:26 +00:00
|
|
|
if (this._def) {
|
2022-08-31 19:25:18 +00:00
|
|
|
this._networkId = 1;
|
2022-08-31 18:42:26 +00:00
|
|
|
} else {
|
2023-03-23 16:42:21 +00:00
|
|
|
Promise.resolve()
|
2023-07-08 11:52:36 +00:00
|
|
|
.then(() => this.callModuleReturn(MODULE, "createNetwork"))
|
2022-08-31 18:42:26 +00:00
|
|
|
.then((ret: ErrTuple) => (this._networkId = ret[0]));
|
|
|
|
}
|
|
|
|
|
2023-03-23 16:42:21 +00:00
|
|
|
return this.callModuleReturn("ready", {
|
|
|
|
network: this._networkId,
|
|
|
|
});
|
2022-08-31 01:39:26 +00:00
|
|
|
}
|
2023-07-05 08:01:12 +00:00
|
|
|
|
2023-03-24 17:20:37 +00:00
|
|
|
public simpleQuery({
|
|
|
|
relay,
|
|
|
|
query,
|
|
|
|
options = {},
|
|
|
|
}: {
|
|
|
|
relay?: Buffer | string;
|
|
|
|
query: ClientRPCRequest;
|
2023-03-24 17:28:54 +00:00
|
|
|
options?: RpcQueryOptions;
|
2023-03-24 17:20:37 +00:00
|
|
|
}): SimpleRpcQuery {
|
2023-03-24 19:58:32 +00:00
|
|
|
return createSimpleRpcQuery({
|
2023-03-23 16:42:21 +00:00
|
|
|
network: this,
|
2022-08-31 01:39:26 +00:00
|
|
|
relay,
|
2023-03-23 16:42:21 +00:00
|
|
|
query,
|
|
|
|
options,
|
|
|
|
}).run();
|
2022-07-20 07:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 16:42:21 +00:00
|
|
|
export abstract class RpcQueryBase extends Client {
|
2022-08-31 01:39:26 +00:00
|
|
|
protected _promise?: Promise<any>;
|
|
|
|
protected _network: RpcNetwork;
|
|
|
|
protected _query: RPCRequest;
|
|
|
|
protected _options: RpcQueryOptions;
|
|
|
|
protected _queryType: string;
|
|
|
|
|
|
|
|
constructor(
|
2023-07-05 08:01:12 +00:00
|
|
|
module: string,
|
2022-08-31 01:39:26 +00:00
|
|
|
network: RpcNetwork,
|
|
|
|
query: RPCRequest,
|
|
|
|
options: RpcQueryOptions = {},
|
2023-07-05 08:01:12 +00:00
|
|
|
queryType: string,
|
2022-08-31 01:39:26 +00:00
|
|
|
) {
|
2023-07-05 08:01:12 +00:00
|
|
|
super(module);
|
2022-08-31 01:39:26 +00:00
|
|
|
this._network = network;
|
|
|
|
this._query = query;
|
|
|
|
this._options = options;
|
|
|
|
this._queryType = queryType;
|
|
|
|
}
|
2022-07-20 07:35:58 +00:00
|
|
|
|
2022-08-31 18:42:26 +00:00
|
|
|
get result(): Promise<RPCResponse> {
|
2023-03-23 16:42:21 +00:00
|
|
|
return (this._promise as Promise<any>)
|
|
|
|
.then((result: ErrTuple): RPCResponse => {
|
|
|
|
return result[0];
|
|
|
|
})
|
|
|
|
.catch((error: Error) => {
|
|
|
|
return { error: error.message };
|
|
|
|
});
|
2022-08-31 18:42:26 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 01:39:26 +00:00
|
|
|
public run(): this {
|
2023-03-23 16:42:21 +00:00
|
|
|
this._promise = this.callModule(this._queryType, {
|
|
|
|
query: this._query,
|
|
|
|
options: this._options,
|
|
|
|
network: this._network.networkId,
|
|
|
|
});
|
2022-08-31 01:39:26 +00:00
|
|
|
|
|
|
|
return this;
|
2022-07-20 07:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-31 01:39:26 +00:00
|
|
|
|
|
|
|
export class SimpleRpcQuery extends RpcQueryBase {
|
2023-03-24 17:20:37 +00:00
|
|
|
protected _relay?: string | Buffer;
|
2023-07-05 08:01:12 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
module: string,
|
|
|
|
{
|
|
|
|
network,
|
|
|
|
relay,
|
|
|
|
query,
|
|
|
|
options,
|
|
|
|
}: {
|
|
|
|
network: RpcNetwork;
|
|
|
|
relay?: string | Buffer;
|
|
|
|
query: RPCRequest;
|
|
|
|
options?: RpcQueryOptions;
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
super(module, network, query, options, "simpleQuery");
|
2022-08-31 21:32:52 +00:00
|
|
|
this._relay = relay;
|
|
|
|
}
|
2023-07-05 08:01:12 +00:00
|
|
|
|
2022-08-31 21:32:52 +00:00
|
|
|
public run(): this {
|
2023-03-23 16:42:21 +00:00
|
|
|
this._promise = this.callModule(this._queryType, {
|
|
|
|
relay: this._relay,
|
|
|
|
query: this._query,
|
|
|
|
options: this._options,
|
|
|
|
network: this._network.networkId,
|
2022-09-01 00:10:24 +00:00
|
|
|
});
|
2022-08-31 01:39:26 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 17:02:29 +00:00
|
|
|
|
2023-07-08 11:52:36 +00:00
|
|
|
export const createClient = factory<RpcNetwork>(RpcNetwork, MODULE);
|
|
|
|
const createSimpleRpcQuery = factory<SimpleRpcQuery>(SimpleRpcQuery, MODULE);
|