遍历区块数据,查询合约交易

// 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();
})();

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值