-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis-nominated.js
63 lines (52 loc) · 2.38 KB
/
is-nominated.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
module.exports = function (RED) {
const axios = require('axios');
function NodeConstructor(config) {
this.ewxConfig = RED.nodes.getNode(config.ewxConfig);
RED.nodes.createNode(this, config);
var node = this;
node.on('input', function (msg, send, done) {
const requestPayload = {
query: `query IsWorkerNominated($workerAddress: String!, $solutionNamespace: String!) {
nominatedWorkersMappings(where:{ worker: {id_eq: $workerAddress}, solution:{ id_eq: $solutionNamespace}}) {
id
}
}`,
variables: {
workerAddress: node.ewxConfig.workerAddress,
solutionNamespace: node.ewxConfig.solutionNamespace
}
};
axios.post(node.ewxConfig.subsquidUrl, requestPayload)
.then((response) => {
const {nominatedWorkersMappings} = response.data.data;
if (nominatedWorkersMappings.length > 0) {
this.status({fill: "green", shape: "dot", text: "nominated"});
this.log(`workerAddress = ${node.ewxConfig.workerAddress} - worker is nominated`);
send({
payload: {
isNominated: true,
}
});
} else {
this.status({fill: "yellow", shape: "dot", text: "not nominated"});
this.log(`workerAddress = ${node.ewxConfig.workerAddress} - worker is not nominated`);
send({
payload: {
isNominated: false,
}
});
}
})
.catch((e) => {
console.error(`failed to obtain nominations for solution = ${node.ewxConfig.solutionNamespace}`, e, e.response?.data);
this.status({fill: "red", shape: "ring", text: "failed to obtain data"});
send({
payload: {
isNominated: null,
}
});
})
});
}
RED.nodes.registerType("is-nominated", NodeConstructor);
}