-
Notifications
You must be signed in to change notification settings - Fork 1
/
MonitoredData.ts
154 lines (110 loc) · 5.19 KB
/
MonitoredData.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
import { Messaging } from "./Messaging";
import { ProxyMessage } from "./ProxyMessage";
import { Nomination, PendingNomination, Nominee } from "./Types";
import { NominationMessage } from "./NominationMessage";
import { Settings } from "./Settings";
import { Utility } from "./Utility";
export class MonitoredData {
private static instance: MonitoredData;
private era: number;
private proxy_info: PendingNomination[];
private nomination_info: Nomination[];
private constructor() {
this.proxy_info = [];
this.nomination_info = [];
this.era = -1;
}
public static getInstance(): MonitoredData {
if (!MonitoredData.instance) {
MonitoredData.instance = new MonitoredData();
}
return MonitoredData.instance;
}
public setEra(era: number) {
this.era = era;
}
public getEra() {
return this.era;
}
public async addProxyCall(proxy_entry: PendingNomination) {
var proxy_message: ProxyMessage = new ProxyMessage(proxy_entry);
var previous_entry = this.proxy_info.find(nom => nom.nominator == proxy_entry.nominator);
//console.log(proxy_entry);
if (previous_entry == undefined) {
proxy_message.generateNewString().then(msg => {
Messaging.sendMessage(msg);
});
} else {
proxy_message.generateDuplicateString(previous_entry.proxy_info.targets).then(msg => {
Messaging.sendMessage(msg);
});
this.removeProxyCall(previous_entry.nominator);
};
this.proxy_info.push(proxy_entry);
}
public hasProxyCall(block_number: number): PendingNomination | undefined {
var result = this.proxy_info.find(entry => (entry.proxy_info.number + Settings.proxy_delay_blocks + 100) == block_number);
//var result = this.proxy_info[0];
return result;
}
public getNominations(controller: string): Nomination {
var tvp_account = Settings.tvp_nominators.find(item => item.controller == controller);
var result = <Nomination>{};
if (tvp_account != undefined) {
var find_nom = this.nomination_info.find(entry => entry.nominator == tvp_account?.stash);
if (find_nom != undefined) {
return find_nom;
}
}
return result;
}
public removeProxyCall(controller: string) {
this.proxy_info = this.proxy_info.filter(entry => entry.nominator != controller);
}
public addNomination(nomination: Nomination) {
if (this.nomination_info.length == 0) {
this.nomination_info.push(nomination);
var nomination_message = new NominationMessage(nomination);
nomination_message.generateString().then(msg => {
Messaging.sendMessage(msg);
});
} else {
var previous_nominee_index = this.nomination_info.findIndex(item => item.nominator == nomination.nominator);
if (previous_nominee_index == -1) {
this.nomination_info.push(nomination);
var nomination_message = new NominationMessage(nomination);
nomination_message.generateString().then(msg => {
Messaging.sendMessage(msg);
});
} else {
//Validators
var differences: Nominee[] = [];
var similar: Nominee[] = [];
nomination.nominees.forEach(nominee => {
let found = false;
//Looks for similiarities and adds one to the count if this is the case
this.nomination_info[previous_nominee_index].nominees.forEach(previous_nominee => {
if (previous_nominee.val_address == nominee.val_address) {
similar.push(<Nominee>{ val_address: previous_nominee.val_address, nomination_count: previous_nominee.nomination_count + 1, score: Utility.getScore(Utility.tvp_candidates, previous_nominee.val_address) });
found = true;
}
});
//If there are no similarities
if (!found) {
differences.push(<Nominee>{ val_address: nominee.val_address, nomination_count: 1, score: Utility.getScore(Utility.tvp_candidates, nominee.val_address) });
}
});
var previous_entry = this.nomination_info[previous_nominee_index];
this.nomination_info[previous_nominee_index] = <Nomination>{
era: nomination.era,
nominator: nomination.nominator,
nominees: differences.concat(similar)
};
var nomination_message = new NominationMessage(this.nomination_info[previous_nominee_index]);
nomination_message.generateDuplicateString(previous_entry, differences).then(msg => {
Messaging.sendMessage(msg);
});
}
}
}
}