-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
190 lines (153 loc) · 5.13 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
ErrorCode,
JsonRpcSigner,
UnsupportedOperationError,
ethers,
makeError,
toQuantity,
} from "ethers";
import EventEmitter from "events";
export class Eip1193Bridge extends EventEmitter {
readonly signer: JsonRpcSigner;
readonly provider: JsonRpcSigner["provider"];
constructor(signer: JsonRpcSigner, provider?: JsonRpcSigner["provider"]) {
super();
this.signer = signer;
this.provider = provider ?? signer.provider;
}
request(request: { method: string; params?: Array<any> }): Promise<any> {
return this.send(request.method, request.params || []);
}
async send(method: string, params?: any[]): Promise<any> {
function throwUnsupported(message: string): UnsupportedOperationError {
const erroInfo = {
operation: method,
};
return makeError<ErrorCode, UnsupportedOperationError>(
message,
"UNSUPPORTED_OPERATION",
erroInfo
);
}
let coerce = (value: any) => value;
switch (method) {
case "eth_gasPrice": {
const result = (await this.provider.getFeeData()).gasPrice;
return result ? toQuantity(result) : null;
}
case "eth_accounts": {
const result = [];
if (this.signer) {
const address = await this.signer.getAddress();
result.push(address);
}
return result;
}
case "eth_blockNumber": {
return await this.provider.getBlockNumber();
}
case "eth_chainId": {
const result = await this.provider.getNetwork();
return toQuantity(result.chainId);
}
case "eth_getBalance": {
const result = await this.provider.getBalance(params![0], params![1]);
return toQuantity(result);
}
case "eth_getStorageAt": {
return this.provider.getStorage(params![0], params![1], params![2]);
}
case "eth_getTransactionCount": {
const result = await this.provider.getTransactionCount(
params![0],
params![1]
);
return toQuantity(BigInt(result));
}
case "eth_getBlockTransactionCountByHash":
case "eth_getBlockTransactionCountByNumber": {
const result = await this.provider.getBlock(params![0]);
return result ? toQuantity(BigInt(result.transactions.length)) : result;
}
case "eth_getCode": {
const result = await this.provider.getCode(params![0], params![1]);
return result;
}
case "eth_sendRawTransaction": {
return await this.signer.sendTransaction(params![0]);
}
case "eth_call": {
return await this.provider.call(params![0]);
}
case "estimateGas": {
if (params![1] && params![1] !== "latest") {
throwUnsupported("estimateGas does not support blockTag");
}
const result = await this.provider.estimateGas(params![0]);
return toQuantity(result);
}
// @TODO: Transform? No uncles?
case "eth_getBlockByHash":
case "eth_getBlockByNumber": {
if (params![1]) {
return await this.provider.getBlock(params![0], true);
} else {
return await this.provider.getBlock(params![0]);
}
}
case "eth_getTransactionByHash": {
return await this.provider.getTransaction(params![0]);
}
case "eth_getTransactionReceipt": {
return await this.provider.getTransactionReceipt(params![0]);
}
case "eth_sign": {
if (!this.signer) {
return throwUnsupported("eth_sign requires an account");
}
const address = await this.signer.getAddress();
if (address !== ethers.getAddress(params![0])) {
makeError(
"account mismatch or account not found",
"INVALID_ARGUMENT",
{ argument: "address", value: params![0] }
);
}
return this.signer.signMessage(ethers.getBytes(params![1]));
}
case "eth_sendTransaction": {
if (!this.signer) {
return throwUnsupported("eth_sendTransaction requires an account");
}
const args = { ...params![0] };
delete args.gas;
const tx = await this.signer.sendTransaction(args as any);
return tx.hash;
}
case "eth_getUncleCountByBlockHash":
case "eth_getUncleCountByBlockNumber": {
coerce = ethers.toQuantity;
break;
}
case "eth_getTransactionByBlockHashAndIndex":
case "eth_getTransactionByBlockNumberAndIndex":
case "eth_getUncleByBlockHashAndIndex":
case "eth_getUncleByBlockNumberAndIndex":
case "eth_newFilter":
case "eth_newBlockFilter":
case "eth_newPendingTransactionFilter":
case "eth_uninstallFilter":
case "eth_getFilterChanges":
case "eth_getFilterLogs":
case "eth_getLogs":
break;
}
// If our provider supports send, maybe it can do a better job?
if ((<any>this.provider).send) {
const result = await (<any>this.provider).send(method, params);
return coerce(result);
}
return throwUnsupported(`unsupported method: ${method}`);
}
}