Created
October 15, 2022 07:33
-
-
Save leeadh/64578feb8956c0c572c225a609b80fe5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
from web3 import Web3 | |
import numpy as np | |
import json | |
from eth_abi import abi | |
import pandas as pd | |
from snowflake.connector.pandas_tools import pd_writer | |
from sqlalchemy import create_engine | |
from hexbytes import HexBytes | |
import snowflake.connector | |
ganache_url = 'HTTP://127.0.0.1:7545' | |
web3 = Web3(Web3.HTTPProvider(ganache_url)) | |
abi = json.load(open('../src/abis/Marketplace.json')) | |
contract = web3.eth.contract(address='<CONTRACT_ADDRESS>',abi=abi['abi']) | |
rows_list = [] | |
def save_transaction_data(): | |
i = 0 | |
length_blockchains=web3.eth.block_number | |
while i <= length_blockchains: | |
block = web3.eth.get_block(i) # example for a recent block | |
for tx_hash in block['transactions']: | |
dict1 = {} | |
tx = web3.eth.get_transaction(tx_hash) | |
tx_receipt = web3.eth.get_transaction_receipt(tx_hash) | |
d = dict(tx_receipt) | |
for key ,value in list(d.items()): | |
if(isinstance(value, HexBytes)): | |
d.update({key:value.hex()}) | |
elif(value is None): | |
d.update({key:"null"}) | |
elif(isinstance(value, list) and len(value)==1): | |
sub_d = dict(value[0]) | |
print("Inside ceck") | |
for key_sub, value_sub in list(sub_d.items()): | |
if(isinstance(value_sub, HexBytes)): | |
sub_d.update({key_sub:value_sub.hex()}) | |
elif(isinstance(value_sub, list) ): | |
sub_d.update({key_sub:value_sub[0].hex()}) | |
d.update({key:sub_d}) | |
save_json_data(d,'TXN_RECEIPT_DATA') | |
i += 1 | |
def save_json_data(var, table): | |
ctx = snowflake.connector.connect( | |
account='xxxxx', | |
user='xxxxx', | |
password='xxxx#', | |
database='xxxx', | |
schema='xxxx', | |
role = "xxxx", | |
warehouse = "xxxxxx" | |
) | |
cs = ctx.cursor() | |
try: | |
cs.execute("insert into " + table+ "(select PARSE_JSON('%s'))" % json.dumps(var)) | |
finally: | |
cs.close() | |
ctx.close() | |
save_transaction_data() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment