const Koa = require('koa');
const router = require('koa-router')();
const Web3 = require('web3');
const bodyParser = require('koa-bodyparser');
const token_abi = require('../config/token_abi');
const token_address = require('../config/token_address');
const request = require("request");
const cors = require('koa2-cors');
const HttpUtils = exports;
var app = new Koa();
var web3_sk;
var Contract_Token;
router.get('/', async (ctx) => {
ctx.body = "router test"
});
router.post('/test1', async (ctx) => {
let res = ctx.request.body;
console.log("res=>", res);
let a = {name: "zhangsan"};
ctx.body = a;
});
router.post('/getethone', async (ctx) => {
let res = ctx.request.body;
let state = Common.verification(res);
if (state != "OK") {
return state;
}
console.log("state:", state)
let accoutdata = await Controller.getmyAccountdata1(
web3_sk,
res.Faddress,
res.FstartBlockNumer,
res.FendBlockNumber,
res.type
);
ctx.body = accoutdata;
});
router.post('/gettoken', async (ctx) => {
console.log(ctx.request.body);
let res = ctx.request.body;
console.log("post parmas is ok22222222 ", res);
let data = {
url: web3_sk,
acount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
start: "4892325",
end: "4892329"
};
let accoutdata = await Controller.getmyAccountdata(data.url, data.acount, data.start, data.end);
ctx.body = accoutdata;
});
router.post('/gettoken201', async (ctx) => {
console.log(ctx.request.body);
let res = ctx.request.body;
console.log("post parmas is ok ");
let data = {
url: web3_sk,
acount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
start: "4892325",
end: "4892329"
};
let accoutdata = await Controller.getTokenmyAccountdata(data.url, data.acount, data.start, data.end);
ctx.body = accoutdata;
});
router.post('/getBatchEthernet', async (ctx) => {
let res = ctx.request.body;
let requestData = [
{
web3Url: web3_sk,
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
startBlockNumber: "5251538",
endBlockNumber: "5251542",
type: 1
},
{
web3Url: web3_sk,
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
startBlockNumber: "5251538",
endBlockNumber: "5251542",
type: 2
},
{
web3Url: web3_sk,
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
startBlockNumber: "5251538",
endBlockNumber: "5251542",
type: 3
}
]
let resultData = await Controller.queryBatchProcessingTaifangAddress(requestData);
ctx.body = resultData;
});
router.post('/getBatchEthernetscope', async (ctx) => {
let res = ctx.request.body;
let requestData = {
web3Url: web3_sk,
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
blockInterval: [
{startBlockNumber: 5251538, endBlockNumber: 5251542, type: 1},
{startBlockNumber: 5251538, endBlockNumber: 5251542, type: 2},
{startBlockNumber: 5251538, endBlockNumber: 5251542, type: 3}
]
}
let resultData = await Controller.queryNumberIntervalInformationAddress(requestData);
ctx.body = resultData;
});
router.post('/getInformation', async (ctx) => {
let res = ctx.request.body;
let requestData = {
web3Url: web3_sk,
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
startBlockNumber: "2019/03/22 10:37:50",
endBlockNumber: "2019/03/22 10:38:15",
type: 3
}
let startTime = new Date(requestData.startBlockNumber).getTime();
let endTime = new Date(requestData.endBlockNumber).getTime();
console.log("startTime =>", startTime);
console.log("endTime =>", endTime);
let requestData1 = {
web3Url: web3_sk,
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
startBlockNumber: startTime,
endBlockNumber: endTime,
type: 2
}
let resultData = await Controller.getsDataForspecifiedTimeAtAddressData(requestData);
ctx.body = resultData;
});
router.post('/getInformations', async (ctx) => {
let res = ctx.request.body;
let requestData = {
web3Url: web3_sk,
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
blockList: [
{starTime: "2019/03/22 10:37:50", endTime: "2019/03/22 10:38:15", type: 3},
{starTime: "2019/03/22 10:37:50", endTime: "2019/03/22 10:38:15", type: 3}
],
type: 3
}
let resultData = await Controller.getsDataForspecifiedTimeAtAddressDatas(requestData);
ctx.body = resultData;
});
router.post('/getInformationsmore', async (ctx) => {
let res = ctx.request.body;
let requestData = [
{
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
startBlockNumber: "2019/03/22 10:37:50",
endBlockNumber: "2019/03/22 10:38:15",
type: 3
},
{
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
startBlockNumber: "2019/03/22 10:37:50",
endBlockNumber: "2019/03/22 10:38:15",
type: 3
},
{
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
startBlockNumber: "2019/03/22 10:37:50",
endBlockNumber: "2019/03/22 10:38:15",
type: 3
}
]
let resultData = await Controller.getsDataForspecifiedTimeAtAddressDatasmore(requestData);
ctx.body = resultData;
});
router.post('/getTokenTransactionDRCone', async (ctx) => {
let res = ctx.request.body;
console.log("post getTokenTransactionDRCone");
let data = {
url: web3_sk,
acount: "0x66A4F55B53Cfd0563a16F40BE7EDF8A07796F692",
start: "4892143",
end: "4892329"
};
let accoutData = await Controller.getTokenmyAccountdata(data.url, data.acount, data.start, data.end);
ctx.body = accoutData;
});
router.post('/gettoken20dan', async (ctx) => {
console.log(ctx.request.body);
let res = ctx.request.body;
console.log("post parmas is ok ");
let resData = {
url: web3_sk,
myAcount: "0x66A4F55B53Cfd0563a16F40BE7EDF8A07796F692",
data: [
{startBlockNumber: "4892325", endBlockNumber: "4892329"},
{startBlockNumber: "4892325", endBlockNumber: "4892329"},
{startBlockNumber: "4892325", endBlockNumber: "4892329"}
]
};
let accoutdata = await Controller.getTokenmyAccountdatamany(resData.url, resData.myAcount, resData.data);
ctx.body = accoutdata;
});
router.post('/gettoken20mar', async (ctx) => {
console.log(ctx.request.body);
let res = ctx.request.body;
console.log("post parmas is ok ");
let data = [
{
url: web3_sk,
acount: "0x66A4F55B53Cfd0563a16F40BE7EDF8A07796F692",
start: "4892143",
end: "4892329"
},
{
url: web3_sk,
acount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
start: "4892325",
end: "4892329"
},
{
url: web3_sk,
acount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
start: "4892325",
end: "4892329"
}
]
let accoutdata = await Controller.getTokenmyAccountdatamore(data);
ctx.body = accoutdata;
});
router.post('/gettimetoken20', async (ctx) => {
let res = ctx.request.body;
let resposeData = {
myAcount: "0x66A4F55B53Cfd0563a16F40BE7EDF8A07796F692",
startTime: "2019/01/25 17:35:20",
endTime: "2019/01/25 18:21:32"
}
let accoutdata = await Controller.getTimeTokenmyAccountdata(resposeData);
ctx.body = accoutdata;
});
router.post('/gettoken20dan', async (ctx) => {
console.log(ctx.request.body);
let res = ctx.request.body;
console.log("post parmas is ok ");
let data = [
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
},
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
},
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
}
]
let resposeData = {
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
list: [
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
},
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
},
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
}
]
}
let accoutdata = await Controller.getTimeTokenmyAccountdatamany(resposeData);
ctx.body = accoutdata;
});
router.post('/gettoken20mar', async (ctx) => {
console.log(ctx.request.body);
let res = ctx.request.body;
console.log("post parmas is ok ");
let data = [
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
},
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
},
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
}
]
let accoutdata = await Controller.getTimeTokenmyAccountdatamore(data);
ctx.body = accoutdata;
});
router.post('/TestgetQueryConcurrency', async (ctx) => {
console.log(ctx.request.body);
let res = ctx.request.body;
console.log("post parmas is ok ");
let data = [
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
},
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
},
{
myAcount: "0x32cda8ca0a0ffa4cb7f40ccc33e007950d96a34f",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:37:50"
}
]
let accoutdata = await Controller.testgetTimeTokenmyAccountdatamore(data);
ctx.body = accoutdata;
});
router.post('/TestgetQueryyitai', async (ctx) => {
let res = ctx.request.body;
let requestData = [
{
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:38:15",
type: 3
},
{
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:38:15",
type: 3
},
{
myAcount: "0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
startTime: "2019/03/22 10:37:50",
endTime: "2019/03/22 10:38:15",
type: 3
}
]
console.time('showColumnInfo');
let resultData = await Controller.testytaigetTimeTokenmyAccountdatamore(requestData);
console.timeEnd('showColumnInfo');
console.log("前台的数据是", resultData);
ctx.body = resultData;
});
router.post('/getOneBlock', async (ctx) => {
let data = ctx.request.body;
console.time('showColumnInfo');
let resultData = await Common.getOneBlock(data.id);
console.timeEnd('showColumnInfo');
console.log("前台的数据是", resultData);
ctx.body = resultData;
});
router.post('/setFabricdata', async (ctx) => {
let data = ctx.request.body;
let arr = [];
let data1 = {
url: "http://localhost:10081/chain/uploadData",
param: {
"datatype": "1",
"data": {
"gradeid": "code00020",
"gradeTxhash": "code00001",
"grademyAcount": "code00001",
"gradeform": "code00001",
"gradeto": "code00001",
"gradenonce": "code00001",
"gradeblockhash": "code00001",
"gradeblocknumber": "code00001",
"gradevalue": "code00001",
"gradeprice": "code00001",
"gradegas": "code00001",
"gradeinput": "code00001",
"gradeipfs": "code00001"
},
"datatime": "2019-09-23"
}
}
let res = await Http_service.postFormJson(data1.url, data1.param);
ctx.body = res;
});
router.post('/getTokenRank', async (ctx) => {
let resposeData = {
myAcount: "0x66A4F55B53Cfd0563a16F40BE7EDF8A07796F692",
startTime: "2019/01/25 17:35:20",
endTime: "2019/01/25 18:21:32"
}
let accoutdata = await Controller.getTokenRank(resposeData);
ctx.body = accoutdata;
});
router.post('/getThreeData', async (ctx) => {
let res = ctx.request.body;
let resposeData = {
myAcount: "0x66A4F55B53Cfd0563a16F40BE7EDF8A07796F692",
startTime: "2019/01/25 17:35:20",
endTime: "2019/01/25 18:21:32",
type:3
}
let accoutdata = await Controller.getTimeTokenthreeAccountdata(resposeData);
ctx.body = accoutdata;
});
var Fabric_service = {
setData_addSauryData: async (data) => {
},
setData_addSauryData: async () => {
}
}
var Http_service = {
postForm: (url, form, callback) => {
let header = Http_service.getHeader();
let option = {
json: true,
header: header,
body: form
};
request.post(url, option, (error, response, body) => {
Http_service.resultFunction(callback, error, response, body);
});
},
postFormJson: async (url, form) => {
return new Promise((resolve, reject) => {
let header = Http_service.getHeader();
let option = {
url: url,
method: "POST",
json: true,
headers: header,
body: form
};
request(option, (error, response, body) => {
if (!error && response.statusCode === 200) {
resolve({success: true, msg: body});
} else {
console.log('request is error', error);
reject("err");
}
});
});
},
resultFunction: (callback, error, response, body) => {
if (!error && response.statusCode === 200) {
callback({success: true, msg: body});
console.log('request is success ');
return callback;
} else {
console.log('request is error', error);
callback({success: false, msg: error});
return callback;
}
},
getHeader: () => {
return {
"content-type": "application/json"
}
}
}
var Controller = {
getmyAccountdata: async (web3, myaccount, startBlockNumber, endBlockNumber) => {
let resData = {
myaddress: "",
num: 0,
list: []
}
if (endBlockNumber == null) {
endBlockNumber = await web3.eth.blockNumber;
console.log("Using endBlockNumber: " + endBlockNumber);
}
if (startBlockNumber == null) {
startBlockNumber = endBlockNumber - 1000;
console.log("Using startBlockNumber: " + startBlockNumber);
}
console.log("Searching for transactions to/from account \""
+ myaccount + "\" within blocks "
+ startBlockNumber
+ " and "
+ endBlockNumber);
resData.myaddress = myaccount;
for (var i = startBlockNumber; i <= endBlockNumber; i++) {
if (i % 1000 == 0) {
console.log("Searching block " + i);
}
var block = await web3.eth.getBlock(i, true);
if (block != null && block.transactions != null) {
block.transactions.forEach(function (e) {
if (myaccount == "*" || myaccount == e.from || myaccount == e.to) {
let rowdata1 = {
"txhash": e.hash,
"nonce": e.nonce,
"blockHash": e.blockHash,
"blockNumber": e.blockNumber,
"transactionIndex": e.transactionIndex,
"from": e.from,
"to": e.to,
"value": e.value,
"time": Common.timeConverter(block.timestamp) + " " + new Date(block.timestamp * 1000).toGMTString(),
"gasPrice": e.gasPrice,
"gas": e.gas,
"input": e.input
}
console.log("time shjijianchuo :::====", block.timestamp);
let row = {"from": e.from, "to": e.to, "row": rowdata1}
resData.list.push(row)
resData.num += 1;
}
});
}
}
console.log("cort is ok ")
return resData;
},
getmyAccountdata1: async (web3, myaccount, startBlockNumber, endBlockNumber, type) => {
let resData = {
myaddress: "",
num: 0,
list: []
}
if (endBlockNumber == null) {
endBlockNumber = await web3.eth.blockNumber;
console.log("Using endBlockNumber: " + endBlockNumber);
}
if (startBlockNumber == null) {
startBlockNumber = endBlockNumber - 1000;
console.log("Using startBlockNumber: " + startBlockNumber);
}
console.log("Searching for transactions to/from account \""
+ myaccount + "\" within blocks "
+ startBlockNumber
+ " and "
+ endBlockNumber);
resData.myaddress = myaccount;
if (type == 1) {
console.log("type=>1")
for (var i = startBlockNumber; i <= endBlockNumber; i++) {
if (i % 1000 == 0) {
console.log("Searching block " + i);
}
var block = await web3.eth.getBlock(i, true);
if (block != null && block.transactions != null) {
block.transactions.forEach(function (e) {
if (myaccount == "*" || myaccount == e.from) {
let rowdata1 = {
"txhash": e.hash,
"nonce": e.nonce,
"blockHash": e.blockHash,
"blockNumber": e.blockNumber,
"transactionIndex": e.transactionIndex,
"from": e.from,
"to": e.to,
"value": e.value,
"time": Common.timeConverter(block.timestamp) + " " + new Date(block.timestamp * 1000).toGMTString(),
"gasPrice": e.gasPrice,
"gas": e.gas,
"input": e.input
}
let row = {"from": e.from, "to": e.to, "row": rowdata1}
resData.list.push(row)
resData.num += 1;
}
});
}
}
} else if (type == 2) {
for (var i = startBlockNumber; i <= endBlockNumber; i++) {
if (i % 1000 == 0) {
console.log("Searching block " + i);
}
var block = await web3.eth.getBlock(i, true);
if (block != null && block.transactions != null) {
block.transactions.forEach(function (e) {
if (myaccount == "*" || myaccount == e.to) {
let rowdata1 = {
"txhash": e.hash,
"nonce": e.nonce,
"blockHash": e.blockHash,
"blockNumber": e.blockNumber,
"transactionIndex": e.transactionIndex,
"from": e.from,
"to": e.to,
"value": e.value,
"time": Common.timeConverter(block.timestamp) + " " + new Date(block.timestamp * 1000).toGMTString(),
"gasPrice": e.gasPrice,
"gas": e.gas,
"input": e.input
}
let row = {"from": e.from, "to": e.to, "row": rowdata1}
resData.list.push(row)
resData.num += 1;
}
});
}
}
} else if (type == 3) {
for (var i = startBlockNumber; i <= endBlockNumber; i++) {
if (i % 1000 == 0) {
console.log("Searching block " + i);
}
var block = await web3.eth.getBlock(i, true);
if (block != null && block.transactions != null) {
block.transactions.forEach(function (e) {
if (myaccount == "*" || myaccount == e.from || myaccount == e.to) {
let rowdata1 = {
"txhash": e.hash,
"nonce": e.nonce,
"blockHash": e.blockHash,
"blockNumber": e.blockNumber,
"transactionIndex": e.transactionIndex,
"from": e.from,
"to": e.to,
"value": e.value,
"time": Common.timeConverter(block.timestamp) + " " + new Date(block.timestamp * 1000).toGMTString(),
"gasPrice": e.gasPrice,
"gas": e.gas,
"input": e.input
}
let row = {"from": e.from, "to": e.to, "row": rowdata1}
resData.list.push(row)
resData.num += 1;
}
});
}
}
}
console.log("cort is ok ")
return resData;
},
queryBatchProcessingTaifangAddress: async (data) => {
let resultData =
{
myAcount: [],
num: 0,
list: []
}
console.log("data length is ====", data.length);
if (data.length != 0) {
for (var i = 0; i < data.length; i++) {
console.log("循环次数是:", i);
if (data[i].endBlockNumber == null) {
data[i].endBlockNumber = await data[i].web3Url.eth.blockNumber;
console.log("Using endBlockNumber: " + endBlockNumber);
}
if (data[i].startBlockNumber == null) {
data[i].startBlockNumber = data[i].endBlockNumber - 1000;
console.log("Using startBlockNumber: " + data[i].startBlockNumber);
}
console.log("Searching for transactions to/from account \""
+ data[i].myAcount + "\" within blocks "
+ data[i].startBlockNumber
+ " and "
+ data[i].endBlockNumber);
resultData.myAcount.push(data[i].myAcount);
let rowsData = {
myAcount: data[i].myAcount,
num: 0,
rowData: []
}
for (var j = data[i].startBlockNumber; j <= data[i].endBlockNumber; j++) {
console.log("区块循环次数:", i, j)
resultData.num += 1;
if (j % 1000 == 0) {
console.log("Searching block " + i);
}
var block = await data[i].web3Url.eth.getBlock(j, true);
if (block != null && block.transactions != null) {
switch (data[i].type) {
case 1:
if (Common.QuerySourceDataInformationOnly(data[i], block).num != 0) {
rowsData.rowData.push(Common.QuerySourceDataInformationOnly(data[i], block));
rowsData.num += 1;
}
console.log("case 1");
break;
case 2:
if (Common.QueryDestinationDataInformationOnly(data[i], block).num != 0) {
rowsData.rowData.push(Common.QueryDestinationDataInformationOnly(data[i], block));
rowsData.num += 1;
}
console.log("case 2");
break;
case 3:
if (Common.QueryOnlySourceDestinationDataInformation(data[i], block).num != 0) {
rowsData.rowData.push(Common.QueryOnlySourceDestinationDataInformation(data[i], block));
rowsData.num += 1;
}
console.log("case 3");
break;
default:
console.log("default");
break;
}
}
}
resultData.list.push(rowsData);
console.log("cort is ok ", i);
}
}
return resultData;
},
queryNumberIntervalInformationAddress: async (data) => {
let resultData =
{
myAcount: [],
num: 0,
list: []
}
console.log("data length is ====", data.blockInterval);
if (data.length != 0) {
resultData.myAcount.push(data.myAcount);
var myAcout = {
myAcount: data.myAcount
}
for (var i = 0; i < data.blockInterval.length; i++) {
console.log("循环次数是:", i);
if (data.blockInterval[i].endBlockNumber == null) {
data[i].endBlockNumber = await data[i].web3Url.eth.blockNumber;
console.log("Using endBlockNumber: " + endBlockNumber);
}
if (data.blockInterval[i].startBlockNumber == null) {
data[i].startBlockNumber = data[i].endBlockNumber - 1000;
console.log("Using startBlockNumber: " + data[i].startBlockNumber);
}
console.log("Searching for transactions to/from account \""
+ data.blockInterval[i].myAcount + "\" within blocks "
+ data.blockInterval[i].startBlockNumber
+ " and "
+ data.blockInterval[i].endBlockNumber);
let rowsData = {
num: 0,
rowData: []
}
for (var j = data.blockInterval[i].startBlockNumber; j <= data.blockInterval[i].endBlockNumber; j++) {
console.log("区块循环次数:", i, j)
resultData.num += 1;
if (j % 1000 == 0) {
console.log("Searching block " + i);
}
var block = await data.web3Url.eth.getBlock(j, true);
if (block != null && block.transactions != null) {
switch (data.blockInterval[i].type) {
case 1:
if (Common.QuerySourceDataInformationOnly(myAcout, block).num != 0) {
rowsData.rowData.push(Common.QuerySourceDataInformationOnly(myAcout, block));
rowsData.num += 1;
}
console.log("case 1");
break;
case 2:
if (Common.QueryDestinationDataInformationOnly(myAcout, block).num != 0) {
rowsData.rowData.push(Common.QueryDestinationDataInformationOnly(myAcout, block));
rowsData.num += 1;
}
console.log("case 2");
break;
case 3:
if (Common.QueryOnlySourceDestinationDataInformation(myAcout, block).num != 0) {
rowsData.rowData.push(Common.QueryOnlySourceDestinationDataInformation(myAcout, block));
rowsData.num += 1;
}
console.log("case 3");
break;
default:
console.log("default");
break;
}
}
}
resultData.list.push(rowsData);
console.log("cort is ok ", i);
}
}
return resultData;
},
getsDataForspecifiedTimeAtAddressData: async (data) => {
console.log("二分查询开始");
let maxNumber = await web3_sk.eth.getBlockNumber();
console.log("最新的区块高度是:", maxNumber);
let start = await Common.indexOfSorted(maxNumber, data.startBlockNumber, 1);
let end = await Common.indexOfSorted(maxNumber, data.endBlockNumber, 2);
console.log("start:=", start);
console.log("end:=", end);
if (start.er == "min-1") {
start = start.mid;
}
if (end.er == "max+1") {
end = end.mid;
}
let resultData = Common.getsTheSpecifiedRangeBlockData(web3_sk, data.myAcount, start, end, data.type);
return resultData;
},
getsDataForspecifiedTimeAtAddressDatas: async (data) => {
let resultData = {
myAcount: data.myAcount,
num: 0,
data: []
}
let start = "";
let end = "";
let maxNumber = await web3_sk.eth.getBlockNumber();
for (var i = 0; i < data.blockList.length; i++) {
start = await Common.indexOfSorted(maxNumber, data.blockList[i].starTime, 1);
end = await Common.indexOfSorted(maxNumber, data.blockList[i].endTime, 2);
if (start.er == "min-1") {
start = start.mid;
}
if (end.er == "max+1") {
end = end.mid;
}
if (start.er == "min-1") {
start = start.mid;
}
if (end.er == "max+1") {
end = end.mid;
}
let res = Common.getsTheSpecifiedRangeBlockData(web3_sk, data.myAcount, start, end, data.type)
let timeLimit = {
startTime: data.blockList[i].starTime,
endTime: data.blockList[i].endTime,
block: {startBlockNumber: start, endBlockNumber: end},
data: res
}
resultData.data.push(timeLimit);
}
return resultData;
},
getsDataForspecifiedTimeAtAddressDatasmore: async (data) => {
let res = [];
for (var i = 0; i < data.length; i++) {
console.log("二分查询开始");
let maxNumber = await web3_sk.eth.getBlockNumber();
console.log("最新的区块高度是:", maxNumber);
let start = await Common.indexOfSorted(maxNumber, data[i].startBlockNumber, 1);
let end = await Common.indexOfSorted(maxNumber, data[i].endBlockNumber, 2);
console.log("start:=", start);
console.log("end:=", end);
if (start.er == "min-1") {
start = start.mid;
}
if (end.er == "max+1") {
end = end.mid;
}
let resultData = Common.getsTheSpecifiedRangeBlockData(web3_sk, data.myAcount, start, end, data.type);
res.push(resultData)
}
console.log("fanhiude shju shi :----------------------------", res);
return res;
},
getTokenmyAccountdata: async (web3, acount, startBlockNumber, endBlockNumber) => {
var res = {
"row": []
};
await Contract_Token.getPastEvents('Transfer', {
filter: {_from: acount},
fromBlock: startBlockNumber,
toBlock: endBlockNumber,
}, (error, events) => {
console.log("events length:", events.length);
for (let i = 0; i < events.length; i++) {
var data = events[i];
var rowdata = {
"no": i,
"address": data.address,
"blockHash": data.blockHash,
"blockNumber": data.blockNumber,
"logIndex": data.logIndex,
"removed": data.removed,
"transactionHash": data.transactionHash,
"transactionIndex": data.transactionIndex,
"id": data.id,
"returnValues": {
"0": data.returnValues["0"],
"1": data.returnValues["1"],
"2": data.returnValues["2"],
"_from": data.returnValues._from,
"_to": data.returnValues._to,
"_value": data.returnValues._value
},
"event": data.event,
"signature": data.signature
};
res.row.push(rowdata)
}
});
console.log("res=>", res)
return res;
},
getTokenmyAccountdatamany: async (web3, myAcount, res) => {
let brr = [];
for (var i = 0; i < res.length; i++) {
console.log("i:", i)
var res1 = {
"row": []
};
await Contract_Token.getPastEvents('Transfer', {
filter: {_from: myAcount},
fromBlock: res[i].startBlockNumber,
toBlock: res[i].endBlockNumber
}, (error, events) => {
console.log("events length:", events.length);
for (let i = 0; i < events.length; i++) {
var data = events[i];
var rowdata = {
"no": i,
"address": data.address,
"blockHash": data.blockHash,
"blockNumber": data.blockNumber,
"logIndex": data.logIndex,
"removed": data.removed,
"transactionHash": data.transactionHash,
"transactionIndex": data.transactionIndex,
"id": data.id,
"returnValues": {
"0": data.returnValues["0"],
"1": data.returnValues["1"],
"2": data.returnValues["2"],
"_from": data.returnValues._from,
"_to": data.returnValues._to,
"_value": data.returnValues._value
},
"event": data.event,
"signature": data.signature
};
res1.row.push(rowdata);
}
brr.push(res1);
});
}
return brr;
},
getTokenmyAccountdatamore: async (parsm) => {
let brr = [];
for (var i = 0; i < parsm.length; i++) {
var res = {
"row": []
};
await Contract_Token.getPastEvents('Transfer', {
filter: {_from: parsm[i].myAcount},
fromBlock: parsm[i].start,
toBlock: parsm[i].end,
}, (error, events) => {
console.log("events length:", events.length);
for (let i = 0; i < events.length; i++) {
var data = events[i];
var rowdata = {
"no": i,
"address": data.address,
"blockHash": data.blockHash,
"blockNumber": data.blockNumber,
"logIndex": data.logIndex,
"removed": data.removed,
"transactionHash": data.transactionHash,
"transactionIndex": data.transactionIndex,
"id": data.id,
"returnValues": {
"0": data.returnValues["0"],
"1": data.returnValues["1"],
"2": data.returnValues["2"],
"_from": data.returnValues._from,
"_to": data.returnValues._to,
"_value": data.returnValues._value
},
"event": data.event,
"signature": data.signature
};
res.row.push(rowdata)
}
});
brr.push(res);
}
return brr;
},
getTimeTokenmyAccountdata: async (data) => {
var res = {
"row": []
};
console.log("二分查询开始");
let maxNumber = await web3_sk.eth.getBlockNumber();
console.log("最新的区块高度是:", maxNumber);
let start = await Common.indexOfSorted(maxNumber, data.startTime, 1);
let end = await Common.indexOfSorted(maxNumber, data.endTime, 2);
console.log("start:=", start);
console.log("end:=", end);
if (start.er == "min-1") {
start = start.mid;
}
if (end.er == "max+1") {
end = end.mid;
}
await Contract_Token.getPastEvents('Transfer', {
filter: {_from: data.myAcount},
fromBlock: start,
toBlock: end,
}, (error, events) => {
console.log("events length:", events.length);
for (let i = 0; i < events.length; i++) {
var data = events[i];
var rowdata = {
"no": i,
"address": data.address,
"blockHash": data.blockHash,
"blockNumber": data.blockNumber,
"logIndex": data.logIndex,
"removed": data.removed,
"transactionHash": data.transactionHash,
"transactionIndex": data.transactionIndex,
"id": data.id,
"returnValues": {
"0": data.returnValues["0"],
"1": data.returnValues["1"],
"2": data.returnValues["2"],
"_from": data.returnValues._from,
"_to": data.returnValues._to,
"_value": data.returnValues._value
},
"event": data.event,
"signature": data.signature
};
res.row.push(rowdata)
}
});
console.log("res=>", res)
return res;
},
getTimeTokenmyAccountdatamany: async (data) => {
let brr = [];
for (var i = 0; i < data.list.lenth; i++) {
var res = {
"row": []
};
console.log("二分查询开始");
let maxNumber = await web3_sk.eth.getBlockNumber();
console.log("最新的区块高度是:", maxNumber);
let start = await Common.indexOfSorted(maxNumber, data.list[i].startTime, 1);
let end = await Common.indexOfSorted(maxNumber, data.list[i].endTime, 2);
console.log("start:=", start);
console.log("end:=", end);
if (start.er == "min-1") {
start = start.mid;
}
if (end.er == "max+1") {
end = end.mid;
}
await Contract_Token.getPastEvents('Transfer', {
filter: {_from: data.list[i].myAcount},
fromBlock: start,
toBlock: end,
}, (error, events) => {
console.log("events length:", events.length);
for (let i = 0; i < events.length; i++) {
var data = events[i];
var rowdata = {
"no": i,
"address": data.address,
"blockHash": data.blockHash,
"blockNumber": data.blockNumber,
"logIndex": data.logIndex,
"removed": data.removed,
"transactionHash": data.transactionHash,
"transactionIndex": data.transactionIndex,
"id": data.id,
"returnValues": {
"0": data.returnValues["0"],
"1": data.returnValues["1"],
"2": data.returnValues["2"],
"_from": data.returnValues._from,
"_to": data.returnValues._to,
"_value": data.returnValues._value
},
"event": data.event,
"signature": data.signature
};
res.row.push(rowdata)
}
});
console.log("res=>", res)
brr.push(res);
}
return res;
},
getTimeTokenmyAccountdatamore: async (data) => {
let brr = [];
for (var i = 0; i < data.lenth; i++) {
var res = {
"row": []
};
console.log("二分查询开始");
let maxNumber = await web3_sk.eth.getBlockNumber();
console.log("最新的区块高度是:", maxNumber);
let start = await Common.indexOfSorted(maxNumber, data[i].startTime, 1);
let end = await Common.indexOfSorted(maxNumber, data[i].endTime, 2);
console.log("start:=", start);
console.log("end:=", end);
if (start.er == "min-1") {
start = start.mid;
}
if (end.er == "max+1") {
end = end.mid;
}
await Contract_Token.getPastEvents('Transfer', {
filter: {_from: data[i].myAcount},
fromBlock: start,
toBlock: end,
}, (error, events) => {
console.log("events length:", events.length);
for (let i = 0; i < events.length; i++) {
var data = events[i];
var rowdata = {
"no": i,
"address": data.address,
"blockHash": data.blockHash,
"blockNumber": data.blockNumber,
"logIndex": data.logIndex,
"removed": data.removed,
"transactionHash": data.transactionHash,
"transactionIndex": data.transactionIndex,
"id": data.id,
"returnValues": {
"0": data.returnValues["0"],
"1": data.returnValues["1"],
"2": data.returnValues["2"],
"_from": data.returnValues._from,
"_to": data.returnValues._to,
"_value": data.returnValues._value
},
"event": data.event,
"signature": data.signature
};
res.row.push(rowdata)
}
});
console.log("res=>", res)
brr.push(res);
}
return res;
},
testgetTimeTokenmyAccountdatamore: async (data) => {
var brr = [];
let maxNumber = await web3_sk.eth.getBlockNumber();
await Common.getforlist(data, maxNumber)
.then(res => {
return res;
}
);
},
testytaigetTimeTokenmyAccountdatamore: async (data) => {
var brr = [];
let maxNumber = await web3_sk.eth.getBlockNumber();
console.log("查询的最新区块是:", maxNumber);
await Common.getforlist1(data, maxNumber)
.then(res1 => {
brr.push(res1)
console.log("多人多区块返回的数据是:", res1);
}
);
return brr;
},
getTokenRank: async (data) => {
let maxNumber = await web3_sk.eth.getBlockNumber();
await Contract_Token.getPastEvents('Transfer', {
filter: {},
fromBlock: "0",
toBlock: maxNumber,
}, (error, events) => {
console.log("events length:", events.length);
return events.length;
});
},
getTimeTokenthreeAccountdata: async (data) => {
console.log("二分查询开始");
var res = {
"row": []
};
console.time('showColumnInfo');
let rest = await Controller.getTimeToBlock(data);
console.timeEnd('showColumnInfo');
await Contract_Token.getPastEvents('Transfer', {
filter: {_from: data.myAcount},
fromBlock: rest[0],
toBlock: rest[1],
}, (error, events) => {
console.log("events length:", events.length);
for (let i = 0; i < events.length; i++) {
var data = events[i];
var rowdata = {
"no": i,
"address": data.address,
"blockHash": data.blockHash,
"blockNumber": data.blockNumber,
"logIndex": data.logIndex,
"removed": data.removed,
"transactionHash": data.transactionHash,
"transactionIndex": data.transactionIndex,
"id": data.id,
"returnValues": {
"0": data.returnValues["0"],
"1": data.returnValues["1"],
"2": data.returnValues["2"],
"_from": data.returnValues._from,
"_to": data.returnValues._to,
"_value": data.returnValues._value
},
"event": data.event,
"signature": data.signature
};
res.row.push(rowdata)
}
});
return res;
},
getTimeToBlock: async (data) => {
console.time()
let arr = [];
var resarr = [];
let maxNumber = await web3_sk.eth.getBlockNumber();
console.log("最新的区块高度是:", maxNumber);
arr.push(Common.indexOfSortedthree(maxNumber, data.startTime, 1)
.then(res => {
resarr.push(res);
})
.catch(e => {
resarr.push(e);
})
);
arr.push(Common.indexOfSortedthree(maxNumber, data.endTime, 2)
.then(
res => {
resarr.push(res);
}
)
.catch(e => {
console.log(e);
}));
await Promise.all(arr)
.then(res => {
console.log("查询到的开始区块号是:", resarr[0]);
console.log("查询到的结束区块号是:", resarr[1]);
if (resarr[0] == "min-1") {
resarr[0] = start.mid;
}
if (resarr[1] == "max+1") {
resarr[1] = end.mid;
}
return resarr;
})
.catch(e => {
return resarr;
console.log("getTimeTokenthreeAccountdata catch", e);
})
console.timeEnd()
return resarr;
}
};
var Common = {
init_web3: (url) => {
var web3 = new Web3(new Web3.providers.HttpProvider(url));
return web3;
},
timeConverter: (UNIX_timestamp) => {
var a = new Date(UNIX_timestamp * 1000);
var year = a.getFullYear();
var month = a.getMonth() + 1;
var date = a.getDate();
var hour = a.getHours();
var min = a.getMinutes();
var sec = a.getSeconds();
if (year < 10) {
year = "0" + year;
}
if (month < 10) {
month = "0" + month;
}
if (date < 10) {
date = "0" + date;
}
if (hour < 10) {
hour = "0" + hour;
}
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
var time = year + '/' + month + '/' + date + ' ' + hour + ':' + min + ':' + sec;
return time
},
timeFormatProcessing: (UNIX_timestamp) => {
var a = new Date(UNIX_timestamp * 1000);
var year = a.getFullYear();
var month = a.getMonth() + 1;
var date = a.getDate();
var hour = a.getHours();
var min = a.getMinutes();
var sec = a.getSeconds();
if (year < 10) {
year = "0" + year;
}
if (month < 10) {
month = "0" + month;
}
if (date < 10) {
date = "0" + date;
}
if (hour < 10) {
hour = "0" + hour;
}
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
var time = year + '/' + month + '/' + date + ' ' + hour + ':' + min + ':' + sec;
return time
},
verification: (data) => {
console.log("start verification");
switch (data) {
case data.Faddress == null || data.Faddress == undefined || data.Faddress == "":
return Error.Faddress;
case data.FstartBlockNumer == null | data.FstartBlockNumer == undefined || data.FstartBlockNumer == "":
return Error.FstartBlockNumer;
case data.FendBlockNumber == null | data.FendBlockNumber == undefined || data.FendBlockNumber == "":
return Error.FendBlockNumber;
case data.type == null | data.type == undefined || data.type == "":
return Error.type;
default:
return "OK";
}
},
queryTimeSourceDataInformationOnly: (data, block) => {
let rowsData = {
num: 0,
rowData: []
}
block.transactions.forEach((e) => {
if (data.myAcount == "*" || data.myAcount == e.from) {
let rowrmessage = {
"txhash": e.hash,
"nonce": e.nonce,
"blockHash": e.blockHash,
"blockNumber": e.blockNumber,
"transactionIndex": e.transactionIndex,
"from": e.from,
"to": e.to,
"value": e.value,
"time": Common.timeConverter(block.timestamp) + " " + new Date(block.timestamp * 1000).toGMTString(),
"gasPrice": e.gasPrice,
"gas": e.gas
}
console.log("---row", rowrmessage);
let row = {
'time': Common.timeConverter(block.timestamp),
"message": {
'from': e.from,
'to': e.to,
},
"row": rowrmessage,
"bz": ''
}
rowsData.rowData.push(row);
rowsData.num += 1;
}
});
return rowsData;
},
queryTimeDestinationDataInformationOnly: (data, block) => {
let rowsData = {
num: 0,
rowData: []
}
block.transactions.forEach((e) => {
if (data.myAcount == "*" || data.myAcount == e.to) {
let rowrmessage = {
"txhash": e.hash,
"nonce": e.nonce,
"blockHash": e.blockHash,
"blockNumber": e.blockNumber,
"transactionIndex": e.transactionIndex,
"from": e.from,
"to": e.to,
"value": e.value,
"time": Common.timeConverter(block.timestamp) + " " + new Date(block.timestamp * 1000).toGMTString(),
"gasPrice": e.gasPrice,
"gas": e.gas
}
console.log("---row", rowrmessage);
let row = {
'time': Common.timeConverter(block.timestamp),
"message": {
'from': e.from,
'to': e.to,
},
"row": rowrmessage,
"bz": ''
}
rowsData.rowData.push(row);
rowsData.num += 1;
}
});
console.log("ProcessingDataCompletion", data.myAcount);
return rowsData;
},
queryTimeOnlySourceDestinationDataInformation: (data, block) => {
let rowsData = {
num: 0,
rowData: []
}
block.transactions.forEach((e) => {
if (data.myAcount == "*" || data.myAcount == e.from || data.myAcount == e.to) {
let rowrmessage = {
"txhash": e.hash,
"nonce": e.nonce,
"blockHash": e.blockHash,
"blockNumber": e.blockNumber,
"transactionIndex": e.transactionIndex,
"from": e.from,
"to": e.to,
"value": e.value,
"time": Common.timeConverter(block.timestamp) + " " + new Date(block.timestamp * 1000).toGMTString(),
"gasPrice": e.gasPrice,
"gas": e.gas
}
let row = {
'time': Common.timeConverter(block.timestamp),
"message": {
'from': e.from,
'to': e.to,
},
"row": rowrmessage,
"bz": ''
}
console.log("查询到的对象是:", row)
rowsData.rowData.push(row);
rowsData.num += 1;
}
});
console.log("查询到的是rowsData =", rowsData);
return rowsData;
},
queryTimeOnlySourceDestinationDataInformationasync: async (data, block) => {
let rowsData = {
num: 0,
rowData: []
}
block.transactions.forEach((e) => {
if (data.myAcount == "*" || data.myAcount == e.from || data.myAcount == e.to) {
let rowrmessage = {
"txhash": e.hash,
"nonce": e.nonce,
"blockHash": e.blockHash,
"blockNumber": e.blockNumber,
"transactionIndex": e.transactionIndex,
"from": e.from,
"to": e.to,
"value": e.value,
"time": Common.timeConverter(block.timestamp) + " " + new Date(block.timestamp * 1000).toGMTString(),
"gasPrice": e.gasPrice,
"gas": e.gas
}
let row = {
'time': Common.timeConverter(block.timestamp),
"message": {
'from': e.from,
'to': e.to,
},
"row": rowrmessage,
"bz": ''
}
console.log("查询到的对象是:", row)
rowsData.rowData.push(row);
rowsData.num += 1;
}
});
console.log("查询到的是rowsData =", rowsData);
return rowsData;
},
QuerySourceDataInformationOnly: (data, block) => {
let rowsData = {
num: 0,
rowData: []
}
block.transactions.forEach((e) => {
if (data.myAcount == "*" || data.myAcount == e.from) {
let rowrmessage = {
"txhash": e.hash,
"nonce": e.nonce,
"blockHash": e.blockHash,
"blockNumber": e.blockNumber,
"transactionIndex": e.transactionIndex,
"from": e.from,
"to": e.to,
"value": e.value,
"time": Common.timeConverter(block.timestamp) + " " + new Date(block.timestamp * 1000).toGMTString(),
"gasPrice": e.gasPrice,
"gas": e.gas,
"input": e.input
}
console.log("---row", rowrmessage);
let row = {"from": e.from, "to": e.to, "row": rowrmessage}
rowsData.rowData.push(row);
rowsData.num += 1;
}
});
console.log("ProcessingDataCompletion", data.myAcount);
return rowsData;
},
QueryDestinationDataInformationOnly: (data, block) => {
let rowsData = {
num: 0,
rowData: []
}
block.transactions.forEach((e) => {
if (data.myAcount == "*" || data.myAcount == e.to) {
let rowrmessage = {
"txhash": e.hash,
"nonce": e.nonce,
"blockHash": e.blockHash,
"blockNumber": e.blockNumber,
"transactionIndex": e.transactionIndex,
"from": e.from,
"to": e.to,
"value": e.value,
"time": Common.timeConverter(block.timestamp) + " " + new Date(block.timestamp * 1000).toGMTString(),
"gasPrice": e.gasPrice,
"gas": e.gas,
"input": e.input
}
console.log("---row", rowrmessage);
let row = {"from": e.from, "to": e.to, "row": rowrmessage}
rowsData.rowData.push(row);
rowsData.num += 1;
}
});
console.log("ProcessingDataCompletion", data.myAcount);
return rowsData;
},
QueryOnlySourceDestinationDataInformation: (data, block) => {
let rowsData = {
num: 0,
rowData: []
}
block.transactions.forEach((e) => {
if (data.myAcount == "*" || data.myAcount == e.from || data.myAcount == e.to) {
let rowrmessage = {
"txhash": e.hash,
"nonce": e.nonce,
"blockHash": e.blockHash,
"blockNumber": e.blockNumber,
"transactionIndex": e.transactionIndex,
"from": e.from,
"to": e.to,
"value": e.value,
"time": Common.timeConverter(block.timestamp) + " " + new Date(block.timestamp * 1000).toGMTString(),
"gasPrice": e.gasPrice,
"gas": e.gas,
"input": e.input
}
console.log("---row", rowrmessage);
let row = {"from": e.from, "to": e.to, "row": rowrmessage}
rowsData.rowData.push(row);
rowsData.num += 1;
}
});
console.log("ProcessingDataCompletion", data.myAcount);
return rowsData;
},
indexOfSorted: async (maxNumber, n, state) => {
var low = 0;
var high = maxNumber - 1;
var led = (low + high) / 2;
var mid = Math.ceil(led);
var i = 0;
while (high - low > 1) {
let p1 = await Common.getTimestamp(low);
let p2 = await Common.getTimestamp(high);
let p3 = await Common.getTimestamp(mid);
if (n == p1) {
console.log("返回成功1", low)
return low;
}
if (n == p2) {
console.log("返回成功2", high)
return high;
}
if (n == p3) {
console.log("返回成功3", mid)
return mid;
}
if (n > p3) {
console.log("小了");
low = mid;
let sum = (low + high) / 2;
mid = Math.ceil(sum);
console.log("转换后的数据是", mid);
}
if (n < p3) {
console.log("大了");
high = mid;
let sd = (low + high) / 2
mid = Math.ceil(sd);
console.log("转换后的数据是", mid);
}
}
if (static == 1) {
return {er: "min-1", mid: mid - 1};
} else if (static == 2) {
return {er: "max+1", mid: mid + 1};
}
},
getTimestamp: async (index) => {
var block = await web3_sk.eth.getBlock(index, false);
if (block != null && block.transactions != null) {
let time = Common.timeFormatProcessing(block.timestamp);
return time;
} else {
return "err";
}
},
getsTheSpecifiedRangeBlockData: async (web3, myaccount, startBlockNumber, endBlockNumber, type) => {
console.log("据区块范围区间查询关于某人的信息");
let brr = [];
let frr = [];
let rowsData = {
myAcount: myaccount,
blocklimited: {startNum: startBlockNumber, endNum: endBlockNumber},
num: 0,
rowData: []
}
let mydat = {
myAcount: myaccount
}
for (var i = startBlockNumber; i <= endBlockNumber; i++) {
if (i % 1000 == 0) {
}
var block = await web3.eth.getBlock(i, true);
if (block != null && block.transactions != null) {
switch (type) {
case 1:
if (Common.queryTimeSourceDataInformationOnly(mydat, block).num != 0) {
rowsData.rowData.push(Common.queryTimeSourceDataInformationOnly(mydat, block));
rowsData.num += 1;
}
break;
case 2:
if (Common.queryTimeDestinationDataInformationOnly(mydat, block).num != 0) {
rowsData.rowData.push(Common.queryTimeDestinationDataInformationOnly(mydat, block));
rowsData.num += 1;
}
console.log("case 2");
break;
case 3:
if (Common.queryTimeOnlySourceDestinationDataInformationasync(mydat, block).num != 0) {
frr.push(Common.queryTimeOnlySourceDestinationDataInformationasync(mydat, block)
.then(res => {
rowsData.rowData.push(res);
rowsData.num += 1;
})
.catch(e => {
console.log("2363", e);
}))
}
break;
default:
console.log("default");
break;
}
}
}
await Promise.all(frr).then(
res => {
console.log("2383")
}
)
.catch(e => {
console.log("2389", e);
})
console.log("区块范围区间查询关于某人的信息wanbi", rowsData);
return rowsData;
},
getFindIndividualData: async (data, maxNumber) => {
var resdata = [];
let brr = [];
let crr = [];
let krr = [];
let grr = [];
console.log("二分查询开始");
brr.push(Common.indexOfSorted(maxNumber, data.startTime, 1)
.then(res => {
crr.push(res);
})
.catch(e => {
console.log(e);
}));
brr.push(Common.indexOfSorted(maxNumber, data.endTime, 2)
.then(res => {
crr.push(res);
}).catch(e => {
console.log(e);
}));
await Promise.all(brr)
.then(res => {
console.log("查询到的开始区块号是:", crr[0]);
console.log("查询到的结束区块号是:", crr[1]);
if (crr[1] == "min-1") {
crr[1] = start.mid;
}
if (crr[2] == "max+1") {
crr[2] = end.mid;
}
grr.push(Common.getsTheSpecifiedRangeBlockData(web3_sk, data.myAcount, crr[0], crr[1], data.type)
.then(res => {
resdata.push(res);
console.log("返回2367111111", res);
})
.catch(e => {
console.log(e);
return e;
}));
})
.catch(e => {
console.log("e2542", e);
});
await Promise.all(grr)
.then(res => {
console.log("2222 2456", res);
}).catch(e => {
console.log("2459", e);
})
return resdata;
},
getforlist: async (data, maxNumber) => {
let ass = [];
let arr = [];
for (var i = 0; i < data.length; i++) {
ass.push(Common.getFindIndividualData(data[i], maxNumber)
.then(res => {
arr.push(res);
}));
}
await Promise.all(ass).then(er => {
console.log("dfadd", arr);
return arr
});
},
getforlist1: async (data, maxNumber) => {
let ass = [];
let arr = [];
for (var i = 0; i < data.length; i++) {
ass.push(Common.getFindIndividualData(data[i], maxNumber)
.then(res => {
console.log("处理单条数据 :", res)
arr.push(res);
})
.catch(e => {
console.log("e", e);
}));
}
await Promise.all(ass)
.then(er => {
console.log('所有的', arr);
return arr;
})
.catch(e => {
console.log("e", e);
});
return arr;
},
getOneBlock: async (index) => {
var block = await web3_sk.eth.getBlock(index, false);
if (block != null && block.transactions != null) {
let time = Common.timeFormatProcessing(block.timestamp);
return time;
} else {
return "err";
}
},
indexOfSortedthree: async (maxNumber, n, state) => {
var low = 0;
var high = maxNumber - 1;
var led = (low + high) / 2;
var mid = Math.ceil(led);
var i = 0;
while (high - low > 1) {
let p1 = await Common.getTimestamp(low);
let p2 = await Common.getTimestamp(high);
let p3 = await Common.getTimestamp(mid);
if (n == p1) {
return low;
}
if (n == p2) {
return high;
}
if (n == p3) {
return mid;
}
if (n > p3) {
low = mid;
let sum = (low + high) / 2;
mid = Math.ceil(sum);
}
if (n < p3) {
high = mid;
let sd = (low + high) / 2
mid = Math.ceil(sd);
}
}
if (static == 1) {
return {er: "min-1", mid: mid - 1};
} else if (static == 2) {
return {er: "max+1", mid: mid + 1};
}
},
getsTheSpecifiedRangeBlockDatathree: async (web3, myaccount, startBlockNumber, endBlockNumber, type) => {
console.log("据区块范围区间查询关于某人的信息");
let brr = [];
let frr = [];
let rowsData = {
myAcount: myaccount,
blocklimited: {startNum: startBlockNumber, endNum: endBlockNumber},
num: 0,
rowData: []
}
let mydat = {
myAcount: myaccount
}
for (var i = startBlockNumber; i <= endBlockNumber; i++) {
if (i % 1000 == 0) {
}
var block = await web3.eth.getBlock(i, true);
if (block != null && block.transactions != null) {
switch (type) {
case 1:
if (Common.queryTimeSourceDataInformationOnly(mydat, block).num != 0) {
rowsData.rowData.push(Common.queryTimeSourceDataInformationOnly(mydat, block));
rowsData.num += 1;
}
break;
case 2:
if (Common.queryTimeDestinationDataInformationOnly(mydat, block).num != 0) {
rowsData.rowData.push(Common.queryTimeDestinationDataInformationOnly(mydat, block));
rowsData.num += 1;
}
console.log("case 2");
break;
case 3:
if (Common.queryTimeOnlySourceDestinationDataInformationasync(mydat, block).num != 0) {
frr.push(Common.queryTimeOnlySourceDestinationDataInformationasync(mydat, block)
.then(res => {
rowsData.rowData.push(res);
rowsData.num += 1;
})
.catch(e => {
console.log("2363", e);
}))
}
break;
default:
console.log("default");
break;
}
}
}
await Promise.all(frr).then(
res => {
console.log("2383")
}
)
.catch(e => {
console.log("2389", e);
})
console.log("区块范围区间查询关于某人的信息wanbi", rowsData);
return rowsData;
},
getOneBlockTranthree:async(index,myAcout,type)=> {
if (index % 1000 == 0) {
console.log("Searching block " + i);
}
let brr = [];
let arr = [];
let crr = [];
var sk;
brr.push(web3_sk.eth.getBlock(index, true)
.then(res => {
console.log("111")
sk=res;
})
.catch(err=>{
sk="1123233"
}))
await Promise.all(brr);
console.log("查询一个区块的数据")
if (sk != null && sk.transactions != null) {
crr.push(Common.getOneBlockMyAccount(sk,myAcout,type)
.then(res=>{
arr.push(res);
})
.catch(err=>{
arr.push(err);
})) ;
}
await Promise.all(crr);
console.log("keyi")
return arr;
},
getOneBlockMyAccount:async (data,myAcount,type)=>{
let arr = [];
let mydat = {
myAcount: myAcount
}
let rowsData = {
myAcount: myAcount,
num: 0,
rowData: []
}
switch (type) {
case 1:
if (Common.queryTimeSourceDataInformationOnly(mydat, data).num != 0) {
arr.push(Common.queryTimeSourceDataInformationOnly(mydat, data)
.then(res => {
rowsData.rowData.push(res);
rowsData.num += 1;
})
.catch(e => {
console.log("2363", e);
}));
}
break;
case 2:
if (Common.queryTimeDestinationDataInformationOnly(mydat, data).num != 0) {
arr.push(Common.queryTimeDestinationDataInformationOnly(mydat, data)
.then(res => {
rowsData.rowData.push(res);
rowsData.num += 1;
})
.catch(e => {
console.log("2363", e);
}));
}
console.log("case 2");
break;
case 3:
if (Common.queryTimeOnlySourceDestinationDataInformationasync(mydat, data).num != 0) {
arr.push(Common.queryTimeOnlySourceDestinationDataInformationasync(mydat, data)
.then(res => {
rowsData.rowData.push(res);
rowsData.num += 1;
})
.catch(e => {
console.log("2363", e);
}));
}
break;
default:
console.log("default");
break;
}
await Promise.all(arr);
console.log("3333",arr)
return rowsData;
}
}
var Web3JS = {
init_web3: () => {
const url = "https://ropsten.infura.io/v3/ee23e77aa14846d88eb5cad3d59e37f2";
web3_sk = new Web3(new Web3.providers.HttpProvider(url));
console.log("web3=>", web3_sk.version);
web3_sk.eth.defaultAccount = '0x38a8DC14edE1DEf9C437bB3647445eEec06fF105';
},
getabitest: () => {
},
getContract: () => {
let Abi_Token = token_abi;
let Address_Token = token_address.token.address;
Contract_Token = web3_sk.eth.Contract(Abi_Token, Address_Token, {
from: '0x38a8DC14edE1DEf9C437bB3647445eEec06fF105',
gasPrice: '2000000000'
});
console.log("Token合约实例完成");
}
};
var Error = {
Faddress: {
status: "nil Faddress",
msg: "Faddress is nil"
},
FstartBlockNumer: {
status: "nil FstartBlockNumer",
msg: "FstartBlockNumer is nil"
},
FendBlockNumber: {
status: "nil FendBlockNumber",
msg: "FendBlockNumber is nil"
},
lowBalance: {
status: "nil Faddress",
msg: "Faddress is nil"
},
}
Web3JS.init_web3();
Web3JS.getContract();
app.use(cors({
origin: function (ctx) {
return "*";
},
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
maxAge: 5,
credentials: true,
allowMethods: ['GET', 'POST', 'DELETE'],
allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}));
app.use(bodyParser());
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);
console.log("server is starting......");