-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathprovider.ts
85 lines (65 loc) · 2.21 KB
/
provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { ethers } from "ethers";
import memoizeOne from "memoize-one";
import memoize from "mem";
import { assert } from "lib/system/assert";
import { MessageType, RpcResponse, ActivitySource } from "core/types";
import { porter } from "./base";
export const getClientProvider = memoize(
(chainId: number) => new ClientProvider(chainId),
);
export class ClientProvider extends ethers.JsonRpcApiProvider {
constructor(public chainId: number) {
super(chainId);
}
source?: ActivitySource;
getNetwork = memoizeOne(super.getNetwork.bind(this));
getCode = memoize(super.getCode.bind(this));
getUncheckedSigner = memoize(
(address: string) => new ethers.JsonRpcSigner(this, address),
);
getVoidSigner = memoize(
(address: string) => new ethers.VoidSigner(address, this),
);
setActivitySource = (source?: ActivitySource) => {
this.source = source;
};
async send(method: string, params: Array<any>): Promise<any> {
const res = await this.sendRpc(method, params);
return getResult(res.response);
}
async _send(
payload: ethers.JsonRpcPayload | Array<ethers.JsonRpcPayload>,
): Promise<Array<ethers.JsonRpcResult>> {
const payloadArr = Array.isArray(payload) ? payload : [payload];
const responses = await Promise.all(
payloadArr.map(async ({ jsonrpc, id, method, params }) => {
// TODO: Check JSONRPC params types
const res = await this.sendRpc(method, params as any);
return { jsonrpc, id, ...res.response };
}),
);
return responses as any;
}
async populateTransaction(transaction: Partial<ethers.TransactionRequest>) {
return transaction;
}
private async sendRpc(method: string, params: Array<any>) {
const type = MessageType.SendRpc;
const chainId = this.chainId;
const res = await porter.request(
{ type, chainId, method, params, source: this.source },
{ timeout: 0 },
);
assert(res?.type === type);
return res;
}
}
function getResult(response: RpcResponse): any {
if ("error" in response) {
const error = new Error(response.error.message);
(error as any).code = response.error.code;
(error as any).data = response.error.data;
throw error;
}
return response.result;
}