aozhiwei d236431e2a 1
2022-01-24 19:25:33 +08:00

173 lines
4.9 KiB
JavaScript

const util = require('util');
const utils = require('./utils');
const bc = require('./blockchain');
const metamgr = require('./metamgr');
const db = require('./db');
async function buyBox(req, rsp) {
try {
const privateKey = metamgr.getServerConf()['private_key'];
const paymentTokenAddress = metamgr.getContractByName('coin')['address'];
const tokenId = 1001;
const boxId = 1001;
const type = 1001;
const price = 100;
const buyerAddress = metamgr.getServerConf()['user_address'];
const nonce = Math.random()*1000|0;
const signStr = bc.web3.utils.soliditySha3(type, paymentTokenAddress, price, nonce);
const { signature } = await bc.web3.eth.accounts.sign(signStr, privateKey);
await bc.coinInstance.methods.increaseAllowance(
metamgr.getContractByName('mall')['address'],
price).send({ gas: 1000000 });
const result = await bc.mallInstance.methods.buyBoxWithSignature(
boxId,
type,
buyerAddress,
price,
paymentTokenAddress,
nonce,
signature).send({gas: 1000000});
utils.rspData(rsp, {
'signature' : signature,
'result': result
});
} catch (err) {
utils.rspErr(rsp, 1, '' + err);
}
}
async function privateKeyToAccount(req, rsp) {
const privateKey = metamgr.getServerConf()['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.getServerConf()['user_address']);
utils.rspData(rsp, {
'user_address': metamgr.getServerConf()['user_address'],
'balance': balance
});
}
async function test(req, rsp) {
const orderIdPre = function () {
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.getServerConf()['private_key'];
const buyerAddress = metamgr.getServerConf()['user_address'];
const paymentTokenAddress = metamgr.getContractByName('coin')['address'];
const nowTime = utils.getUtcTime();
for (let i = 0; i < 10; ++i) {
const orderId = orderIdPre + utils.pad(i + 1, 6);
const boxId = orderId;
const itemId = 10001;
const price = 100;
await db.execScript(
'INSERT INTO t_box(box_id, goods_id, goods_idx, item_id, state,' +
' order_id, order_valid, owner_address, createtime, modifytime)' +
'VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[
boxId,
0, //goods_id
0, //goods_idx
itemId, //item_id
0, //state
orderId, //order_id
0, //order_valid
buyerAddress,
nowTime, //creattime
nowTime //modifytime
]);
const nonce = orderId;
const signStr = bc.web3.utils.soliditySha3(itemId, paymentTokenAddress, price, nonce);
await bc.coinInstance.methods.increaseAllowance(
metamgr.getContractByName('mall')['address'],
price).send({ gas: 1000000 });
await db.execScript(
'INSERT INTO t_box_order(order_id, box_id, state, bc_synced, bc_sync_count,' +
' bc_sync_time, bc_result, bc_block_number, bc_fail_reason, buyer_address,' +
' price, payment_token_address, nonce, signature, expired, done, createtime, modifytime)' +
'VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[
orderId,
boxId,
0, //state
0, //bc_synced
0, //bc_sync_count
0, //bc_sync_time,
0, //bc_result,
0, //bc_block_number,
'', //bc_fail_reason,
buyerAddress, //buyer_address,
price, //price
paymentTokenAddress,
nonce,
signature,
0, //expired
0, //done
nowTime, //createtime,
nowTime, //modifytime
]
);
}
utils.rspOk(rsp);
}
async function clearDb(req, rsp) {
await db.execScript('DELETE FROM t_box', []);
await db.execScript('DELETE FROM t_box_order', []);
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 = {
'buyBox': buyBox,
'getAccounts': getAccounts,
'getBalance': getBalance,
'privateKeyToAccount': privateKeyToAccount,
'test': test,
'clearDb': clearDb,
};
const handle = handlers[cmd];
if (handle) {
handle(req, rsp);
} else {
utils.rspErr(rsp, 100, 'not found');
}
}
exports.execCmd = execCmd;