This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
pool-proxy.js
125 lines (89 loc) · 2.56 KB
/
pool-proxy.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
const net = require('net');
const BN = require('bignumber.js');
const config = require('./config');
const rpc = require('./lib/rpc');
const calculatePayout = require('./lib/calculate-payout');
const ipayouts = require('./lib/ipayouts');
const poolHost = 'us1-zcash.flypool.org';
const poolPort = 3333;
net.createServer(client => {
const socket = net.connect(poolPort, poolHost);
const disconnectOnError = f => async (...args) => {
try {
return await f(...args);
} catch (error) {
console.log(error);
socket.end();
client.end();
}
};
let sendBuffer = '';
let recvBuffer = '';
let address;
let target;
let nextTarget;
const submits = [];
client.on('data', disconnectOnError(data => {
sendBuffer += data;
const messages = sendBuffer.split('\n');
sendBuffer = messages.pop();
for (const rawMessage of messages) {
const message = JSON.parse(rawMessage);
console.log('->', message);
if (message.method === 'mining.submit')
submits.push(message.id);
if (message.method === 'mining.authorize') {
let mainAddress;
[mainAddress, address] = message.params[0].split('.');
console.log('address', address);
if (mainAddress !== 't1YtcRXgoDsVj6sDhGA71sgdDLoR9Q1QcnL')
throw new Error('Please mine on zFaucet\'s address');
}
}
socket.write(data);
}));
socket.on('data', disconnectOnError(async data => {
recvBuffer += data;
const messages = recvBuffer.split('\n');
recvBuffer = messages.pop();
for (const rawMessage of messages) {
const message = JSON.parse(rawMessage);
console.log('<-', message);
if (message.method === 'mining.set_target')
nextTarget = new BN(message.params[0], 16);
if (message.method === 'mining.notify')
target = nextTarget;
if (submits.indexOf(message.id) > -1) {
if (message.error)
throw new Error(message.error);
if (!(target instanceof BN))
throw new Error('Target not set');
console.log('Payout due!');
const networkDifficulty = await rpc.getdifficulty();
console.log('Found difficulty:', networkDifficulty);
const total = Number(calculatePayout(networkDifficulty, target));
console.log('Total:', total);
const amount = total * (1 - config.ipayoutFee);
console.log('Amount:', amount);
await ipayouts.insert({
address,
amount,
processed: false
});
}
}
client.write(data);
}));
client.on('end', () => {
socket.end();
});
socket.on('end', () => {
client.end();
});
client.on('error', () => {
socket.end();
});
socket.on('error', () => {
client.end();
});
}).listen(3333);