Пример #1
0
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)
Пример #2
0
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"]
Пример #4
0
with open('api_key.json', mode='r') as key_file:
    key = loads(key_file.read())['key']
api = Proxies(api_key=key)

try:
    mkdir(blocks_path + str(current_dir))
except OSError:
    print('Failed to create directory %s' % blocks_path + str(current_dir))
else:
    print('Created directory %s successfully' % blocks_path + str(current_dir))

print("Downloading transactions for blocks: {} to {}".format(
    first_block_num, last_block_num))
for i in tqdm(range(4708444, last_block_num + 1)):
    try:
        block = api.get_block_by_number(i)
        minified_txs = []
        for tx in block['transactions']:
            tx_item = {}
            tx_item['hash'] = tx['hash']
            tx_item['gas'] = int(tx['gas'], 16)
            tx_item['gasPrice'] = int(tx['gasPrice'], 16) / pow(10, 9)
            minified_txs.append(tx_item)
        minified_txs.sort(key=lambda x: x['gasPrice'], reverse=True)

        with open(blocks_path + str(current_dir) + '/' + str(i) + '.json',
                  'w') as fwriter:
            dump(minified_txs, fwriter)

        files_in_dir = files_in_dir + 1
        if files_in_dir == max_files_in_dir:
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)
block = api.get_block_by_number(5747732)
print(block['number'])
Пример #6
0
 def test_get_block_by_number(self):
     api = Proxies(api_key=API_KEY)
     block = api.get_block_by_number(BLOCK_NUMBER)
     print(block)
     self.assertEqual(block['difficulty'], hex(BLOCK_DIFFICULTY))
Пример #7
0
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
Пример #8
0
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)
block = api.get_block_by_number(3638953)
print(block.keys())