-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (100 loc) · 3.5 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
const express = require('express');
const app = express();
const port = 4000;
const cors = require('cors');
app.use(cors());
const title = 'FIN4Xplorer Demo Faucet Server';
const HDWalletProvider = require('@truffle/hdwallet-provider');
const Tx = require('ethereumjs-tx'); // <-- for version 1.3.7, for version ^2.1.1 add .Transaction
const Web3 = require('web3');
const config = require('./config.json');
const dripAmount = 0.1; // unit: ether
const networkURL = 'https://rinkeby.infura.io/v3/' + config.INFURA_API_KEY;
const provider = new HDWalletProvider(config.MNEMONIC, networkURL);
const web3 = new Web3(provider);
const address = web3.currentProvider.addresses[0];
app.listen(port, () => console.log(title + ' listening on port ' + port));
app.get('/', (req, res) => res.send(title));
app.get('/faucet', (request, response) => {
console.log('Received funding request: ', request.query);
checkUsersBalance(request.query.recipient, response, () => {
// TODO also limit total/timeframed amount of requests per user? Or is total amount enough #ConceptualDecision
sendEther(request.query.recipient, dripAmount.toString(), request.query.networkID.toString(), networkURL, response);
});
});
let checkUsersBalance = async function(recipient, response, callback) {
console.log('Checking ETH balance of user ' + recipient);
web3.eth.getBalance(recipient, (err, res) => {
if (err) {
let report = 'Failed to check users balance, not sending Ether.';
console.log(report);
response.send(report);
return;
}
let eth = web3.utils.fromWei(res, 'ether');
if (eth >= 1) {
let report = 'The user has already more than 1 ETH (' + eth + '), the faucet is not sending Ether.';
console.log(report);
response.send(report);
return;
}
console.log('User has ' + eth + ' ETH');
callback();
});
};
let sendEther = async function(recipient, amount, networkID, networkURL, response) {
console.log(
'Attempting to send ' +
amount +
' ETH from ' +
address +
' to ' +
recipient +
', network: ' +
networkID +
' ' +
networkURL
);
// TODO derive private key from mnemonic via bip39... if possible?
let privateKey = Buffer.from(config.PRIVATE_KEY_OF_FAUCET_ACCOUNT, 'hex');
web3.eth.getGasPrice(function(e, gasPrice) {
console.log('Got gas price: ' + gasPrice);
web3.eth.getTransactionCount(address).then(count => {
console.log('Transaction count: ' + count);
const rawTransaction = {
from: address,
// gasLimit: web3.utils.toHex(210000),
gas: web3.utils.toHex(100000), // 21000,
gasPrice: web3.utils.toHex(gasPrice * 2), // is * 2 a reasonable factor??
to: recipient,
value: web3.utils.toHex(web3.utils.toWei(amount, 'ether')), //'0x0',
chainId: web3.utils.toHex(networkID),
// data: data,
nonce: web3.utils.toHex(count)
};
var tx = new Tx(rawTransaction);
tx.sign(privateKey);
console.log('Transaction is signed');
web3.eth.sendSignedTransaction('0x' + tx.serialize().toString('hex')).on('receipt', receipt => {
let report = 'Faucet sent ' + amount + ' ETH to ' + recipient; // + ' from ' + address;
console.log(report);
response.send(report);
// process.exit(0);
});
});
});
};
/*
const getNetworkURL = networkID => {
switch (networkID) {
case '3':
return 'https://ropsten.infura.io/v3/' + config.INFURA_API_KEY;
case '4':
return 'https://rinkeby.infura.io/v3/' + config.INFURA_API_KEY;
case '5':
return 'https://goerli.infura.io/v3/' + config.INFURA_API_KEY;
case '5777':
return 'http://127.0.0.1:7545';
}
};
*/