// Part 1 Import Related Package
var Web3 = require('web3');
var mongoose = require('mongoose');
var moment = require('moment');
const startBlock = 14650515;
const endBlock = 14656856;
const ethContractSchema = mongoose.Schema({
address: String,
input: String,
//code: String,
//storage: String,
});
const EthContract = mongoose.model('ctaddr_func_425', ethContractSchema);
EthContract.addContract = function(newContract, callback) {
newContract.save(callback);
};
EthContract.updateContract = function(contractAddress, newCode, newStorage) {
EthContract.update({address: contractAddress}, { $set: {
"code": newCode,
"storage": newStorage,
}}, (err, suc) => {});
}
EthContract.checkUnique = function(contractAddress) {
let res = EthContract.find({address: contractAddress});
console.log(res);
if(res) return false;
return true;
};
async function connectDB() {
mongoose.connect('mongodb://localhost:27017/eth_ctaddrandfunc');
mongoose.connection.on('connected', () => {
console.log('MongoDB has started successfully.');
});
mongoose.connection.on('error', (err) => {
console.log('Database error' + err);
});
}
// Part Connection to Local RPC
async function connectWeb3() {
//web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/v3/daa578881869407da748a2603f5cee52"));
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8546"));
global.web3 = web3;
console.log('web3 has connected successfully.');
}
// Part Functions
async function checkIfIsContract(newAddress) {
if(newAddress == null)
return false;
var code = await web3.eth.getCode(newAddress);
// console.log(" ** CODE ** " + code);
if(code == '0x') {
// console.log('NOT CONTRACT');
return false;
}
else return true;
}
async function uniqueAddAddress(newAddress,transactionInput) {
let result = await checkIfIsContract(newAddress);
if(!result)
return;
{
// console.log(" *** [NEW CONTRACT] *** " + newAddress);
//var newCode = await web3.eth.getCode(newAddress);
//var newStorage = await web3.eth.getStorageAt(newAddress);
// input = await web3.eth.getTransaction(transactionHash);
// console.log(" ** CODE ** " + code);
if(transactionInput == '0x') {
// console.log('NOT CONTRACT');
newInput= transactionInput;
}
else {
newInput=transactionInput.slice(0,10);
}
// console.log('[NEW CONTRACT FOUNDED!]' + newAddress + ' = '+ newCode + ' = '+ newStorage);
console.log('[NEW CONTRACT FOUNDED!]' + newAddress +' '+newInput );
EthContract.addContract(new EthContract({
address: newAddress,
input: newInput,
//code: newCode,
//storage: newStorage,
}));
}
}
async function scanTheChain() {
for(let i = startBlock; i <= endBlock; i++) {
console.log('scanning the chain.');
console.log("[ " + moment().format('MMMM Do YYYY, h:mm:ss a') + " ] " + "Scanning Block " + i);
var blockInfo = await web3.eth.getBlock(i);
console.log('blockinfo.' + blockInfo);
if(!Boolean(blockInfo)){continue}
var blockTxes = blockInfo.transactions;
var blockTxCnt = await web3.eth.getBlockTransactionCount(i);
console.log("CNT => " + blockTxCnt);
for(let j = 0; j < blockTxCnt; j++) {
var thisTx = await web3.eth.getTransaction(blockTxes[j]);
const txFrom = thisTx.from;
uniqueAddAddress(txFrom,thisTx.input);
const txTo = thisTx.to;
uniqueAddAddress(txTo,thisTx.input);
}
}
}
// Part Main Function
(async function main(){
moment.locale('zh-cn');
await connectDB();
await connectWeb3();
await scanTheChain();
})();
遍历区块数据,查询合约交易
于 2022-05-16 14:55:47 首次发布