From 99981d0906f118437e8d40065f0334df8f4f8943 Mon Sep 17 00:00:00 2001 From: Bruno Skvorc Date: Wed, 31 Jul 2019 11:34:16 +0200 Subject: [PATCH] Parametrized more options, fixes #4 --- .env.example | 7 ++++- README.md | 2 +- deploy/faucet/config.json.example | 9 +++--- deploy/faucet/index.js | 2 +- deploy/faucet/package.json | 2 +- deploy/faucet/public/views/index.pug | 2 +- start.js | 42 ++++++++++++++++++++++------ 7 files changed, 48 insertions(+), 18 deletions(-) diff --git a/.env.example b/.env.example index a6e28ab..233dc81 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,6 @@ -MNEMONIC=main blouse fashion brand own rocket fluid notable vacuum gain guitar leaf \ No newline at end of file +MNEMONIC=main blouse fashion brand own rocket fluid notable vacuum gain guitar leaf +PORT=8545 +FAUCET_PORT=5001 +# Amount is in Ether +FAUCET_AMOUNT=1000000 +NETWORK_ID=666 \ No newline at end of file diff --git a/README.md b/README.md index 7cd8d14..6f23b64 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ You will have to pollute your system a little for this to work. Luckily, it work - Install NVM to have a working NodeJS setup. If you don't, nodeJS and npm will probably break in many, many ways. Once you have NVM, set it to Node version 10. - Install the Yarn package manager: https://yarnpkg.com/lang/en/docs/install/ -- Run `yarn install` to install dependencies +- Run `yarn install && cd deploy/faucet && yarn install` to install dependencies ### Start diff --git a/deploy/faucet/config.json.example b/deploy/faucet/config.json.example index 49eaf25..df59511 100644 --- a/deploy/faucet/config.json.example +++ b/deploy/faucet/config.json.example @@ -1,6 +1,7 @@ { "environment": "prod", "debug": false, + "port": "FAUCET_PORT", "Captcha": { "secret": "", "sitekey": "" @@ -8,7 +9,7 @@ "Tweeter": { "similarityTreshold": 0.75, "cooldown": 8, - "predefinedTweet": "I'm using the Thundercloud (https://github.com/swader/thundercloud) Faucet to get some Test Ether to launch an Ethereum 2.0 Genesis event", + "predefinedTweet": "I'm using the Thundercloud (https://github.com/status-im/thundercloud) Faucet to get some Test Ether to launch an Ethereum 2.0 Genesis event", "predefinedHashTags": "ethereum, eth2, serenity" }, "Ethereum": { @@ -16,9 +17,9 @@ "milliEtherToTransferWithoutTweet": 1000, "gasLimit": "21000", "prod": { - "rpc": "http://127.0.0.1:8545", - "account": "0x471e0575bFC76d7e189ab3354E0ecb70FCbf3E46", - "privateKey": "2960a712dcbcd755be598955b08ee0f697b372aed31005bd55fd02949b6917bb" + "rpc": "http://127.0.0.1:PORT", + "account": "ADDRESS", + "privateKey": "PRIVATE_KEY_WITHOUT_0x" } } } diff --git a/deploy/faucet/index.js b/deploy/faucet/index.js index d68c1bb..cbbaeb5 100644 --- a/deploy/faucet/index.js +++ b/deploy/faucet/index.js @@ -46,7 +46,7 @@ app.configureWeb3(config) }); }); - app.set('port', (process.env.PORT || 5000)); + app.set('port', (process.env.PORT || app.config.port || 5000)); app.listen(app.get('port'), function () { console.log('Thundercloud faucet is running on port', app.get('port')); diff --git a/deploy/faucet/package.json b/deploy/faucet/package.json index a300af9..8bf9c5f 100644 --- a/deploy/faucet/package.json +++ b/deploy/faucet/package.json @@ -11,7 +11,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/swader/thundercloud" + "url": "https://github.com/status-im/thundercloud" }, "author": "swader", "license": "MIT", diff --git a/deploy/faucet/public/views/index.pug b/deploy/faucet/public/views/index.pug index 3168872..4243281 100644 --- a/deploy/faucet/public/views/index.pug +++ b/deploy/faucet/public/views/index.pug @@ -24,7 +24,7 @@ html .inner h1.hidden Thundercloud Testnet Faucet .allign-middle - a(href='https://github.com/swader/thundercloud') + a(href='https://github.com/status-im/thundercloud') img.center-logo-img(src='assets/images/cloud.png' alt='Thundercloud Logo') main.inner.content(role='main') diff --git a/start.js b/start.js index 0bd4abf..0ad4c3c 100644 --- a/start.js +++ b/start.js @@ -26,13 +26,12 @@ args.forEach(function (val, index, array) { } }); -// @TODO make faucet balance configurable const startupOptions = { - "mnemonic": process.env.mnemonic, - "default_balance_ether": 1000000, + "mnemonic": process.env.MNEMONIC, + "default_balance_ether": process.env.FAUCET_AMOUNT, "total_accounts": 1, "db_path": "./deploy/db", - "network_id": 666, + "network_id": process.env.NETWORK_ID, "account_keys_path": "./deploy/keys/validators.json" // Does not work yet: https://github.com/trufflesuite/ganache-cli/issues/663 }; @@ -45,7 +44,7 @@ if (!arglist.v && !arglist.mykeys) { // Generate `v` number of accounts with 32.1 ether each var accounts = []; if (arglist.v && arglist.v > 0) { - accounts.push({"balance": 0xD3C21BCECCEDA1000000}); + accounts.push({"balance": ethers.utils.bigNumberify(process.env.FAUCET_AMOUNT + "000000000000000000").toHexString()}); console.log("Creating " + arglist.v + " validator accounts and making deposits for them. Find their private keys in deploy/keys"); for (var i = 0; i < arglist.v; i++) { accounts.push({"balance": 0x1BD7A1BED4A0A0000}); // 32.1 ether to each validator @@ -76,7 +75,7 @@ if (arglist.mykeys) { startupOptions.accounts = startupOptions.accounts.concat(myAccounts); } else { console.log("Generating faucet account and appending queued keys."); - myAccounts.unshift({"balance": 0xD3C21BCECCEDA1000000}); + myAccounts.unshift({"balance": ethers.utils.bigNumberify(process.env.FAUCET_AMOUNT + "000000000000000000").toHexString()}); startupOptions.accounts = myAccounts; } } @@ -94,7 +93,7 @@ provider.listAccounts().then(function(result){ faucetAmount = balanceResult / 1e18; - let mnemonic = process.env.mnemonic; + let mnemonic = process.env.MNEMONIC; let mnemonicWallet = ethers.Wallet.fromMnemonic(mnemonic); fs.writeFile("deploy/keys/faucetkey.txt", mnemonicWallet.privateKey + ":" + mnemonicWallet.address, function(err) { @@ -102,6 +101,7 @@ provider.listAccounts().then(function(result){ return console.log(err); } console.log("Private key of faucet account "+ result[0] +" now in /deploy/keys/faucetkey.txt. It is seeded with ~" + faucetAmount + " ether."); + fwdFaucetConfiguration(mnemonicWallet); }); deployDepositContract(mnemonicWallet.privateKey).then(makeValidatorDeposits); @@ -147,7 +147,7 @@ async function makeValidatorDeposits() { if (arglist.v) { console.log("Generating keys for "+ arglist.v +" auto-generated accounts"); for (var i = 0; i <= arglist.v; i++) { - let mnemonicWallet = new ethers.Wallet.fromMnemonic(process.env.mnemonic, "m/44'/60'/0'/0/"+i); + let mnemonicWallet = new ethers.Wallet.fromMnemonic(process.env.MNEMONIC, "m/44'/60'/0'/0/"+i); let pk = mnemonicWallet.privateKey; let bls_key_sign = new keypair.Keypair(privateKey.PrivateKey.fromHexString(pk)).privateKey.toHexString(); @@ -270,7 +270,7 @@ async function makeValidatorDeposits() { async function serverStart() { const server = ganache.server(startupOptions); - server.listen(8545, function(err, blockchain) { + server.listen(process.env.PORT, function(err, blockchain) { // The server starts, you can connect to it with RPC now. console.log("Server is running, feel free to connect!"); @@ -283,4 +283,28 @@ async function serverStart() { function invertHex(hexString) { hexString = hexString.replace("0x", ""); return "0x" + hexString.split("").reverse().join(""); +} + +function fwdFaucetConfiguration(walletInfo) { + console.log("Configuring faucet."); + // check if config.json exists, if not, copy from config.json.example + if (!fs.existsSync("./deploy/faucet/config.json")) { + fs.copyFileSync("./deploy/faucet/config.json.example", "./deploy/faucet/config.json"); + console.log("Config file created."); + + let faucet_config = require("./deploy/faucet/config.json"); + faucet_config.port = process.env.FAUCET_PORT; + faucet_config.Ethereum.prod.rpc = 'http://127.0.0.1:' + process.env.PORT; + faucet_config.Ethereum.prod.account = walletInfo.address; + faucet_config.Ethereum.prod.privateKey = walletInfo.privateKey; + + fs.writeFile("./deploy/faucet/config.json", JSON.stringify(faucet_config), function(err) { + if(err) { + return console.log(err); + } + console.log("Saved new faucet configuration."); + }); + } else { + console.log("Config file for faucet already exists. Edit it manually or delete to regenerate it."); + } } \ No newline at end of file