This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path00.setup.js
executable file
·136 lines (114 loc) · 3.54 KB
/
00.setup.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
#!/usr/bin/env -S yarn node
/*
1. Creates new Ethereum wallet on the network indicated by RPC_URL
2. Saves the private key and other info in config.json
*/
const fs = require('fs')
const readline = require('readline')
const Web3 = require('web3')
const P = require('aigle')
const saveAsJson = require('./helpers/saveAsJson')
const RPC_URL = process.argv[2]
const createNewAccount = process.env.CREATE_NEW_ACCOUNT === 'true'
const useExistingAccount = process.env.USE_EXISTING_ACCOUNT === 'true'
const API_URL = process.env.API_URL || 'https://api.stg.rhino.fi'
const DATA_API_URL = process.env.DATA_API_URL || API_URL
if (!RPC_URL) {
console.error('ERROR: RPC_URL not set')
console.error(`\nusage: ./0.setup.js RPC_URL
You need an Ethereum node to connect to.
Either run one locally or use a service such as https://www.infura.io or https://www.alchemy.com`)
process.exit(1)
}
const configFileName = process.env.CONFIG_FILE_NAME || 'config.json'
const configFilePath = `${__dirname}/${configFileName}`
const getBalanceInEth = async (web3, account) => {
return web3.utils.fromWei(
await web3.eth.getBalance(account.address),
'ether'
)
}
const go = async (configPath) => {
const web3 = new Web3(new Web3.providers.HttpProvider(RPC_URL))
let account
if (configPath) {
console.log(`using existing config at: ${configPath}`)
const config = require(configPath)
if (!(config.account && config.account.address)) throw new Error('account.address not defined in config')
account = config.account
} else {
account = web3.eth.accounts.create()
console.log('Created new Ethereum account:', account.address)
saveAsJson(configFilePath, {
RPC_URL,
ETH_PRIVATE_KEY: account.privateKey,
API_URL,
DATA_API_URL,
account
})
console.log(`Created ./${configFileName}`)
}
const balance = await getBalanceInEth(web3, account)
const minBalance = 0.1
if (balance < minBalance) {
console.warn(
`\nWARNING: Your account balance (${balance} ETH) migth be insufficient to run the examples.
Please transfer some ETH to your account (${account.address}).
You can use a faucet like: https://goerlifaucet.com/`
)
}
}
const ask = question => {
return new P((resolve, reject) => {
try {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question(question, answer => {
rl.close()
resolve(answer)
})
} catch (error) {
reject(error)
}
})
}
;(async () => {
if (fs.existsSync(configFilePath)) {
if (useExistingAccount) {
await go(configFilePath)
} else if (createNewAccount) {
await go()
} else {
let answer
// For non-interactive mode (reponds to the prompt below)
if (process.argv.length >= 4) {
switch (process.argv[3]) {
case '--yes':
answer = 'yes'
break
case '--no':
answer = 'no'
break
default:
break
}
}
answer = answer || await ask(
`The ./${configFileName} file exits, do you want to use this config?
If you choose 'yes', existing ./${configFileName} will not be modified.
If you chooce 'no', a new account will be created and the ./${configFileName} file overwritten (yes/no): `,
)
await (answer === 'yes'
? go(configFilePath)
: go()
)
}
} else {
await go()
}
})().catch(error => {
console.error(error)
process.exit(1)
})