-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
163 lines (143 loc) · 4.63 KB
/
index.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
const Web3 = require('web3');
const express = require('express')
const app = express()
const level = require('level')
const util = require('ethereumjs-util')
const db = level('./db')
const list = level('./queue')
const Tx = require('ethereumjs-tx')
const cors = require('cors')
//process.argv[2] = ""
// const url = 'wss://kovan.infura.io/ws '
const KOVAN_WSS = "wss://kovan.swarm.city"; // Swarm City Chain
// const url = "ws://178.128.207.83:8546"; // Swarm City Chain
// const url = 'ws://my.kovan.dnp.dappnode.eth:8546'
let provider = new Web3.providers.WebsocketProvider(KOVAN_WSS);
web3 = new Web3(provider);
provider.on('error', e => console.log('WS Error', e));
provider.on('end', e => {
console.log('WSS closed');
console.log('Attempting to reconnect...');
provider = new Web3.providers.WebsocketProvider(KOVAN_WSS);
provider.on('connect', function () {
console.log('WSS Reconnected');
});
web3.setProvider(provider);
});
setInterval(() => { web3.eth.getBlockNumber().then(console.log) }, 30 * 1000);
const asyncMiddleware = fn =>
(req, res, next) => {
Promise.resolve(fn(req, res, next))
.catch(next);
};
app.use(cors({
origin: '*'
}));
app.listen(33333)
async function iterateQueue () {
console.log('Going through queue')
var stream = list
.createReadStream({
keys: true,
values: true
})
.on("data", asyncMiddleware(async item => {
console.log("item:", item);
var isItemAddress = await isAddress(item.key)
if(!isItemAddress) {
console.log(item.key, " is not a valid address.")
list.del(item.key)
stream.destroy()
return
}
var isItemKnown = await makeKnown(item.key)
if(!isItemKnown) {
console.log(item.key, " something wrong storing or retrieving")
list.del(item.key)
stream.destroy()
return
}
var isItemValid = await checkValidity(item.key)
if(isItemValid) {
console.log('item can get money now')
var result = await doTransfer(item.key)
console.log(result)
list.del(item.key)
console.log("Removed item ", item.key, " from list")
stream.destroy();
} else {
console.log('item is not valid')
}
}))
}
var q = []
app.get('/:address', asyncMiddleware(async (req, res, next) => {
console.log('Request: ', req.params.address)
var address = req.params.address
list.put(address, Date.now())
res.status(200).json({ 'status': 'queued'})
}));
async function doTransfer(address) {
db.put(address, Date.now())
//return 'transfer'
var private = Buffer.from(process.argv[2], 'hex')
var senderBuffer = await util.privateToAddress('0x'+process.argv[2])
var sender = util.bufferToHex(senderBuffer)
var value = web3.utils.toHex('200000')
var gasPrice = await web3.eth.getGasPrice()
var nonceTo = await web3.eth.getTransactionCount(address)
var nonceSender = await web3.eth.getTransactionCount(sender)
var rawTransaction = {
"nonce": nonceSender++,
"from": sender,
"to": address,
"gasLimit": 314150,
"gasPrice": 2400000000,
"value": 50000000000000000,
"chainId": 42
};
var tx = new Tx(rawTransaction)
tx.sign(private);
var raw = '0x' + tx.serialize().toString('hex');
var status = await web3.eth.sendSignedTransaction(raw)
console.log('https://kovan.etherscan.io/tx/'+status.transactionHash)
console.log('\n')
db.put(address, Date.now())
return ('https://kovan.etherscan.io/tx/'+status.transactionHash)
}
async function getBalance(address) {
var balance = await web3.eth.getBalance(address)
return balance
}
async function isAddress(address) {
return await web3.utils.isAddress(address)
}
async function makeKnown(address) {
try {
await db.get(address);
return true
} catch (error) {
//return response.status(400).send(error);
var res = await db.put(address, Date.now()-10000)
return true
}
}
async function checkValidity(address) {
console.log('checking validity for ', address)
var balance = await getBalance(address)
if(balance > 500000000000000000) throw Error('Balance sufficient')
if(await db.get(address) < (Date.now() - 10000)) {
return true
} else {
console.log("Too soon for ", address)
return false
}
}
async function runFaucet() {
console.log("Swarm City Faucet")
// queue monitor
setInterval(() => {
iterateQueue();
}, 10 * 1000);
}
runFaucet()