123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- const router = require('koa-router')() //导入 koa-router
- const moralis = require("../model/moralis_sdk") //导入 db.js
- // https://github.com/luin/ioredis#readme
- const withdraw_db = require('../model/db/withdraw_db')
- router.prefix('/denettest');
- const BigNumber = require('bignumber.js')
- function calculate_total_usdprice(amount, decimals, usdprice) {
- return parseInt(amount) / (10 ** parseInt(decimals)) * parseFloat(usdprice);
- // var new_decimals = 15;
- // var pos = 0;
- // var new_amount = 0;
- // var new_amount_bint = 0;
- // if (parseInt(decimals) > new_decimals) {
- // pos = parseInt(decimals) - parseInt(decimals) % new_decimals
- // new_amount = amount.substring(0, pos+1)
- // new_amount_bint = parseInt(new_amount)
- // var scale_dec = parseInt((10 ** new_decimals))
- // console.log('amount',amount)
- // console.log('pos',pos)
- // console.log('new_amount',new_amount)
- // console.log('new_amount_bint',new_amount_bint)
- // console.log('scale_dec',scale_dec)
- // console.log('usdprice',usdprice)
- // 1000000000000000
- // 1000000000000000
- // return new_amount_bint / scale_dec * parseInt(usdprice);
- // } else {
- // return BigInt(amount) / (10 ** BigInt(decimals) * BigInt(usdprice));
- // }
- }
- /**
- * 获取交易记录
- * @param {*} ctx
- */
- async function getTransfers(ctx) {
- const obj = ctx.request.body;
- console.log("obj", obj);
- if (!obj.chain)//默认 bsc 币安链
- obj.chain = 'bsc'
- var token_total_usdprice = calculate_total_usdprice('99999999999999999998', '18', 0.1);
- var data =
- {
- nonce: 118,
- gasPrice: { type: 'BigNumber', hex: '0x02540be400' },
- gasLimit: { type: 'BigNumber', hex: '0x5208' },
- to: '0x7C7401fcc82D1e53C4090561c3e6fde80d74e317',
- value: { type: 'BigNumber', hex: '0x039696f3392000' },
- data: '0x',
- chainId: 97,
- v: 230,
- r: '0x075149b50e81da71aa72ee73d1b07e8df6fa2416b97133dc3e2dea19b4ed4c88',
- s: '0x7d8ac906b85275d63233e8f8ba6c0f5b860e9ebea1bc896346fe81824d9ec9c3',
- from: '0xAD48D13E77011cFE03fF19729B6A247847AfD28E',
- hash: '0xb9a75efb6a17325f2decaec9fba30fead7664c13a589fe0ccac02f791a7c84c8',
- type: null,
- confirmations: 0
- }
- var curGasPrice = BigNumber(data.gasPrice.hex)
- var curGasLimit = BigNumber(data.gasLimit.hex)
- if (curGasLimit > 0) {
- console.log('curGasLimit', curGasLimit.toNumber())
- }
- console.log('curGasPrice', curGasPrice.toNumber())
- var a = parseInt('10000000000000000')
- var b = parseInt('100000000000000000')
- console.log(token_total_usdprice, a / b);
- // ctx.body = await moralis.collectCoins(obj);
- //提交归集任务
- if (obj.address) {
- moralis.pushCollectConisObj(obj)
- }
- ctx.body = curGasPrice + '-' + curGasLimit;
- // await moralis.collectCoins(obj).then((result) => {
- // ctx.body = result;
- // })
- }
- //获取交易记录
- router.post('/getTransfers', getTransfers)
- async function withdraw(ctx) {
- if (ctx.request == null || ctx.request.body == null) {
- ctx.body = utils.toJson(-1, null, "request error. ");
- return;
- }
- const obj = ctx.request.body;
- var temp_ob = { ...obj }
- temp_ob.from_address = '0xxpaoifhjopiesaj'
- temp_ob.to_address = temp_ob.receiver
- var ret = await withdraw_db.create_withdraw_task(temp_ob);
- ctx.body = ret;
- var isExist = await withdraw_db.withdraw_id_exist(temp_ob.withdraw_id)
- if (isExist) {
- // [ret_obj.withdraw_status, ret_obj.withdraw_hash, ret_obj.nonce, ret_obj.update_time, ret_obj.gas_price, ret_obj.gas_limit, withdraw_id]
- // var update_obj = {
- // withdraw_status: 1,
- // withdraw_hash: 'asdfasdf',
- // nonce: 0,
- // update_time: 12989,
- // gas_price: '1200000',
- // gas_limit: '80000'
- // }
- var update_obj={}
- await withdraw_db.update_withdraw_task(temp_ob.withdraw_id, update_obj)
- }
- }
- async function crypto_test(ctx) {
- if (ctx.request == null || ctx.request.body == null) {
- ctx.body = utils.toJson(-1, null, "request error. ");
- return;
- }
- const obj = ctx.request.body;
- var crypt_key = obj.key;
- var message = obj.privateKey;
- //process.env.NODE_ENV
- console.log("NDK_HOME", process.env.NDK_HOME);
- var de_message = utils.decryptPrivityKey(crypt_key, message);
- console.log("decrypt message:", message);
- ctx.body = {
- decrypt: de_message
- }
- }
- router.post('/crypto_test', crypto_test)
- router.post('/withdraw', withdraw)
- module.exports = router
|