import { ZError } from 'common/ZError' import BaseController, { ROLE_ANON } from 'common/base.controller' import { role, router } from 'decorators/router' import logger from 'logger/logger' import { PayRecord, PayRecordClass, PayStatus } from 'modules/PayRecord' import { TransferRecord, TransferRecordClass } from 'modules/TransferRecord' import { hmacsha256 } from 'utils/security.util' import { DocumentType } from '@typegoose/typegoose' import { queryPrice, updateOrderStatus } from 'service/alchemy.svr' const calcHash = function (data: any) { let signStr = JSON.stringify(data) return hmacsha256(signStr, process.env.HASH_SALT) } const notify = async function (record: DocumentType, subTask: DocumentType) { let data: any = { orderNo: record.outOrderId, // AlchemyPay订单号 crypto: record.crypto, // 用户购买的数字货币 cryptoAmount: record.cryptoAmount, //用户的提币数量 cryptoPrice: record.cryptoPrice, // 实时的价格/USDT CEG锚定USDT txHash: record.txHash, // 给用户转账的hash network: record.network, // 用户购买的数字货币对应的网络 // networkFee: record.networkFee, // 网络费用/USDT address: record.address, // 用户的提币地址 status: record.status === PayStatus.TRANSFERED ? 'SUCCESS' : 'FAIL', // SUCCESS/FAIL } try { let priceData = await queryPrice({ gas: subTask.gas }) data.networkFee = priceData.leagel let result = await updateOrderStatus(data) logger.info('update transfer status success::', JSON.stringify(result)) if (result.success) { subTask.status = 8 await subTask.save() } } catch (err) { logger.error(`notify alchemy error:: ${err.message}`) } } export default class InternalController extends BaseController { @role(ROLE_ANON) @router('post /api/internal/update_task') async updateTaskInfo(req) { let { sign, id, result, successCount, errorCount, gas, gasPrice, hashList } = req.params if (!sign) { throw new ZError(10, 'sign not found') } let hash = calcHash({ id, result, successCount, errorCount, gas, gasPrice, hashList }) console.log(hash, sign) if (sign !== hash) { throw new ZError(11, 'sign not match') } logger.info(`task report:: ${id}|${result}|${successCount}|${errorCount}|${JSON.stringify(hashList)}}`) if (!id) { throw new ZError(11, 'taskId not found') } let record = await TransferRecord.findById(id) if (!record) { throw new ZError(12, 'TransferRecord not found') } let task = await PayRecord.findById(record.recordId) if (!task) { throw new ZError(13, 'PayRecord not found') } if (result === 2) { record.status = 9 record.gas = gas record.gasPrice = gasPrice record.hashList = hashList task.txHash = hashList[0] task.status = PayStatus.TRANSFERED } else { record.status = 10 task.status = PayStatus.TRANSFER_FAIL } await record.save() await task.save() setImmediate(async () => { await notify(task, record) }) return {} } }