-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (78 loc) · 3 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
const http = require('http');
const url = require('url');
const Web3 = require('web3');
const mu = require('mu2');
const config = require('./config');
// globals
let web3 = null;
// wrap the init stuff into async main in order to have await available
async function start() {
if(isNaN(config.amount)) {
console.error('ERR: no valid amount configured in "faucet.amount"');
process.exit(1);
}
web3 = new Web3(new Web3.providers.HttpProvider(config.network.rpc));
http.createServer(handleRequest).listen(config.network.port, config.network.interface);
console.log(`http server listening at interface ${config.network.interface} port ${config.network.port}`);
}
function handleRequest(req, res) {
var pathname = url.parse(req.url).pathname;
console.log(`request for ${pathname}`);
res.setHeader('Access-Control-Allow-Origin', '*');
// check if it's a path we care about
var splitPath = url.parse(req.url).path.split('/');
if(splitPath[1].startsWith('0x')) {
var userAddr = splitPath[1];
if (!web3.utils.isAddress(userAddr)) {
res.writeHead(401, {'Content-Type': 'text/plain'});
res.end(`not a valid address: ${userAddr}\n`);
return;
}
console.log(`processing for ${userAddr}`);
refuelAccount(userAddr, (err, txHash) => {
// this is an ugly workaround needed because web3 may throw an error after giving us a txHash
if (res.finished) return;
if (err) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end(`${err}\n`);
}
if (txHash) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(`txHash: ${txHash}\n`);
}
});
} else {
var stream = mu.compileAndRender(config.indexHtml, {});
stream.pipe(res);
//res.end('Please enter your address starting with 0x: <input type="text" />');
}
}
// sends some coins to the given account <userAddr>, invokes the given callback with the resulting transaction hash
async function refuelAccount(userAddr, callback) {
console.log(`sending ${config.amount} ATS to ${userAddr}...`);
const txObj = {
from: config.account.address,
to: userAddr,
value: web3.utils.toWei(config.amount.toString()),
gas: config.gas,
gasPrice: config.gasPrice
};
const signedTxObj = await web3.eth.accounts.signTransaction(txObj, config.account.privateKey);
web3.eth.sendSignedTransaction(signedTxObj.rawTransaction)
.once('transactionHash', function (txHash) {
console.log(`waiting for processing of token transfer transaction ${txHash}`);
callback(null, txHash);
})
.once('receipt', function (receipt) {
if (! receipt.status) {
console.error(`transfer transaction ${receipt.transactionHash} failed`);
} else {
console.log(`transfer transaction ${receipt.transactionHash} executed in block ${receipt.blockNumber} consuming ${receipt.gasUsed} gas`);
}
})
.on('error', function (err) {
console.error(`transfer transaction failed: ${err}`);
callback(err, null);
});
}
start();