def get_stakes(): with open('config.json') as config_file: data = json.load(config_file) if "etherscan_api_token" not in data.keys( ) or data['etherscan_api_token'] == "": raise Exception("etherscan_api_token must be specified in config.json") key = data['etherscan_api_token'] stake_address = '0x370f4d7625c10499bbb9353f3e91f83a0b750bec' api = Account(address=stake_address, api_key=key) transactions = api.get_transaction_page(page=1, offset=10000, sort='des', erc20=True) total_in = 0 total_out = 0 stargates = 0 for t in transactions: if t["tokenName"] == "DADI" and t["to"] == stake_address: value = int(t["value"]) / 1e18 total_in += value if value == 5e5: stargates += 1 elif t["from"] == stake_address and t[ "to"] != "0xef45b79def79a2cd2d0c77f84fddaab8d0b8be35": total_out = value if value == 5e5: stargates -= 1 total_staked = total_in - total_out stargates_staked = stargates * 5e5 hosts_staked = total_staked - stargates_staked return total_staked, hosts_staked, stargates_staked
def watch(self): count = 0 while True: for name, address in self.address_book.items(): print(f"{count} looking through {name}'s address") acc = Account(address=address, api_key=self.key) transactions = acc.get_transaction_page(page=0, sort ='desc', erc20=True) if len(transactions) < 50: upper_range = len(transactions) else: upper_range = 50 for i in range(0,upper_range): curr_tx = transactions[i] token_name = curr_tx['tokenName'] if token_name not in self.influencer_dicts[name]: print(f"New token {token_name} found by {name}") if token_name not in self.shared_coins: self.shared_coins[token_name] = 0 discord_notif.notif(curr_tx, name, token_name, self.shared_coins[token_name], True) else: self.shared_coins[token_name] += 1 discord_notif.notif(curr_tx, name, token_name, self.shared_coins[token_name]) self.influencer_dicts[name][token_name] = True discord_notif.update_json(name, token_name, self.influencer_dicts[name], self.shared_coins) count += 1 print('waiting') time.sleep(10)
def get_transaction_page(address, page, offset, sort): api = Account(address=address, api_key=key) transactions = api.get_transaction_page(page=page, offset=offset, sort=sort) transactions = pd.DataFrame(transactions) transactions.to_csv( r'C:\Yiru Xiong-Professional\实习\CryptoAlgoWheel\S1\task3\S1_task3_accounts_transactionpage.csv', index=False)
def get_transaction_page_erc20(address, page, offset, sort, erc20): api = Account(address=address, api_key=key) transactions = api.get_transaction_page(page=1, offset=10000, sort='des', erc20=True) transactions = pd.DataFrame(transactions) transactions.to_csv( r'C:\Yiru Xiong-Professional\实习\CryptoAlgoWheel\S1\task3\pageerc20.csv', index=False)
def main(): if len(sys.argv) < 2: print("usage: " + os.path.basename(__file__) + " user_wallet") sys.exit(1) else: wallet_addy = str(sys.argv[1]) address = wallet_addy api = Account(address=wallet_addy, api_key=key) transaction_page = api.get_transaction_page(page=1, offset=10000, sort='des') pprint.pprint(transaction_page)
def checkIfRelayed(address): #check if a transcation has been sent to the consensus within the last 3 hours time_now_utc = datetime.utcnow() unixtime = calendar.timegm(time_now_utc.utctimetuple()) api = Account(address=Web3.toChecksumAddress(address), api_key="") transactions = api.get_transaction_page(page=1, offset=10, sort='des', internal=False) relayed = False for trans in transactions: if Web3.toChecksumAddress(trans['to']) == Web3.toChecksumAddress( '0x3014ca10b91cb3D0AD85fEf7A3Cb95BCAc9c0f79'): if int(trans['timeStamp']) > (unixtime - 60 * 60 * HOURS_TO_SEARCH_BACK): relayed = True break return relayed
class reader(): add = None web3 = None api = None def __init__(self, _add, _web3): self.add = _add self.web3 = _web3 with open('apiKey.json', mode='r') as key_file: key = json.loads(key_file.read())['key'] self.api = Account(address=_add, api_key=key) def read(self, _nr): transactions = self.api.get_transaction_page(page=1, offset=10000, sort='des') return transactions[_nr]
from etherscan.accounts import Account import json with open('../../api_key.json', mode='r') as key_file: key = json.loads(key_file.read())['key'] address = '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a' api = Account(address=address, api_key=key) transactions = api.get_transaction_page(page=1, offset=10000, sort='des', erc20=True) print(transactions)
def get_transaction_by_address(address, asset, page): address = address.lower() try: mongo_client.insert_address(address) except Exception as e: app_logger.error(e) txs = [ tx for tx in mongo_client.get_transfer_records(address, asset, page) ] new_txs = [] for tx in txs: if tx["txReceiptStatus"] == "0": tx["txReceiptStatus"] = "-1" tx["addressFrom"] = checksum_encode(tx["addressFrom"]) tx["addressTo"] = checksum_encode(tx["addressTo"]) new_txs.append(tx) if page != 1: return new_txs else: if new_txs: return new_txs my_account = Account(address=address, api_key=setting.API_KEY) my_account.PREFIX = setting.ETHSCAN_API_PREFIX try: res = my_account.get_transaction_page(sort="desc") except: res = None if res: tmp_list_0x = [] tmp_list_contract = [] for r in res: tmp_dict = {} input_data = r.get("input") if r.get("to") == asset and input_data[:10] == "0xa9059cbb": tmp_dict["addressFrom"] = checksum_encode(r.get("from")) tmp_dict["addressTo"] = checksum_encode(input_data[34:74]) tmp_dict["value"] = str(int(input_data[74:], 16)) tmp_dict["txId"] = r.get("hash") tmp_dict["gasUsed"] = r.get("gasUsed") tmp_dict["gasPrice"] = r.get("gasPrice") tmp_dict["gas"] = r.get("gas") tmp_dict["blockNumber"] = r.get("blockNumber") tmp_dict["blockTime"] = r.get("timeStamp") tmp_dict["asset"] = r.get("to") tmp_dict["txReceiptStatus"] = r.get( "txreceipt_status" ) if r.get("txreceipt_status") != "0" else "-1" tmp_list_contract.append(tmp_dict) if input_data == "0x" and asset == "0x00000000000000000000000000000000000000": tmp_dict["addressFrom"] = checksum_encode(r.get("from")) tmp_dict["addressTo"] = checksum_encode(r.get("to")) tmp_dict["value"] = r.get("value") tmp_dict["txId"] = r.get("hash") tmp_dict["gasUsed"] = r.get("gasUsed") tmp_dict["gasPrice"] = r.get("gasPrice") tmp_dict["gas"] = r.get("gas") tmp_dict["blockNumber"] = r.get("blockNumber") tmp_dict["blockTime"] = r.get("timeStamp") tmp_dict["asset"] = "0x00000000000000000000000000000000000000" tmp_dict["txReceiptStatus"] = r.get( "txreceipt_status" ) if r.get("txreceipt_status") != "0" else "-1" tmp_list_0x.append(tmp_dict) if asset == "0x00000000000000000000000000000000000000": tmp_list = tmp_list_0x else: tmp_list = tmp_list_contract if len(tmp_list) > 8: return tmp_list[:8] return tmp_list return []
def deployer_watcher(address, key): acc = Account(address=address, api_key=key) transactions = acc.get_transaction_page(page=1, offset=10000, sort='des', erc20=True) for transaction in transactions: if transaction['from'] == 'ADDRESS_HERE': print(transaction)
def get_single_transaction_page(): api = Account(address=ADDRESS, api_key=API_KEY) transactions = api.get_transaction_page(page=1, offset=10000, sort='des') return transactions
class Accounts(EtherscanAPI): def __init__(self, address): super().__init__() self.address = address self.api = Account(address=address, api_key=self.key) self._create_data_folder(DATA_DIR) def _data_to_df_save(self, data, filename, save=False): df = pd.DataFrame(data) df['timeStamp'] = pd.to_datetime(df['timeStamp'], unit='s') df.set_index('timeStamp', inplace=True) if save: filepath = os.path.join(DATA_DIR, filename) df.to_csv(filepath) print('Data file saved') return df def get_balance(self): balance = self.api.get_balance() return balance def get_balance_multi(self): balances = self.api.get_balance_multiple() return balances def get_block_mined(self, page=1, offset=10000, blocktype='blocks', save=False): block = self.api.get_blocks_mined_page(page=page, offset=offset, blocktype=blocktype) filename = f'{self.address}-page{page}-offset{offset}-blocks-mined.csv' blocks_df = self._data_to_df_save(block, filename, save=save) return blocks_df def get_all_blocks_mined(self, offset=10000, blocktype='blocks', save=False): blocks = self.api.get_all_blocks_mined(offset=offset, blocktype=blocktype) filename = f'{self.address}-all-blocks-mined.csv' blocks_df = self._data_to_df_save(blocks, filename, save=save) return blocks_df def get_all_transactions(self, offset=1000, sort='acs', internal=False, save=False): transactions = self.api.get_all_transactions(offset=offset, sort=sort, internal=internal) filename = f'{self.address}-all-transactions.csv' transac_df = self._data_to_df_save(transactions, filename, save=save) return transac_df def get_transaction_page(self, page=1, offset=10000, sort='asc', internal=False, erc20=False, save=True): transactions = self.api.get_transaction_page(page=page, offset=offset, sort=sort, internal=internal, erc20=erc20) transac_df = pd.DataFrame(transactions) transac_df['timeStamp'] = pd.to_datetime(transac_df['timeStamp'], unit='s') transac_df.set_index('timeStamp', inplace=True) filename = f'{self.address}-page{page}-offset{offset}-transactions.csv' transac_df = self._data_to_df_save(transactions, filename, save=save) return transac_df
from etherscan.accounts import Account import json with open('../../api_key.json', mode='r') as key_file: key = json.loads(key_file.read())['key'] address = '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a' api = Account(address=address, api_key=key) transactions = api.get_transaction_page(page=1, offset=10000, sort='des') print(transactions)
'0x83fE4c5638889318ccE79103efe3881E1EfC3DF8', '0x52e448aF9bf91916Fb7555cd59A6eB38Ba613E00', '0xD5c0e626eD1Ea60776b452C018524D374C30bC26', '0x4b11F5AcB84A4E12AD90C4253bB602Cd56Dd3a4F', '0xA79B8c2B89f3E7Eb04ead67e65bBF3fCA6d9935E', '0x847830bcf9135dc4548a64a55ca2b48a8a75c9bb', '0xD9E68069917393974A464e25F123F22C208afc1D', '0xD3db32545817a0574938Dc0ab27697699A86efed', '0x53c7a8d887d8517aa5a8b268ad48caab20a53a50', '0xa4aAA1B5f371Ac9Df6DB74b4aaAB4cAe91810ce8', '0x036BBfafae484e8964d861822C9415707b694D4E', '0x17AB69ccE747130a5faC74Cb033A3Fa9eFb26d41', '0xCdCd8B86263496270568D6CA74626038Bb6c7Edf', '0xA5aaC3ff6a6eA1134037752F8eaB76c1854c12e0', '0xcf6f8CC2B2B2f06F9Cf2c2f919BB8b128F3D0FFA', '0x63ECc6c4594171289a304FCb9e3829cC35Eb6b9C' ] transactions_all = [] api = Account(address=addresses[0:20], api_key=key) transactions = api.get_transaction_page(page=1, offset=10000, sort='asc') #print(transactions[0]) transactions_all.append(transactions) hash_list = [] for transaction in transactions: if transaction.get('input') == '0x4e7407ea': hash_list.append(transaction.get('hash')) print(hash_list)
logger.info(e) mongo_client = MongodbEth() while True: address = mongo_client.get_address() if not address: time.sleep(15) continue my_account = Account(address=address.get("address"), api_key=setting.API_KEY) my_account.PREFIX = setting.ETHSCAN_API_PREFIX try: res = my_account.get_transaction_page(sort="desc") except Exception as e: res = None if res: for r in res: tmp_dict = {} tmp_dict["txId"] = r.get("hash") tmp_dict["addressFrom"] = r.get("from") tmp_dict["gasUsed"] = r.get("gasUsed") tmp_dict["gasPrice"] = r.get("gasPrice") tmp_dict["gas"] = r.get("gas") tmp_dict["blockNumber"] = r.get("blockNumber") tmp_dict["blockTime"] = r.get("timeStamp") tmp_dict["txReceiptStatus"] = r.get("txreceipt_status")
import json with open('./api_key.json', mode='r') as key_file: key = json.loads(key_file.read())['key'] # Accounts ## Single address address = '0x9dd134d14d1e65f84b706d6f205cd5b1cd03a46b' api = Account(address=address, api_key=key) ### get balance single_bal = api.get_balance() print(single_bal) ### get transactions single_trans = api.get_transaction_page(page=1, offset=10) formatted_st = pd.DataFrame.from_dict(single_trans) formatted_st['timeStamp'] = pd.to_datetime(formatted_st['timeStamp'], unit='s') print(formatted_st.head(5)) ### get blocks mined single_bm = api.get_blocks_mined_page(page=1, offset=10) formatted_bm = pd.DataFrame.from_dict(single_bm) formatted_bm['timeStamp'] = pd.to_datetime(formatted_bm['timeStamp'], unit='s') print(formatted_bm.head(5)) ## multiple address addresses = ['0xbb9bc244d798123fde783fcc1c72d3bb8c189413', '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a'] api = Account(address=addresses, api_key=key) ### get balance mutiple_bal = api.get_balance_multiple()