-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
133 lines (130 loc) · 3.09 KB
/
utils.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
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
const _ = require('lodash')
function convert(filter) {
const topics =filter.topics ? new Array(filter.topics.length).fill(null).map((t,i)=>{
return filter.topics[i] ?? t
}) : []
let result = {
...topics,
}
if(filter.address!=null){
result['address'] = filter.address
}
if (filter.fromBlock != null) {
result['fromBlock'] = filter.fromBlock
}
if (filter.fromBlock != null) {
result['toBlock'] = filter.toBlock
}
return result
}
function split(filter) {
for (const key of Object.keys(filter)) {
const values = _.get(filter, key)
if (_.isArray(values)) {
return _.flatten(
values.map((value) => {
const f = JSON.parse(JSON.stringify(filter))
_.set(f, key, value)
return split(f)
})
)
}
}
return filter
}
function explode(s) {
let topics = { ...s };
delete topics['address'];
delete topics['fromBlock'];
delete topics['toBlock'];
topics = Object.values(topics);
let filter = {};
if (s['address'] != null) {
filter['address'] = s['address'];
}
if (topics.length) {
filter['topics'] = topics;
}
if (s['fromBlock'] != null) {
filter['fromBlock'] = s['fromBlock'];
}
if (s['toBlock'] != null) {
filter['toBlock'] = s['toBlock'];
}
return filter;
}
function compareLog(a, b) {
if (a.blockNumber < b.blockNumber) {
return -2
} else if (a.blockNumber > b.blockNumber) {
return 2
}
if (a.logIndex < b.logIndex) {
return -1
} else if (a.logIndex > b.logIndex) {
return 1
}
return 0
}
function mergeTwoUniqSortedLogs(a, b) {
if (!a?.length) {
return b ?? []
}
if (!b?.length) {
return a ?? []
}
const r = []
const i = {
a: 0,
b: 0
}
while (i.a < a.length || i.b < b.length) {
if (a[i.a] == null) {
r.push(b[i.b++])
continue
}
if (b[i.b] == null) {
r.push(a[i.a++])
continue
}
const c = compareLog(a[i.a], b[i.b])
if (c < 0) {
r.push(a[i.a++])
continue
}
if (c == 0) {
i.a++
}
r.push(b[i.b++])
}
return r;
}
/**
* Generate a valid filter list with scan api. The sum of the logs of the filters is equal to the logs of the filter
* @param {*} filter
* @returns
*/
const translateFilter = (filter)=>{
// remove trailing null topics
while(filter?.topics?.length && filter.topics[filter.topics.length-1] == null) {
filter.topics.pop()
}
let filters = split(convert(filter));
if (Array.isArray(filters)) {
filters = filters.map((e) => explode(e));
} else {
filters = [explode(filters)];
}
return filters
}
const isOrMode = (topics) => {
return topics?.some(topic => topic != null && topic.length > 0 && topic[0] == null)
}
module.exports = {
convert,
split,
explode,
mergeTwoUniqSortedLogs,
translateFilter,
isOrMode,
}