Newer
Older
// 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/>.
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
import { ApiPromise } from '@polkadot/api';
import { BlockHash } from '@polkadot/types/interfaces/rpc';
export default class ApiHandler {
private api: ApiPromise;
constructor(api: ApiPromise) {
this.api = api;
}
async fetchBlock(hash: BlockHash) {
const { api } = this;
const { block } = await api.rpc.chain.getBlock(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 extrinsics = block.extrinsics.map((extrinsic) => {
const { method, nonce, signature, signer, isSigned, args } = extrinsic;
return {
method: `${method.sectionName}.${method.methodName}`,
signature: isSigned ? { signature, signer } : null,
nonce,
args,
};
});
return {
number,
hash,
parentHash,
stateRoot,
extrinsicsRoot,
logs,
extrinsics,
};
}
async fetchBalance(hash: BlockHash, address: string) {
const { api } = this;
const [header, free, reserved, locks] = await Promise.all([
api.rpc.chain.getHeader(hash),
api.query.balances.freeBalance.at(hash, address),
api.query.balances.reservedBalance.at(hash, address),
api.query.balances.locks.at(hash, address),
]);
const at = {
hash,
height: header.number,
};
return { at, free, reserved, locks };
}
}