-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrpc_collector.js
77 lines (75 loc) · 2.11 KB
/
rpc_collector.js
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
import JSBI from "jsbi";
import { Reader } from "../reader";
import { HexStringToBigInt, BigIntToHexString } from "../rpc";
export class RPCCollector {
constructor(
rpc,
lockHash,
{
skipCellWithContent = true,
loadData = false,
loadBlockNumber = true
} = {}
) {
this.rpc = rpc;
this.lockHash = new Reader(lockHash).serializeJson();
this.skipCellWithContent = skipCellWithContent;
this.loadData = loadData;
this.loadBlockNumber = loadBlockNumber;
}
async *collect() {
const to = HexStringToBigInt(await this.rpc.get_tip_block_number());
let currentFrom = JSBI.BigInt(0);
while (JSBI.lessThanOrEqual(currentFrom, to)) {
let currentTo = JSBI.add(currentFrom, JSBI.BigInt(100));
if (JSBI.greaterThan(currentTo, to)) {
currentTo = to;
}
const cells = await this.rpc.get_cells_by_lock_hash(
this.lockHash,
BigIntToHexString(currentFrom),
BigIntToHexString(currentTo)
);
for (const cell of cells) {
if (this.skipCellWithContent) {
if (
cell.type ||
JSBI.greaterThan(
HexStringToBigInt(cell.output_data_len),
JSBI.BigInt(100)
)
) {
continue;
}
}
let data = null;
if (this.loadData) {
const cellWithData = await this.rpc.get_live_cell(
cell.out_point,
true
);
data = cellWithData.cell.data.content;
}
let block_number = null;
if (this.loadBlockNumber) {
const header = await this.rpc.get_header(cell.block_hash);
block_number = header.number;
}
yield {
cellbase: cell.cellbase,
cell_output: {
capacity: cell.capacity,
lock: cell.lock,
type: cell.type
},
out_point: cell.out_point,
block_hash: cell.block_hash,
data: data,
output_data_len: cell.output_data_len,
block_number
};
}
currentFrom = JSBI.add(currentTo, JSBI.BigInt(1));
}
}
}