const util = require('util'); const utils = require('./utils'); const bc = require('./blockchain'); const metamgr = require('./metamgr'); const dbhelper = require('./dbhelper'); async function privateKeyToAccount(req, rsp) { const privateKey = metamgr.getWeb3Conf()['private_key']; const account = await bc.web3.eth.accounts.privateKeyToAccount(privateKey); utils.rspData(rsp, { 'account' : account }); } async function getAccounts(req, rsp) { const accounts = await bc.web3.eth.getAccounts(); utils.rspData(rsp, accounts); } async function getBalance(req, rsp) { const balance = await bc.getBalance(metamgr.getWeb3Conf()['user_address']); utils.rspData(rsp, { 'user_address': metamgr.getWeb3Conf()['user_address'], 'balance': balance }); } async function test(req, rsp) { const orderIdPre = function () { const date = new Date(); const year = date.getFullYear(); const month = date.getMonth() + 1;//月份是从0开始的 const day = date.getDate(); const hour = date.getHours(); const min = date.getMinutes(); const sec = date.getSeconds(); return year + '' + (month < 10 ? '0' + month : month) + '' + (day < 10 ? '0' + day : day) + '' + (hour < 10 ? '0' + hour : hour) + '' + (min < 10 ? '0' + min : min) + '' + (sec < 10 ? '0' + sec : sec) + '002006'; }(); const privateKey = metamgr.getWeb3Conf()['private_key']; const buyerAddress = metamgr.getWeb3Conf()['user_address']; const paymentTokenAddress = metamgr.getContractByName('coin')['address']; const nowTime = utils.getUtcTime(); const count = 1; const batchId = 1; for (let i = 0; i < count; ++i) { const orderId = orderIdPre + utils.pad(i + 1, 6); const itemId = 10001; const price = 100; const nonce = orderId; const signStr = bc.web3.utils.soliditySha3(itemId, paymentTokenAddress, price, nonce); const { signature } = await bc.web3.eth.accounts.sign(signStr, privateKey); const ret = await bc.coinInstance.methods.increaseAllowance( metamgr.getContractByName('mall')['address'], price * count).send({ gas: 1000000 }); await dbhelper.insert( 't_box_order', [ ['order_id', orderId], ['batch_id', batchId], ['item_id', itemId], ['state', 0], ['bc_synced', 0], ['bc_sync_count', 0], ['bc_sync_time', 0], ['bc_result', 0], ['bc_block_number', '0'], ['bc_fail_reason', ''], ['buyer_address', buyerAddress], ['token_id', ''], ['price', price], ['payment_token_address', paymentTokenAddress], ['nonce', nonce], ['signature', signature], ['done', 0], ['createtime', nowTime], ['modifytime', nowTime], ] ); } utils.rspOk(rsp); } async function clearDb(req, rsp) { await dbhelper._delete('t_box_order', []); utils.rspOk(rsp); } async function searchEvent(req, rsp) { let instance = req.query.instance; let event = req.query.event; let from = req.query.from; let paramName = req.query.paramName; let paramValue = req.query.paramValue; instance = 'mallInstance'; event = 'BEBoxPaid'; from = 8639235; paramValue = '1010016076900035502'; events = await bc[instance].getPastEvents( event, { fromBlock: from - 1000, toBlock: from + 5000 - 1, filter: { boxId: paramValue } }); console.log(events); utils.rspOk(rsp); } async function execCmd(req, rsp) { if (utils.isOnlineEnv()) { utils.rspErr(rsp, 100, 'not found'); return; } const cmd = req.query.cmd; const handlers = { 'getAccounts': getAccounts, 'getBalance': getBalance, 'privateKeyToAccount': privateKeyToAccount, 'test': test, 'clearDb': clearDb, 'searchEvent': searchEvent, }; const handle = handlers[cmd]; if (handle) { handle(req, rsp); } else { utils.rspErr(rsp, 100, 'not found'); } } exports.execCmd = execCmd;