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