157 lines
4.4 KiB
JavaScript
157 lines
4.4 KiB
JavaScript
const util = require('util');
|
|
const utils = require('./utils');
|
|
const bc = require('./blockchain');
|
|
const metamgr = require('./metamgr');
|
|
const dbhelper = require('./dbhelper');
|
|
|
|
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 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.getServerConf()['private_key'];
|
|
const buyerAddress = metamgr.getServerConf()['user_address'];
|
|
const paymentTokenAddress = metamgr.getContractByName('coin')['address'];
|
|
const nowTime = utils.getUtcTime();
|
|
const count = 10;
|
|
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 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;
|