-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathApiHandler.ts
448 lines (369 loc) · 12.4 KB
/
ApiHandler.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import { ApiPromise } from '@polkadot/api';
import { BlockHash } from '@polkadot/types/interfaces/chain';
import { EventRecord } from '@polkadot/types/interfaces/system';
import { EventData } from '@polkadot/types/generic/Event';
import { blake2AsU8a } from '@polkadot/util-crypto';
import { u8aToHex } from '@polkadot/util';
import { getSpecTypes } from '@polkadot/types-known';
import { u32 } from '@polkadot/types/primitive';
import { DispatchInfo } from '@polkadot/types/interfaces';
import * as BN from 'bn.js';
interface SanitizedEvent {
method: string;
data: EventData;
}
export default class ApiHandler {
// private wsUrl: string,
private api: ApiPromise;
private specVersion: u32;
private txVersion: u32;
constructor(api: ApiPromise) {
this.api = api;
// this.wsUrl = wsUrl;
this.specVersion = api.createType('u32', -1);
this.txVersion = api.createType('u32', -1);
}
async fetchBlock(hash: BlockHash) {
const api = await this.ensureMeta(hash);
const [{ block }, events] = await Promise.all([
api.rpc.chain.getBlock(hash),
this.fetchEvents(api, hash),
]);
const { parentHash, number, stateRoot, extrinsicsRoot } = block.header;
const logs = block.header.digest.logs.map((log) => {
const { type, index, value } = log;
return { type, index, value };
});
const defaultSuccess = typeof events === 'string' ? events : false;
const extrinsics = block.extrinsics.map((extrinsic) => {
const { method, nonce, signature, signer, isSigned, tip, args } = extrinsic;
const hash = u8aToHex(blake2AsU8a(extrinsic.toU8a(), 256));
return {
method: `${method.sectionName}.${method.methodName}`,
signature: isSigned ? { signature, signer } : null,
nonce,
args,
tip,
hash,
info: {},
events: [] as SanitizedEvent[],
success: defaultSuccess,
// paysFee overrides to bool if `system.ExtrinsicSuccess|ExtrinsicFailed` event is present
paysFee: null as null | boolean,
};
});
const onInitialize = { events: [] as SanitizedEvent[] };
const onFinalize = { events: [] as SanitizedEvent[] };
const successEvent = 'system.ExtrinsicSuccess';
const failureEvent = 'system.ExtrinsicFailed';
if (Array.isArray(events)) {
for (const record of events) {
const { event, phase } = record;
const sanitizedEvent = {
method: `${event.section}.${event.method}`,
data: event.data,
};
if (phase.isApplyExtrinsic) {
const extrinsicIdx = phase.asApplyExtrinsic.toNumber();
const extrinsic = extrinsics[extrinsicIdx];
if (!extrinsic) {
throw new Error(`Missing extrinsic ${extrinsicIdx} in block ${hash}`);
}
const method = `${event.section}.${event.method}`;
if (method === successEvent) {
extrinsic.success = true;
}
if (method === successEvent || method === failureEvent) {
const sanitizedData = event.data.toJSON() as any[];
for (const data of sanitizedData) {
if (data && data.paysFee) {
extrinsic.paysFee =
data.paysFee === true ||
data.paysFee === 'Yes';
break;
}
}
}
extrinsic.events.push(sanitizedEvent);
} else if (phase.isFinalization) {
onFinalize.events.push(sanitizedEvent);
} else if (phase.isInitialization) {
onInitialize.events.push(sanitizedEvent);
}
}
}
const perBill = new BN(1e9);
const perByteFee = api.consts.transactionPayment.transactionByteFee.mul(perBill);
const extrinsicBaseWeight = api.consts.system.extrinsicBaseWeight.mul(perBill);
// Here we assume that `weightToFee` Vec only has 1 element, which is of
// degree 1. FIXME Implement polynomial.
const coefficients = api.consts.transactionPayment.weightToFee[0];
const coeffFrac = coefficients.coeffFrac;
const coeffInt = coefficients.coeffInteger.mul(perBill);
// bn.js values must be < 0x20000000000000, cannot divide by 1e18 [Ref: bn.js:128]
const targetedFeeAdjustment = (await api.query.transactionPayment.nextFeeMultiplier.at(parentHash)).div(perBill); // FIXME This might round the number
// TODO: We assume that the polynomial has one positive term of degree one.
// const degree = coefficients.degree;
// const negative = coefficients.negative;
for (let idx = 0; idx < block.extrinsics.length; ++idx) {
if (!extrinsics[idx].paysFee || !block.extrinsics[idx].isSigned) {
continue;
}
try {
if (api.runtimeVersion.specName.toString() === 'kusama') {
extrinsics[idx].info = await api.rpc.payment.queryInfo(block.extrinsics[idx].toHex(), parentHash);
continue;
}
const xtEvents = extrinsics[idx].events;
const completedEvent = xtEvents.find((event) => event.method === successEvent || event.method === failureEvent);
if (!completedEvent) {
extrinsics[idx].info = {
error: 'Unable to find success or failure event for extrinsic'
};
continue;
}
const completedData = completedEvent.data;
if (!completedData) {
extrinsics[idx].info = {
error: 'Success or failure event for extrinsic does not contain expected data'
};
continue;
}
// both ExtrinsicSuccess and ExtrinsicFailed events have DispatchInfo types as their
// final arg
const weightInfo = completedData[completedData.length - 1] as DispatchInfo;
if (!weightInfo.weight) {
extrinsics[idx].info = {
error: 'Success or failure event for extrinsic does not specify weight'
};
continue;
}
// Note: we use the same variable names as in Rust
const len = block.extrinsics[idx].encodedLength;
const lenFee = perByteFee.muln(len);
// weight_to_fee calculation
// FIXME Implement polynomial.
const weight = weightInfo.weight;
const weightedCoeffFrac = weight.mul(coeffFrac);
const weightedCoeffInt = weight.mul(coeffInt);
const unadjustedWeightFee = weightedCoeffInt.add(weightedCoeffFrac); // In Perbill
const adjustableFee = lenFee.add(unadjustedWeightFee);
const adjustedFee = targetedFeeAdjustment.mul(adjustableFee);
// weight_to_fee calculation
// FIXME Implement polynomial.
const baseFeeFrac = extrinsicBaseWeight.mul(coeffFrac);
const baseFeeInt = extrinsicBaseWeight.mul(coeffInt);
const baseFee = baseFeeInt.add(baseFeeFrac); // In Perbill
let partialFee = baseFee.add(adjustedFee); // In Perbill
extrinsics[idx].info = api.createType('RuntimeDispatchInfo', {
weight,
class: weightInfo.class,
partialFee: partialFee.div(perBill) // We were in Perbill-land, now back to integer
});
} catch (err) {
console.error(err);
extrinsics[idx].info = { error: 'Unable to fetch fee info' };
}
}
return {
number,
hash,
parentHash,
stateRoot,
extrinsicsRoot,
logs,
onInitialize,
extrinsics,
onFinalize,
};
}
async fetchBalance(hash: BlockHash, address: string) {
const api = await this.ensureMeta(hash);
const [header, locks, sysAccount] = await Promise.all([
api.rpc.chain.getHeader(hash),
api.query.balances.locks.at(hash, address),
api.query.system.account.at(hash, address),
]);
const account = sysAccount.data != null
? sysAccount.data :
await api.query.balances.account.at(hash, address);
const at = {
hash,
height: header.number.toNumber().toString(10),
};
if (account && locks && sysAccount) {
const { free, reserved, miscFrozen, feeFrozen } = account;
const { nonce } = sysAccount;
return {
at,
nonce,
free,
reserved,
miscFrozen,
feeFrozen,
locks,
};
} else {
throw {
at,
error: "Account not found",
};
}
}
async fetchStakingLedger(hash: BlockHash, address: string) {
const api = await this.ensureMeta(hash);
const [header, staking] = await Promise.all([
api.rpc.chain.getHeader(hash),
api.query.staking.ledger.at(hash, address),
]);
const at = {
hash,
height: header.number.toNumber().toString(10),
};
return {
at,
staking: staking.isNone ? {} : staking,
}
}
async fetchVesting(hash: BlockHash, address: string) {
const api = await this.ensureMeta(hash);
const [header, vesting] = await Promise.all([
api.rpc.chain.getHeader(hash),
api.query.vesting.vesting.at(hash, address),
]);
const at = {
hash,
height: header.number.toNumber().toString(10),
};
return {
at,
vesting: vesting.isNone ? {} : vesting,
}
}
async fetchMetadata(hash: BlockHash) {
const api = await this.ensureMeta(hash);
const metadata = await api.rpc.state.getMetadata(hash);
return metadata;
}
async fetchTxArtifacts(hash: BlockHash) {
const api = await this.ensureMeta(hash);
const [header, metadata, genesisHash, version] = await Promise.all([
api.rpc.chain.getHeader(hash),
api.rpc.state.getMetadata(hash),
api.rpc.chain.getBlockHash(0),
api.rpc.state.getRuntimeVersion(hash),
]);
const at = {
hash,
height: header.number.toNumber().toString(10),
};
return {
at,
genesisHash,
specVersion: version.specVersion,
txVersion: version.transactionVersion,
metadata: metadata.toHex(),
};
}
async fetchPayoutInfo(hash: BlockHash, address: string) {
const api = await this.ensureMeta(hash);
let [header, rewardDestination, bonded] = await Promise.all([
api.rpc.chain.getHeader(hash),
api.query.staking.payee.at(hash, address),
api.query.staking.bonded.at(hash, address),
]);
const at = {
hash,
height: header.number.toNumber().toString(10),
};
return {
at,
rewardDestination,
bonded,
};
}
async submitTx(extrinsic: string) {
const api = await this.resetMeta();
let tx;
try {
tx = api.tx(extrinsic);
} catch (err) {
throw {
error: 'Failed to parse a tx',
data: extrinsic,
cause: err.toString(),
};
}
try {
const hash = await api.rpc.author.submitExtrinsic(tx);
return {
hash,
};
} catch (err) {
throw {
error: 'Failed to submit a tx',
cause: err.toString(),
}
}
}
private async fetchEvents(api: ApiPromise, hash: BlockHash): Promise<EventRecord[] | string> {
try {
return await api.query.system.events.at(hash);
} catch (_) {
return 'Unable to fetch Events, cannot confirm extrinsic status. Check pruning settings on the node.';
}
}
private async resetMeta(): Promise<ApiPromise> {
const { api } = this;
return await this.ensureMeta(await api.rpc.chain.getFinalizedHead());
}
private async ensureMeta(hash: BlockHash): Promise<ApiPromise> {
const { api } = this;
try {
const version = await api.rpc.state.getRuntimeVersion(hash);
const blockSpecVersion = version.specVersion;
const blockTxVersion = version.transactionVersion;
// Swap metadata if tx version is different. This block of code should never execute, as
// specVersion always increases when txVersion increases, but specVersion may increase
// without an increase in txVersion. Defensive only.
if (!this.txVersion.eq(blockTxVersion) && this.specVersion.eq(blockSpecVersion)) {
console.warn('txVersion bumped without specVersion bumping');
this.txVersion = blockTxVersion;
const meta = await api.rpc.state.getMetadata(hash);
api.registry.setMetadata(meta);
}
// Swap metadata and confirm txVersion if specVersion is different.
else if (!this.specVersion.eq(blockSpecVersion)) {
this.specVersion = blockSpecVersion;
this.txVersion = blockTxVersion;
const meta = await api.rpc.state.getMetadata(hash);
const chain = await api.rpc.system.chain();
api.registry.register(
getSpecTypes(api.registry, chain, version.specName, blockSpecVersion)
);
api.registry.setMetadata(meta);
}
} catch (err) {
console.error(`Failed to get Metadata for block ${hash}, using latest.`);
console.error(err);
this.specVersion = api.createType('u32', -1);
this.txVersion = api.createType('u32', -1);
}
return api;
}
}