-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcfBakerRegistry.js
182 lines (159 loc) · 7.17 KB
/
tcfBakerRegistry.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
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
var encoder = new TextEncoder();
var decoder = new TextDecoder();
let bakerWithNoReporterText = "secondary key not specified";
function genPaymentConfigStruct(paymentConfigMask) {
return {
payForOwnBlocks: (paymentConfigMask & 1) > 0,
payForEndorsements: (paymentConfigMask & 2) > 0,
payGainedFees: (paymentConfigMask & 4) > 0,
payForAccusationGains: (paymentConfigMask & 8) > 0,
subtractLostDepositsWhenAccused: (paymentConfigMask & 16) > 0,
subtractLostRewardsWhenAccused: (paymentConfigMask & 32) > 0,
subtractLostFeesWhenAccused: (paymentConfigMask & 64) > 0,
payForRevelation: (paymentConfigMask & 128) > 0,
subtractLostRewardsWhenMissRevelation: (paymentConfigMask & 256) > 0,
subtractLostFeesWhenMissRevelation: (paymentConfigMask & 512) > 0,
compensateMissedBlocks: !((paymentConfigMask & 1024) > 0),
payForStolenBlocks: (paymentConfigMask & 2048) > 0,
compensateMissedEndorsements: !((paymentConfigMask & 4096) > 0),
compensateLowPriorityEndorsementLoss: !((paymentConfigMask & 8192)) > 0,
};
}
function bufferToHex (buffer) {
return Array
.from (new Uint8Array (buffer))
.map (b => b.toString (16).padStart (2, "0"))
.join ("");
}
function hexToArray (hex) {
return new Uint8Array(hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
}
/*
Current working public default for indexerProvider: https://api.staging.tzstats.com
Current registry contractAddress: KT1ChNsEFxwyCbJyWGSL3KdjeXE28AY1Kaog
Current registry contract bigMapId: 17
*/
function getRegistryBaker(bakerAccount, indexerProvider, bigMapId) {
return new Promise(function (resolve, reject) {
fetch(indexerProvider + '/explorer/bigmap/' + bigMapId + '/' + bakerAccount + '?prim=false').then(function(res){
if (!res.ok) {
if (res.status == 400 || res.status == 404) {
resolve({});
} else {
throw new Error("HTTP error, status = " + res.status);
}
} else {
res.json().then(function(data) {
baker = parseBakerData(data);
resolve (baker);
}).catch(function(e){
reject(e);
});
}
}).catch(function(e){
reject(e);
});
});
}
//returns the bigmap id, signup fee, update fee values from the registry contract storage
function getRegistryContractConfig(indexerProvider, contractAddress) {
return new Promise(function(resolve, reject) {
fetch(indexerProvider + '/explorer/contract/' + contractAddress +'/storage?prim=false').then(function(res){
if (!res.ok) {
throw new Error("HTTP error, status = " + res.status);
}
res.json().then(function(data) {
resolve (parseContractConfigData(data));
}).catch(function(e){
reject(e);
});
}).catch(function(e){
reject(e);
});
});
}
/*
Current working public default for indexerProvider: https://api.staging.tzstats.com
Current registry contractAddress: KT1ChNsEFxwyCbJyWGSL3KdjeXE28AY1Kaog
Current registry contract bigMapId: 17
*/
function getAllRegistryBakers(indexerProvider, bigMapId) {
return new Promise(function(resolve, reject) {
fetch(indexerProvider + '/explorer/bigmap/' + bigMapId + '/values?limit=100').then(function(res){
if (!res.ok) {
throw new Error("HTTP error, status = " + res.status);
}
res.json().then(function(data) {
fetch(indexerProvider + '/explorer/bigmap/' + bigMapId + '/values?limit=100&offset=100').then(function(res){
if (!res.ok) {
throw new Error("HTTP error, status = " + res.status);
}
res.json().then(function(offsetdata) {
// reportersMap = new Map();
bakersList = [];
var count = -1;
var combinedData = data.concat(offsetdata);
combinedData.forEach(function(bakerData) {
count += 1;
var baker = parseBakerData(bakerData);
bakersList.push(baker);
});
resolve(bakersList);
}).catch(function(e){
reject(e);
});
}).catch(function(e){
reject(e);
});
}).catch(function(e){
reject(e);
});
}).catch(function(e){
reject(e);
});
});
}
function parseBakerData(bakerData) {
var baker = {
"bakerName": bakerData.value.data.bakerName,
"openForDelegation": bakerData.value.data.openForDelegation,
"bakerAccount": bakerData.key,
"reporterAccount": bakerData.value.reporterAccount,
"bakerOffchainRegistryUrl": bakerData.value.data.bakerOffchainRegistryUrl,
"bakerRegistryLastUpdated": bakerData.value.last_update
}
baker.paymentModel = {
"split": bakerData.value.data.split / 10000,
"bakerPaysFromAccounts": bakerData.value.data.bakerPaysFromAccounts,
"minDelegation": bakerData.value.data.minDelegation / 10000,
"subtractPayoutsLessThanMin": bakerData.value.data.subtractPayoutsLessThanMin,
"payoutDelay": bakerData.value.data.payoutDelay * 1, //convert the json string to an int
"payoutFrequency": bakerData.value.data.payoutFrequency * 1, //convert the json string to an int
"minPayout": bakerData.value.data.minPayout / 10000,
"bakerChargesTransactionFee": bakerData.value.data.bakerChargesTransactionFee,
"paymentConfigMask": bakerData.value.data.paymentConfigMask,
"overDelegationThreshold": bakerData.value.data.overDelegationThreshold,
"subtractRewardsFromUninvitedDelegation": bakerData.value.data.subtractRewardsFromUninvitedDelegation
}
baker.bakerName = decoder.decode(hexToArray(baker.bakerName));
if (baker.bakerOffchainRegistryUrl != '') {
baker.bakerOffchainRegistryUrl = decoder.decode(hexToArray(baker.bakerOffchainRegistryUrl));
}
if (baker.reporterAccount == null) {
baker.reporterAccount = bakerWithNoReporterText;
baker.reporterAccountFromStorageWasEmpty = true; //todo: only needed because combining the contract calls to "updateData" and "updateReporter", in which case when modify the reporter model need to differentiate if its the first time reporter added
} else {
baker.reporterAccountFromStorageWasEmpty = false;
baker.reporterAccountFromStorage = baker.reporterAccount;
}
baker.paymentModel.paymentConfig = genPaymentConfigStruct(baker.paymentModel.paymentConfigMask); // baking bad compatible struct;
return baker;
}
function parseContractConfigData (data) {
return {
'signup_fee': data.value.signup_fee,
'update_fee': data.value.update_fee,
'bigMapId': data.value['0@big_map'], //17 bigmapID on main, 538 on babylonnet -- this syntax needed when property is not annotated in contract
'owner': data.value.owner
};
}