index.js 3.82 KB
// import { password } from './config'
const password = require('./config').password
const sdk = require('@ddn/node-sdk').default
const data = require('./config/mem_accounts.json')
const addressList = require('./config/uninclude.js').addressList
// const nodeFetch = require('node-fetch')
const request = require('request')
const file = require('fs')

async function main() {
  let success = [], // 转账成功的下标
    error = [], // 转账失败的数据
    total = 0, //转账总条数
    successCount = 0, //成功条数
    errorCount = 0,  // 失败条数
    succeeAmount = 0,// 转账成功的金额
    errorAmount = 0,//失败的金额
    totalAmount = 0, //总金额
    unTrsAmount = 0, //没有转账的金额
    unTrsCount = 0,// 没有转账的条数
    unTrs = []; // 没有转账的数据
  for (let index = 0; index < data.RECORDS.length; index++) {
    const element = data.RECORDS[index];
    if (Number(element.balance) > 0 && element.publicKey && !addressList.includes(element.address)) {
      await wait(1000)
      const amount = Number(element.balance) / 10 ** 8
      totalAmount += amount
      total++
      const data = await createTransaction(element.address, amount)
      let result = await pushTransaction(data)
      console.log(result)
      result = JSON.parse(result)
      if (result.success) {
        successCount++
        succeeAmount += amount
        success.push({ index, address: element.address, balance: element.balance })
        console.log(`转帐成功条数:${successCount}, 转账成功总金额:${succeeAmount}`)
      } else {
        errorCount++
        errorAmount += amount
        error.push({ index, result, address: element.address, balance: element.balance })
        console.log(`转帐失败条数:${errorCount}, 转账失败总金额:${errorAmount},当前失败金额${amount}`)
      }
    } else {
      const amount = Number(element.balance) / 10 ** 8
      unTrsAmount += amount
      unTrsCount++
      unTrs.push({ index, address: element.address, balance: element.balance })
    }
  }
  console.log(`转账总条数:${total}, 转账总金额:${totalAmount}`)
  console.log(`转帐成功条数:${successCount}, 转账成功总金额:${succeeAmount}`)
  console.log(`转帐失败条数:${errorCount}, 转账失败总金额:${errorAmount}`)
  console.log(`没有转账条数:${unTrsCount}, 没有转账金额:${unTrsAmount}`)
  file.writeFile('./log/success.text', `{"total":${successCount},"totalAmount":${succeeAmount},"list":${JSON.stringify(success)}}`, { flag: 'a' }, err => { })
  file.writeFile('./log/error.text', `{"total":${errorCount},"totalAmount":${errorAmount},"list":${JSON.stringify(error)}}`, { flag: 'a' }, err => { })
  file.writeFile('./log/unTrs.text', `{"total":${unTrsCount},"totalAmount":${unTrsAmount},"list":${JSON.stringify(unTrs)}}`, { flag: 'a' }, err => { })
  file.writeFile('./log/all.text', `{"total":${total},"totalAmount":${totalAmount}}`, { flag: 'a' }, err => { })
}
async function createTransaction(recipientId, amount) {
  const transaction = await sdk.transaction.createTransaction(recipientId, amount, '账户转账', password)
  return JSON.stringify({ transaction })
}
async function pushTransaction(data) {
  return new Promise((resolve, reject) => {
    request({
      url: 'http://localhost:8001/api/transactions',
      method: 'POST',
      body: data,
      headers: {
        "content-type": 'application/json'
      }
    }, (err, response, body) => {
      if (err) {
        reject(err)
      } else {
        resolve(body)
      }
    })
  })
  //  const respose=await nodeFetch('http://localhost:8001/api/transaction', {
  // 	method: 'post',
  // 	body: JSON.stringify(data),
  // 	headers: {'Content-Type': 'application/json'}
  // })
  // return await respose.json()
}

let wait = ms => new Promise(resolve => setTimeout(resolve, ms));
main()