create-distributor-json.ts 1.74 KB
Newer Older
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
import fs from 'fs'

import { task } from 'hardhat/config'
import { parse } from 'csv-parse'
import { BigNumber } from 'ethers'

task('create-distributor-json')
  .addParam('inFile', 'CSV to read')
  .addParam('outFile', 'JSON to create')
  .addOptionalParam(
    'mnemonic',
    'Mnemonic',
    process.env.DISTRIBUTOR_FALLBACK_MNEMONIC
  )
  .setAction(async (args, hre) => {
    const parser = fs.createReadStream(args.inFile).pipe(parse())
    const records = []
    let total = BigNumber.from(0)
    for await (const record of parser) {
      const name = record[0].trim()
      const amt = record[record.length - 4].trim().replace(/,/gi, '')
      const address = record[record.length - 3].trim()

      records.push({
        name,
        amountHuman: amt,
        amount: hre.ethers.utils.parseEther(amt).toString(),
        address,
        fallbackIndex: -1,
      })
      total = total.add(amt)
    }

    records.sort((a, b) => {
      if (a.name > b.name) {
        return 1
      }

      if (a.name < b.name) {
        return -1
      }

      return 0
    })

    for (let i = 0; i < records.length; i++) {
      const record = records[i]
      if (record.address.slice(0, 2) !== '0x') {
        console.log(
          `Generating fallback address for ${record.name}. Account index: ${i}`
        )
        const wallet = hre.ethers.Wallet.fromMnemonic(
          args.mnemonic,
          `m/44'/60'/0'/0/${i}`
        )
        record.address = wallet.address
        record.fallbackIndex = i
      }
    }

    fs.writeFileSync(args.outFile, JSON.stringify(records, null, ' '))
    console.log(`Total: ${total.toString()}`)
    if (total.eq(1_434_262_041)) {
      console.log('AMOUNTS VERIFIED')
    } else {
      throw new Error('AMOUNTS INVALID')
    }
  })