transfer_20220522102720.js 4.86 KB
'use strict';

const Controller = require('egg').Controller;
const nodeSdk = require('@ddn/node-sdk').default;
const accountData=require('../../config/mem_accounts.json')
class TransferController extends Controller {
  async postTransaction() {
    const { ctx, config } = this;
    const body = ctx.request.body;
    const rule = {
      secret: { type: 'string' },
      amount: { type: 'string' },
      transaction: { type: 'string' },
    };
    console.log(body);
    try {
      ctx.validate(rule);
    } catch (error) {
      ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
      return;
    }
    const oldNetTransaction = JSON.parse(body.transaction);
    if (oldNetTransaction.recipientId !== config.ed25519Address) {
      ctx.helper.err({
        ctx,
        err: '接收地址不正确,请使用正确的地址',
      });
      return;
    }
    // const oldNetTransaction = JSON.stringify(body.transaction);
    // 旧网转出
    const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
    console.log('就往转账', oldNetData, typeof oldNetData);
    if (!oldNetData || !oldNetData.success) {
      ctx.helper.err({
        ctx,
        data: oldNetData,
      });
      return;
    }
    const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
    ctx.helper.success({
      ctx,
      data: result,
    });

    // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
    // const trs = JSON.stringify({ transaction });
    // // 上链操作
    // const data = await this.service.sdk.pushTransaction(trs);
    // if (data.success) {
    //   ctx.helper.success({
    //     ctx,
    //     data,
    //   });
    //   ctx.model.Transfer.update({ id: result.id }, { finish: true });
    // } else {
    //   ctx.helper.err({
    //     ctx,
    //     data,
    //   });
    //   ctx.model.Transfer.update({ id: result.id }, { finish: false });
    // }

  }
  async getBalance() {
    const { ctx, config } = this;
    const query = ctx.request.query;
    const rule = {
      address: { type: 'string' },
    };
    console.log(query);
    try {
      ctx.validate(rule, query);
    } catch (error) {
      console.log(error);
      ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
      return;
    }
    const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
    ctx.helper.success({
      ctx,
      data,
    });
  }
  async getTransaction() {
    const { ctx, config } = this;
    const query = ctx.request.query;
    const rule = {
      address: { type: 'string' },
    };
    console.log(query);
    try {
      ctx.validate(rule, query);
    } catch (error) {
      console.log(error);
      ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
      return;
    }
    const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
    ctx.helper.success({
      ctx,
      data,
    });
  }
  async asyncAccount() {
    const { ctx, config } = this;
    const limit = 100;
    let asyncCount = 0;
    let errAsyncAccount = 0;
    let balanceIsZero = 0;
    const data = await this.service.sdk.getAccountCount(config.ed25519Url);
    if (data.success) {
      const length = Math.ceil(data.count / 100);
      console.log(data.count)
      for (let index = 0; index < length; index++) {
        const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
        console.log(acountList)
        if (acountList.success) {
          for (let index = 0; index < acountList.accounts.length; index++) {
            const element = acountList.accounts[index];
            if (element.balance == 0) {
              balanceIsZero++
              continue;
            }
            const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
            if (!result) {
              errAsyncAccount++;
              this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
            } else {
              asyncCount++;
            }
          }
        } else {
          this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
        }
      }
    } else {
      this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
    }
    ctx.helper.success({
      ctx,
      data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
    });
  }
}

module.exports = TransferController;