def main(): with open('../api_key.json', mode='r') as key_file: key = json.loads(key_file.read())['key'] # get steven's contract initialization input data as a reference api = Proxies(api_key=key) transaction_1 = api.get_transaction_by_hash( tx_hash='0x5920ad6d2c1caa27dae2e85b4764a28c7665d8f4ecfc17150c0abd9f26cb2ca4') target_input_1 = transaction_1['input'] transaction_2 = api.get_transaction_by_hash( tx_hash='0x881bd88aac40ba6f49e7e6130e7bafc154a9e918b6f792742ff10cf731309cce') target_input_2 = transaction_2['input'] target_input = longestSubstringFinder(target_input_1, target_input_2) # Search over all Tx within all blocks between 6pm and 7pm local time #block = api.get_block_by_number(3638953) #print(block.keys()) hash_list = [] block_number_all = list(range(3626750,3626760)) for block_number in block_number_all: block = api.get_block_by_number(block_number) for transaction in block['transactions']: if transaction['input'][0:5960] == target_input: hash_list.append(transaction['hash']) print (hash_list)
class proxies(): def __init__(self, key): self.key = key self.api = Proxies(api_key=key) ''' def gas_price(self): return self.api.gas_price() ''' def get_block(self, number): block_number = self.api.get_block_by_number(block_number=number) return pd.DataFrame.from_dict(block_number, orient='index') def tx_count(self, number): tx_count = self.api.get_block_transaction_count_by_number( block_number=number) tx_count = [int(tx_count, 16)] return pd.DataFrame(tx_count, ['block_transaction_count_by_number']) ''' def get_code(self, address): Error: Exception has occurred: AttributeError. 'Proxies' object has no attribute 'get_code' return self.api.get_code(address = address) ''' def get_recent_block(self): block = self.api.get_most_recent_block() block = [int(block, 16)] return pd.DataFrame(block, columns=['most recent block']) ''' def value(self): Error: no attribute return self.api.get_storage_at('0x6e03d9cce9d60f3e9f2597e13cd4c54c55330cfd', 0x0) ''' def Transactions(self, number, index): return self.api.get_transaction_by_blocknumber_index( block_number=number, index=index) def transaction_hash(self, TX_HASH): transaction = self.api.get_transaction_by_hash(tx_hash=TX_HASH) return transaction def transaction_count(self, address): count = self.api.get_transaction_count(address=address) count = [int(count, 16)] return count def transaction_receipt(self, TX_HASH): receipt = self.api.get_transaction_receipt(tx_hash=TX_HASH) return receipt def uncle(self, number, index): uncel = self.api.get_uncle_by_blocknumber_index(block_number=number, index=index) return uncel
class Proxies_usage: def __init__(self, key): self.api_key = key self.api = Proxies(api_key=key) #### get information about a block by block number (and save the result in a JSON file) def block_info_by_number(self, block_number): block = self.api.get_block_by_number(block_number) with open("./block_info_by_number.json", "w") as json_file: json.dump(block, json_file) #### get the number of transactions in a block matching the given block number def block_trans_count_by_number(self, block_number): tx_count = self.api.get_block_transaction_count_by_number( block_number=block_number) return int(tx_count, 16) #### get the number of most recent block def most_recent_block(self): block = self.api.get_most_recent_block() return int(block, 16) #### get information about a transaction by block number and transaction index position (and save to a csv file) def trans_info_by_number_index(self, block_number, index): transaction = self.api.get_transaction_by_blocknumber_index( block_number=block_number, index=index) transaction = pd.DataFrame(transaction, index=[0]) transaction.to_csv("./transaction_info_by_number_index.csv", index=False) #### get the informationa bout a transaction by transaction hash (and save to a csv file) def trans_info_by_hash(self, hash): transaction = self.api.get_transaction_by_hash(tx_hash=hash) transaction = pd.DataFrame(transaction, index=[0]) transaction.to_csv("./transaction_info_by_hash.csv", index=False) #### get the number of transactions sent from an address def trans_count_from_addr(self, address): count = self.api.get_transaction_count(address) return int(count, 16) #### get (display) the receipt of a transaction by transaction hash def trans_receipt_by_hash(self, hash): receipt = self.api.get_transaction_receipt(hash) receipt = pd.DataFrame.from_dict(receipt, orient="index") display(receipt) #### get information about a uncle by block number def uncle_by_number_index(self, block_number, index="0x0"): uncles = self.api.get_uncle_by_blocknumber_index( block_number=block_number, index=index) return uncles["uncles"]
from etherscan.proxies import Proxies import json with open('../../api_key.json', mode='r') as key_file: key = json.loads(key_file.read())['key'] api = Proxies(api_key=key) transaction = api.get_transaction_by_hash( tx_hash='0x1e2910a262b1008d0616a0beb24c1a491d78771baa54a33e66065e03b1f46bc1' ) print(transaction['hash'])
from etherscan.proxies import Proxies import json with open('../../api_key.json', mode='r') as key_file: key = json.loads(key_file.read())['key'] TX_HASH = '0x1e2910a262b1008d0616a0beb24c1a491d78771baa54a33e66065e03b1f46bc1' api = Proxies(api_key=key) transaction = api.get_transaction_by_hash( tx_hash=TX_HASH) print(transaction['hash'])
def test_get_transaction_by_hash(self): api = Proxies(api_key=API_KEY) tx = api.get_transaction_by_hash(TX_HASH) print(tx) self.assertEqual(tx['blockNumber'], hex(BLOCK_NUMBER))
# get block transaction count by number tx_count = api.get_block_transaction_count_by_number(block_number='0x10FB78') print(int(tx_count, 16)) # get most recent block block = api.get_most_recent_block() print(int(block, 16)) # get transactions transaction = api.get_transaction_by_blocknumber_index(block_number='0x57b2cc', index='0x2') print(transaction['transactionIndex']) # get transactions by hash TX_HASH = '0x1e2910a262b1008d0616a0beb24c1a491d78771baa54a33e66065e03b1f46bc1' transaction = api.get_transaction_by_hash(tx_hash=TX_HASH) print(transaction['hash']) # get transaction count count = api.get_transaction_count('0x6E2446aCfcec11CC4a60f36aFA061a9ba81aF7e0') print(int(count, 16)) # get transaction receipt receipt = api.get_transaction_receipt( '0xb03d4625fd433ad05f036abdc895a1837a7d838ed39f970db69e7d832e41205d') print(receipt) # get uncle by blocknumber index uncles = api.get_uncle_by_blocknumber_index(block_number='0x210A9B', index='0x0') print(uncles['uncles'])
class Proxy(EtherscanAPI): def __init__(self): super().__init__() self.api = Proxies(api_key=self.key) self._create_data_folder(DATA_DIR) # def gas_price(self): # price = self.api.gas_price() # return price def _save_json(self, data, filename, save): if save: filepath = os.path.join(DATA_DIR, filename) with open(filepath, 'w') as json_file: json.dump(data, json_file, indent=4) def get_most_recent_block(self, save=False): block = self.api.get_most_recent_block() filename = f'most-recent-block.json' self._save_json(block, filename, save) return block def get_block_by_number(self, block_num, save=False): block = self.api.get_block_by_number(block_num) filename = f'block-{block_num}-data.json' self._save_json(block, filename, save) return block def get_uncle_by_blocknumber_index(self, block_num, index, save=False): uncles = self.api.get_uncle_by_blocknumber_index( block_number=block_num, index=index) filename = f'block-{block_num}-{index}-uncles-data.json' self._save_json(uncles, filename, save) return uncles def get_block_transaction_count_by_number(self, block_num, save=False): tx_count = self.api.get_block_transaction_count_by_number( block_number=block_num) return int(tx_count, 16) def get_transaction_by_hash(self, tx_hash, save=False): transaction = self.api.get_transaction_by_hash(tx_hash=tx_hash) filename = f'transaction-data-of-hash-{tx_hash}.json' self._save_json(transaction, filename, save) return transaction def get_transaction_by_blocknumber_index(self, block_num, index, save=False): transaction = self.api.get_transaction_by_blocknumber_index( block_number=block_num, index=index) filename = f'transaction-data-of-block-{block_num}.json' self._save_json(transaction, filename, save) return transaction def get_transaction_count(self, address, save=False): count = self.api.get_transaction_count(address) return int(count, 16) def get_transaction_receipt(self, tx_hash, save=False): receipt = self.api.get_transaction_receipt(tx_hash) filename = f'transaction-recepit-of-hash-{tx_hash}.json' self._save_json(receipt, filename, save) return receipt