forked from saturn-network/airdrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
309 lines (273 loc) · 9.78 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env node
const ethers = require('ethers')
const fg = require('fast-glob')
const path = require('path')
const fs = require('fs')
const _ = require('lodash')
const BigNumber = require('bignumber.js')
const inquirer = require('inquirer')
const chalk = require('chalk')
const providers = require('./src/networks')
const greeting = require('./src/logo')
const utils = require('./src/utils')
const logic = require('./src/airdrop-logic')
console.log(greeting)
let config = {
blockchain: '',
provider: null,
wallet: null,
myAddress: '',
airdropContractAddress: '',
tokenAddress: '',
tokenName: '',
tokenSymbol: '',
tokenDecimals: null,
tokenIsERC223: false,
airdropAmount: null,
myTokenBalance: null,
addresses: []
}
let chainPrompt = {
type: 'list',
name: 'chain',
message: 'What blockchain does your token exist on?',
choices: Object.keys(providers)
};
inquirer.prompt(chainPrompt).then(answers => {
config.blockchain = answers.chain
config.provider = providers[answers.chain].provider
config.airdropContractAddress = providers[answers.chain].airdropContractAddress
}).then(() => {
getWallet()
})
function getWallet() {
let pkeyOrMnemonicPrompt = {
type: 'list',
name: 'usersecret',
message: 'How would you like to access your wallet?',
choices: ['Private Key', '12 Word Mnemonic']
}
inquirer.prompt(pkeyOrMnemonicPrompt).then(answers => {
if (answers.usersecret === 'Private Key') {
walletFromPkey()
} else {
walletFromMnemonic()
}
})
}
function walletFromPkey() {
let pkeyinput = {
type: 'password',
message: 'Enter your Private Key',
name: 'pkey'
}
inquirer.prompt(pkeyinput).then(answers => {
try {
let wallet = new ethers.Wallet(answers.pkey)
config.wallet = wallet.connect(config.provider)
config.myAddress = wallet.address
getTokenInfo()
} catch(e) {
console.error(`Unable to connect to ${config.chain} blockchain. Check that your private key is correct.`)
walletFromPkey()
}
})
}
function walletFromMnemonic() {
let mnemonicPrompt = {
type: 'password',
message: 'Enter your 12 Word Seed Phrase',
name: 'mnemonic'
}
inquirer.prompt(mnemonicPrompt).then(answers => {
let offsets = [...Array(10).keys()]
try {
let wallets = offsets.map(x => {
return ethers.Wallet.fromMnemonic(answers.mnemonic, `m/44'/60'/0'/0/${x}`)
})
let selectWalletPrompt = {
type: 'rawlist',
message: 'Select wallet:',
name: 'addy',
choices: wallets.map(x => x.address)
}
inquirer.prompt(selectWalletPrompt).then(answers => {
let filtered = _.filter(wallets, x => x.address === answers.addy)
let wallet = filtered[0].connect(config.provider)
config.wallet = wallet
config.myAddress = wallet.address
getTokenInfo()
})
} catch(e) {
console.error(`Unable to connect to ${config.chain} blockchain. Check that your 12 word mnemonic is correct.`)
walletFromMnemonic()
}
})
}
function getTokenInfo() {
let tokenAddressPrompt = {
type: 'input',
message: 'Enter address of the token you wish to airdrop',
name: 'tokenAddress'
}
inquirer.prompt(tokenAddressPrompt).then(async (answers) => {
try {
let tokenInfo = await utils.getTokenInfo(config.blockchain, answers.tokenAddress)
let tokenIsERC223 = await utils.tokenIsERC223(answers.tokenAddress, config.provider)
let myTokenBalance = await utils.myTokenBalance(
config.blockchain,
answers.tokenAddress,
config.myAddress
)
let myEtherBalance = await utils.myEtherBalance(
config.blockchain,
config.myAddress
)
config.tokenAddress = tokenInfo.address
config.tokenName = tokenInfo.name
config.tokenSymbol = tokenInfo.symbol
config.tokenDecimals = tokenInfo.decimals
config.tokenIsERC223 = tokenIsERC223
config.myTokenBalance = new BigNumber(myTokenBalance)
config.myEtherBalance = new BigNumber(myEtherBalance)
if (config.myTokenBalance.isEqualTo(new BigNumber(0))) {
throw new Error(`You do not have any ${config.tokenSymbol} in your wallet. Please top up your airdrop wallet first. Having trouble? You can order an airdrop here ${chalk.red.underline('https://forms.gle/QjtUYcbttCeyUfK48')}`)
}
console.log(`Fetched information for ${chalk.underline(config.tokenName)}. Your current token balance is ${chalk.green(config.myTokenBalance.toFixed())} ${config.tokenSymbol}`)
console.log(`Your ${config.blockchain.toUpperCase()} balance is ${chalk.green(config.myEtherBalance)}. Make sure you have enough ${config.blockchain.toUpperCase()} in your wallet before you start the airdrop. We recommend having ${chalk.green.underline('at least 0.3 ' + config.blockchain.toUpperCase())} in your wallet for a successful airdrop.`)
getAirdropAmount()
} catch(e) {
console.error(e.message)
console.error(`Unable to fetch token information for '${answers.tokenAddress}'. Please ensure you entered correct token address and try again`)
getTokenInfo()
}
})
}
function getAirdropAmount() {
let prompt = {
type: 'input',
message: `How many ${config.tokenSymbol} tokens do you wish to airdrop? Press [ENTER] to airdrop full wallet balance.`,
name: 'airdropAmount',
default: config.myTokenBalance.toFixed()
}
inquirer.prompt(prompt).then((answers) => {
let airdropAmount = new BigNumber(answers.airdropAmount)
if (airdropAmount.isNaN()) {
console.error(`Incorrect input. Try again`)
getAirdropAmount()
} else if (airdropAmount.isGreaterThan(config.myTokenBalance)) {
console.error(`You cannot airdrop more tokens than you have in your wallet.`)
getAirdropAmount()
} else {
config.airdropAmount = airdropAmount
airdropAddressSelect()
}
})
}
function airdropAddressSelect() {
utils.hodlers().then(async (hodlers) => {
let genesis = require('./src/saturn-genesis.json')
let strntraders = await utils.toptraders('strn')
let saturntraders = await utils.toptraders('strn')
let daoGenesisString = `Saturn DAO Genesis (${genesis.length} addresses)`
let hodlerString = `Saturn HODL Investors (${hodlers.length} addresses)`
let strnString = `Top 100 STRN traders by volume over last 100 days`
let saturnString = `Top 100 SATURN traders by volume over last 100 days`
let customString = `Custom airdrop - follow guide at ${chalk.underline('https://www.saturn.network/blog/airdrop-tool/')}`
let prompt = {
type: 'list',
message: `Who do you want to receive the airdrop?`,
name: 'airdropType',
choices: [
daoGenesisString,
hodlerString,
strnString,
saturnString,
customString
]
}
inquirer.prompt(prompt).then((answers) => {
if (answers.airdropType == customString) {
customAirdropAddressInput()
} else if (answers.airdropType == daoGenesisString) {
config.addresses = genesis
resume()
} else if (answers.airdropType == hodlerString) {
config.addresses = hodlers
resume()
} else if (answers.airdropType == strnString) {
config.addresses = strntraders
resume()
} else if (answers.airdropType == saturnString) {
config.addresses = saturntraders
resume()
} else {
console.error(`Wrong input. Try again`)
airdropAddressSelect()
}
})
})
}
function customAirdropAddressInput() {
let goback = 'I do not have a JSON file. Go back to previous menu.'
let pattern = path.join(process.cwd(), '*.json').replace(/\\/g, '/')
let prompt = {
type: 'list',
message: `Select the .json file with addresses that you have prepared in advance.`,
name: 'customAddresses',
choices: fg.sync(pattern).concat([goback])
}
inquirer.prompt(prompt).then((answers) => {
if (answers.customAddresses == goback) {
return airdropAddressSelect()
} else {
config.addresses = require(answers.customAddresses)
resume()
}
})
}
async function activateAirdrop() {
if (!config.tokenIsERC223) {
console.log(`Activating the airdrop...`)
await logic.approve(
config.wallet,
config.tokenAddress,
config.airdropAmount,
config.tokenDecimals,
config.airdropContractAddress
)
}
}
async function doAirdrop() {
let batchabi = require('./config/airdropabi.json')
console.log(`Airdropping ${config.airdropAmount} ${config.tokenName} tokens to ${config.addresses.length} addresses`)
let batch = new ethers.Contract(config.airdropContractAddress, batchabi, config.wallet)
let gasPrice = await utils.gasPrice(config.blockchain)
let tokenhash = {
"name": config.tokenName,
"address": config.tokenAddress,
"amount": config.airdropAmount
}
await activateAirdrop()
config.tokenIsERC223
? await logic.erc223Airdrop(tokenhash, config.wallet, batch, config.addresses, gasPrice)
: await logic.erc20Airdrop(tokenhash, config.wallet, batch, config.addresses, gasPrice)
}
function resume() {
let prompt = {
type: 'confirm',
message: `Press [ENTER] to confirm that you have entered information correctly and you authorize the airdrop.`,
name: 'execute',
default: true
}
inquirer.prompt(prompt).then(async (answers) => {
if (answers.execute) {
await doAirdrop()
console.log(chalk.green.bold(`Airdrop complete! Tell your community to visit ${chalk.underline('https://www.saturn.network/my-airdrops')} to collect their new ${config.tokenSymbol} tokens!`))
process.exit(0)
} else {
console.log(`You have decided to abort the airdrop and the program will now exit.\nHaving trouble? You can order an airdrop here ${chalk.red.underline('https://forms.gle/QjtUYcbttCeyUfK48')}`)
process.exit(0)
}
})
}