This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
client.ts
247 lines (220 loc) · 6.8 KB
/
client.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const newrelic = require("newrelic");
import { Hash, HexNumber, HexString, Script } from "@ckb-lumos/base";
import { Reader } from "@ckb-lumos/toolkit";
import { RPC } from "./rpc";
import {
BlockParameter,
L2Transaction,
L2TransactionReceipt,
L2TransactionWithStatus,
NodeInfo,
RawL2Transaction,
RegistryAddress,
RunResult,
U128,
U32,
} from "./types";
import { SerializeL2Transaction, SerializeRawL2Transaction } from "../schemas";
import {
NormalizeL2Transaction,
NormalizeRawL2Transaction,
} from "./normalizers";
import { logger } from "./logger";
export class GodwokenClient {
private rpc: RPC;
private readonlyRpc: RPC;
constructor(url: string, readonlyUrl?: string) {
this.rpc = new RPC(url);
this.readonlyRpc = !!readonlyUrl ? new RPC(readonlyUrl) : this.rpc;
}
// This RPC only for fullnode
public async isRequestInQueue(hash: Hash): Promise<boolean> {
const result = await this.writeRpcCall("is_request_in_queue", hash);
return result;
}
public async getScriptHash(accountId: U32): Promise<Hash> {
const hash = await this.rpcCall("get_script_hash", toHex(accountId));
return hash;
}
public async getAccountIdByScriptHash(
scriptHash: Hash
): Promise<U32 | undefined> {
const accountId: HexNumber | undefined = await this.rpcCall(
"get_account_id_by_script_hash",
scriptHash
);
if (accountId == null) {
return undefined;
}
return +accountId;
}
public async getRegistryAddressByScriptHash(
scriptHash: Hash,
registryId: U32
): Promise<RegistryAddress | undefined> {
const registryAddress = await this.rpcCall(
"get_registry_address_by_script_hash",
scriptHash,
toHex(registryId)
);
return registryAddress;
}
public async getScriptHashByRegistryAddress(
serializedRegistryAddress: HexString
): Promise<Hash | undefined> {
const scriptHash = await this.rpcCall(
"get_script_hash_by_registry_address",
serializedRegistryAddress
);
return scriptHash;
}
public async getBalance(
serializedRegistryAddress: HexString,
sudtId: U32,
blockParameter?: BlockParameter
): Promise<U128> {
const balance: HexNumber = await this.rpcCall(
"get_balance",
serializedRegistryAddress,
toHex(sudtId),
toHex(blockParameter)
);
return BigInt(balance);
}
public async getStorageAt(
accountId: U32,
key: HexString,
blockParameter?: BlockParameter
): Promise<Hash> {
return await this.rpcCall(
"get_storage_at",
toHex(accountId),
key,
toHex(blockParameter)
);
}
public async getScript(scriptHash: Hash): Promise<Script | undefined> {
return await this.rpcCall("get_script", scriptHash);
}
public async getNonce(
accountId: U32,
blockParameter?: BlockParameter
): Promise<U32> {
const nonce: HexNumber = await this.writeRpcCall(
"get_nonce",
toHex(accountId),
toHex(blockParameter)
);
return +nonce;
}
public async getData(
dataHash: Hash,
blockParameter?: BlockParameter
): Promise<HexString> {
return await this.rpcCall("get_data", dataHash, toHex(blockParameter));
}
// Don't log `invalid exit code 83` error
public async executeForGetAccountScriptHash(
rawL2tx: RawL2Transaction,
blockParameter?: BlockParameter
): Promise<RunResult> {
const data: HexString = new Reader(
SerializeRawL2Transaction(NormalizeRawL2Transaction(rawL2tx))
).serializeJson();
const name = "gw_execute_raw_l2transaction";
const result = await this.readonlyRpc[name](data, toHex(blockParameter));
return result;
}
public async executeRawL2Transaction(
rawL2tx: RawL2Transaction,
blockParameter?: BlockParameter,
serializedRegistryAddress?: HexString
): Promise<RunResult> {
const data: HexString = new Reader(
SerializeRawL2Transaction(NormalizeRawL2Transaction(rawL2tx))
).serializeJson();
const params = [data, toHex(blockParameter)];
if (serializedRegistryAddress != null) {
params.push(serializedRegistryAddress);
}
return await this.rpcCall("execute_raw_l2transaction", ...params);
}
public async executeL2Transaction(l2tx: L2Transaction): Promise<RunResult> {
const data: HexString = new Reader(
SerializeL2Transaction(NormalizeL2Transaction(l2tx))
).serializeJson();
return await this.rpcCall("execute_l2transaction", data);
}
public async submitL2Transaction(
l2tx: L2Transaction
): Promise<Hash | undefined> {
const data: HexString = new Reader(
SerializeL2Transaction(NormalizeL2Transaction(l2tx))
).serializeJson();
return await this.writeRpcCall("submit_l2transaction", data);
}
public async getTransaction(
hash: Hash
): Promise<L2TransactionWithStatus | undefined> {
const txWithStatus = await this.rpcCall("get_transaction", hash);
if (txWithStatus == null && this.rpc !== this.readonlyRpc) {
// Only fullnode has queue info
return await this.writeRpcCall("get_transaction", hash);
}
return txWithStatus;
}
public async getTransactionReceipt(
hash: Hash
): Promise<L2TransactionReceipt | undefined> {
return await this.rpcCall("get_transaction_receipt", hash);
}
public async getNodeInfo(): Promise<NodeInfo> {
return await this.rpcCall("get_node_info");
}
public async getTipBlockHash(): Promise<HexString> {
return await this.rpcCall("get_tip_block_hash");
}
public async getMemPoolStateRoot(): Promise<HexString> {
return await this.rpcCall("get_mem_pool_state_root");
}
public async ping(): Promise<string> {
const result = await this.rpcCall("ping");
return result;
}
public async pingFullNode(): Promise<string> {
const result = await this.writeRpcCall("ping");
return result;
}
public async getBlock(blockHash: HexString): Promise<any> {
const result = await this.rpcCall("get_block", blockHash);
return result;
}
private async rpcCall(methodName: string, ...args: any[]): Promise<any> {
const name = "gw_" + methodName;
try {
return await newrelic.startSegment(`read_${name}`, true, async () => {
return this.readonlyRpc[name](...args);
});
} catch (err: any) {
logger.info(`Call gw rpc "${name}" error:`, err.message);
throw err;
}
}
private async writeRpcCall(methodName: string, ...args: any[]): Promise<any> {
const name = "gw_" + methodName;
try {
return await newrelic.startSegment(`write_${name}`, true, async () => {
return this.rpc[name](...args);
});
} catch (err: any) {
logger.info(`Call gw rpc "${name}" error:`, err.message);
throw err;
}
}
}
function toHex(num: number | bigint | undefined | null): HexNumber | undefined {
if (num == null) {
return undefined;
}
return "0x" + num.toString(16);
}