Example #1
0
def get_contract_from_blockchain(address):
    key = get_api_key()
    api = Contract(address=address, api_key=key)
    sourcecode = api.get_sourcecode()

    assert len(sourcecode) == 1, f"no results found for {address}"
    assert len(sourcecode[0]["SourceCode"]) > 0, f"no sourcecode found for {address}"

    if sourcecode[0]["SourceCode"][0] == "{":
        # Multiple files
        file_mapping = {}
        src_path = f"{contract_dir}/contract_{address}/"
        if not os.path.exists(src_path):
            os.makedirs(src_path)
        src = sourcecode[0]["SourceCode"][1:-1]
        src = json.loads(src)
        for filepath, file in src["sources"].items():
            filename = os.path.basename(filepath)
            file_mapping[filepath] = filename
            with open(f"{src_path}{filename}", "w") as f:
                f.write(file["content"])
        # save the mappings
        with open(os.path.join(src_path, ".mapping"), "w+") as f:
            f.write(json.dumps(file_mapping))
    else:
        # Multiple files
        src_path = f"{contract_dir}/contract_{address}.sol"
        with open(src_path, "w") as c_file:
            c_file.write(sourcecode[0]["SourceCode"])

    return src_path, sourcecode[0]
def get_contract_from_blockchain(address, key_file=None):

    key = get_api_key(key_file)
    api = Contract(address=address, api_key=key)
    sourcecode = api.get_sourcecode()
    with open(contract_dir, "w") as contract_file:
        contract_file.write(sourcecode[0]['SourceCode'])

    return contract_dir
Example #3
0
class contracts():
    def __init__(self, key, address):
        # Setup api for single address
        self.key = key
        self.address = address
        self.api = Contract(address=address, api_key=key)

    def get_contract(self):
        # cannot transfer to DataFrame
        return self.api.get_abi()

    def get_sourcecode(self):
        sourcecode = self.api.get_sourcecode()
        return pd.DataFrame(sourcecode)
Example #4
0
def get_sourcecode_from_address(address):
    """
    Return sourcecode for address

    See: https://git.io/fjbQW
         https://cloud.google.com/docs/authentication/getting-started
         https://sebs.github.io/etherscan-api/
    """
    api_key = get_etherscan_api_key()
    api = Contract(address=address,
                   api_key=api_key)
    try:
        sourcecode = api.get_sourcecode()
    except:
        logger.warning('Empty Response. Try again later.')
        return None
    else:
        if not sourcecode[0]['SourceCode']:
            return None
        if sourcecode[0]['ABI'] == 'Contract source code not verified':
            return None
        return sourcecode
from etherscan.contracts import Contract
import json

with open('../../api_key.json', mode='r') as key_file:
    key = json.loads(key_file.read())['key']

address = '0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359'

api = Contract(address=address, api_key=key)
sourcecode = api.get_sourcecode()
# TODO: make this return something pretty
print(sourcecode[0]['SourceCode'])
Example #6
0
print(uncles['uncles'])

#Stats
api4 = stats.Stats(api_key=key)
total_supply = api4.get_total_ether_supply
print('total supply:', total_supply)

last_price = api4.get_ether_last_price()
print('last price:', last_price)

#Contracts
api5 = Contract(address=address, api_key=key)
#abi = api5.get_abi()
#print('abi:',abi)

sourcecode = api5.get_sourcecode()
print(sourcecode[0]['SourceCode'])

#Block
#api7 = Blocks(api_key=key)
#reward = api7.get_block_reward(2165403)
#print(reward)

#Transactions
#api6 = transactions.Transactions(api_key=key)
#TX_HASH = '0x15f8e5ea1079d9a0bb04a4c58ae5fe7654b5b2b4463375ff7ffb490aa0032f3a'
#status = api6.get_status(tx_hash=TX_HASH)
#print ('status:', status)

#TX_HASH = '0x513c1ba0bebf66436b5fed86ab668452b7805593c05073eb2d51d3a52f480a76'
#receipt_status = api6.get_tx_receipt_status(tx_hash=TX_HASH)
Example #7
0
 def get_contract_source(self, address):
     api = Contract(address=address, api_key=self.api_key)
     sourcecode = api.get_sourcecode()
     return sourcecode[0]['SourceCode']
Example #8
0
 def get_resource_code(self, sc_addr):
     api = Contract(address=str(sc_addr), api_key=self.key)
     sourcecode = api.get_sourcecode()
     return sourcecode[0]['SourceCode']
api = EtherscanAPI()
API_KEY = api.key
api._create_data_folder(DATA_DIR)

def save_json(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 save_sol(data, filename, save):
    if save:
        filepath = os.path.join(DATA_DIR, filename)
        with open(filepath, 'w') as sol_file:
            sol_file.write(data)

if __name__=='__main__':
    test_address = '0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359'

    contract = Contract(address=test_address, api_key=API_KEY)
    abi = contract.get_abi()
    js = json.loads(abi)
    abi_filename = f'abi-address-{test_address}.json'
    save_json(js, abi_filename, save=True)

    sourcecode = contract.get_sourcecode()
    sourcecode_filename = f'contract-sourcecode-address-{test_address}.sol'
    save_sol(sourcecode[0]['SourceCode'], sourcecode_filename, save=True)