transfer.js 6.38 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 some = accountData.RECORDS.some(item => {
      if (item.address === body.senderAddress) {
        return (body.amount * 10 ** 8) == item.balance;
      }
    });
    // 加上手续费
    body.amount = (Number(body.amount) + 0.1)
    if (!some) {
      ctx.helper.err({
        ctx,
        err: '金额不正确,请联系社区工作人员',
      });
      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 haveBalance() {
  //   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 getBalance() {
    const { ctx, config } = this;
    const query = ctx.request.query;
    let balance = 0;
    let total = 0
    const rule = {
      address: { type: 'string' },
    };
    try {
      ctx.validate(rule, query);
    } catch (error) {
      console.log(error);
      ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
      return;
    }
    const transferCount = await this.ctx.model.Transfer.count({ where: { address: query.address } });
    if (transferCount !== 0) {
      balance = 0;
    } else {
      accountData.RECORDS.some(item => {
        if (item.address === query.address && item.balance > 0) {
          balance = item.balance / (10 ** 8);
          return true;
        }
        if (item.balance > 0) {
          total += Number(item.balance)
        }
      });
    }
    console.log(total / (10 ** 8))
    ctx.helper.success({
      ctx,
      data: {
        account: { balance }
      },
    });


  }
  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;