|
| 1 | +from .client import Client |
| 2 | +import re |
| 3 | + |
| 4 | + |
| 5 | +class Account(Client): |
| 6 | + def __init__(self, address=Client.dao_address, api_key='YourApiKeyToken'): |
| 7 | + Client.__init__(self, address=address, api_key=api_key) |
| 8 | + self.url_dict[self.MODULE] = 'account' |
| 9 | + |
| 10 | + def get_balance(self): |
| 11 | + self.url_dict[self.ACTION] = 'balance' |
| 12 | + self.url_dict[self.TAG] = 'latest' |
| 13 | + self.build_url() |
| 14 | + req = self.connect() |
| 15 | + return req['result'] |
| 16 | + |
| 17 | + def get_balance_multiple(self): |
| 18 | + self.url_dict[self.ACTION] = 'balancemulti' |
| 19 | + self.url_dict[self.TAG] = 'latest' |
| 20 | + self.build_url() |
| 21 | + req = self.connect() |
| 22 | + return req['result'] |
| 23 | + |
| 24 | + def get_transaction_page(self, page=1, offset=10000, sort='asc', internal=False) -> list: |
| 25 | + """ |
| 26 | + Get a page of transactions, each transaction returns list of dict with keys: |
| 27 | + nonce |
| 28 | + hash |
| 29 | + cumulativeGasUsed |
| 30 | + gasUsed |
| 31 | + timeStamp |
| 32 | + blockHash |
| 33 | + value (in wei) |
| 34 | + input |
| 35 | + gas |
| 36 | + isInternalTx |
| 37 | + contractAddress |
| 38 | + confirmations |
| 39 | + gasPrice |
| 40 | + transactionIncex |
| 41 | + to |
| 42 | + from |
| 43 | + isError |
| 44 | + blockNumber |
| 45 | +
|
| 46 | + sort options: |
| 47 | + 'asc' -> ascending order |
| 48 | + 'des' -> descending order |
| 49 | +
|
| 50 | + internal options: |
| 51 | + True -> Gets the internal transactions of a smart contract |
| 52 | + False -> (default) get normal external transactions |
| 53 | + """ |
| 54 | + if internal: |
| 55 | + self.url_dict[self.ACTION] = 'txlistinternal' |
| 56 | + else: |
| 57 | + self.url_dict[self.ACTION] = 'txlist' |
| 58 | + self.url_dict[self.PAGE] = str(page) |
| 59 | + self.url_dict[self.OFFSET] = str(offset) |
| 60 | + self.url_dict[self.SORT] = sort |
| 61 | + self.build_url() |
| 62 | + req = self.connect() |
| 63 | + return req['result'] |
| 64 | + |
| 65 | + def get_all_transactions(self, offset=10000, sort='asc', internal=False) -> list: |
| 66 | + if internal: |
| 67 | + self.url_dict[self.ACTION] = 'txlistinternal' |
| 68 | + else: |
| 69 | + self.url_dict[self.ACTION] = 'txlist' |
| 70 | + self.url_dict[self.PAGE] = str(1) |
| 71 | + self.url_dict[self.OFFSET] = str(offset) |
| 72 | + self.url_dict[self.SORT] = sort |
| 73 | + self.build_url() |
| 74 | + |
| 75 | + trans_list = [] |
| 76 | + while True: |
| 77 | + self.build_url() |
| 78 | + req = self.connect() |
| 79 | + if "No transactions found" in req['message']: |
| 80 | + print("Total number of transactions: {}".format(len(trans_list))) |
| 81 | + self.page = '' |
| 82 | + return trans_list |
| 83 | + else: |
| 84 | + trans_list += req['result'] |
| 85 | + # Find any character block that is a integer of any length |
| 86 | + page_number = re.findall(r'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0', self.url_dict[self.PAGE]) |
| 87 | + print("page {} added".format(page_number[0])) |
| 88 | + self.url_dict[self.PAGE] = str(int(page_number[0]) + 1) |
| 89 | + |
| 90 | + def get_blocks_mined_page(self, blocktype='blocks', page=1, offset=10000) -> list: |
| 91 | + """ |
| 92 | + Get a page of blocks mined by given address, returns list of dict with keys: |
| 93 | + blockReward (in wei) |
| 94 | + blockNumber |
| 95 | + timeStamp |
| 96 | +
|
| 97 | + blocktype options: |
| 98 | + 'blocks' -> full blocks only |
| 99 | + 'uncles' -> uncles only |
| 100 | + """ |
| 101 | + self.url_dict[self.ACTION] = 'getminedblocks' |
| 102 | + self.url_dict[self.BLOCK_TYPE] = blocktype |
| 103 | + self.url_dict[self.PAGE] = str(page) |
| 104 | + self.url_dict[self.OFFSET] = str(offset) |
| 105 | + self.build_url() |
| 106 | + req = self.connect() |
| 107 | + return req['result'] |
| 108 | + |
| 109 | + def get_all_blocks_mined(self, blocktype='blocks', offset=10000) -> list: |
| 110 | + self.url_dict[self.ACTION] = 'getminedblocks' |
| 111 | + self.url_dict[self.BLOCK_TYPE] = blocktype |
| 112 | + self.url_dict[self.PAGE] = str(1) |
| 113 | + self.url_dict[self.OFFSET] = str(offset) |
| 114 | + blocks_list = [] |
| 115 | + while True: |
| 116 | + self.build_url() |
| 117 | + req = self.connect() |
| 118 | + print(req['message']) |
| 119 | + if "No transactions found" in req['message']: |
| 120 | + print("Total number of blocks mined: {}".format(len(blocks_list))) |
| 121 | + return blocks_list |
| 122 | + else: |
| 123 | + blocks_list += req['result'] |
| 124 | + # Find any character block that is a integer of any length |
| 125 | + page_number = re.findall(r'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0', self.url_dict[self.PAGE]) |
| 126 | + print("page {} added".format(page_number[0])) |
| 127 | + self.url_dict[self.PAGE] = str(int(page_number[0]) + 1) |
| 128 | + |
| 129 | + def get_internal_by_hash(self, tx_hash=''): |
| 130 | + """ |
| 131 | + Currently not implemented |
| 132 | + :return: |
| 133 | + """ |
| 134 | + pass |
| 135 | + |
| 136 | + def update_transactions(self, address, trans): |
| 137 | + """ |
| 138 | + Gets last page of transactions (last 10k trans) and updates current trans book (book) |
| 139 | + """ |
| 140 | + pass |
0 commit comments