在mongo db中插入以太坊阻止信息

时间:2018-12-07 09:13:24

标签: python-3.x mongodb ethereum web3js

我试图将主网中范围为5000000-5100000的以太坊区块信息插入 MongoDB-4 。这是我的代码,它不起作用。有人可以帮我提供代码吗?我是python的新手。

import threading
import binascii
import pymongo
import json
import sys

from web3 import Web3, HTTPProvider  
w3 = Web3(Web3.HTTPProvider("http://ipaddress:port"))  
myclient = pymongo.MongoClient("mongodb://ipaddress:port")  
mydb = myclient["TestDB06"]
mycol = mydb["Testcollection06"]

for i in range(5000000,5100000):  
    print(i)  
    transactionArray = []  
    blockResult = w3.eth.getBlock(i)  
    print(blockResult)  
    for tx in blockResult["transactions"]:  
        txResult = binascii.hexlify(tx).decode()  
        print(txResult)  
        try:  
            transactionResult = w3.getTransactionReceipt(txResult)  
            print(transactionResult)  
        except:  
            print("No Receipt")  

   transactionJson={          
      'blockHash':binascii.hexlify(transactionResult["blockHash"])
      .decode(),
      'blockNumber':transactionResult["blockNumber"],
      'contractAddress':transactionResult["contractAddress"],                  
      'cumulativeGasUsed':transactionResult["cumulativeGasUsed"],
      'from':transactionResult["from"],
      'gasUsed':transactionResult["gasUsed"],
      'logs':transactionResult["logs"],                   
      'logsBloom':binascii.hexlify(transactionResult["logsBloom"]).
        decode(),
      'root':transactionResult["root"],
      'to':transactionResult["to"],                    
      'transactionHash':binascii.hexlify
      (transactionResult["transactionHash"]).decode(),                    
       'transactionIndex':transactionResult["transactionIndex"]
      }
        transactionArray.append(blockResult)
        if(len(transactionArray)>0):
            mycol.insert_many(transactionArray)

0 个答案:

没有答案