|
| 1 | +from typing import Dict |
| 2 | + |
| 3 | +from httpx import Response |
| 4 | + |
| 5 | +from ._types import Address, TXHash, IntNumber |
| 6 | +from .apis import APTTransactionsAPI, APTAccountAPI, APTGeneralAPI, APTBlockAPI, APTEventAPI, APTTablesAPI |
| 7 | +from .errors import ParamsError, RPCRequestError |
| 8 | +from .providers import BaseProvider |
| 9 | + |
| 10 | + |
| 11 | +class APTClient: |
| 12 | + def __init__(self, provider: BaseProvider): |
| 13 | + self.provider = provider |
| 14 | + |
| 15 | + # account |
| 16 | + def get_account(self, address: Address): |
| 17 | + return self.provider.get(APTAccountAPI.GET_ACCOUNT.format(address=address)) |
| 18 | + |
| 19 | + def get_account_resource(self, address: Address, resource_type: str): |
| 20 | + return self.provider.get( |
| 21 | + APTAccountAPI.GET_ACCOUNT_RESOURCE.format(address=address, resource_type=resource_type)) |
| 22 | + |
| 23 | + def get_account_resources(self, address: Address): |
| 24 | + return self.provider.get(APTAccountAPI.GET_ACCOUNT_RESOURCES.format(address=address)) |
| 25 | + |
| 26 | + def get_account_modules(self, address: Address): |
| 27 | + return self.provider.get(APTAccountAPI.GET_ACCOUNT_MODULES.format(address=address)) |
| 28 | + |
| 29 | + def get_account_module(self, address: Address, module_name: str): |
| 30 | + return self.provider.get(APTAccountAPI.GET_ACCOUNT_MODULE.format(address=address, module_name=module_name)) |
| 31 | + |
| 32 | + # useful account related method |
| 33 | + def get_account_balance(self, address: Address): |
| 34 | + # return self.get_account_resource(address, APT_COIN_STORE)['data']['coin']['value'] |
| 35 | + return self.provider.get(APTAccountAPI.GET_ACCOUNT_BALANCE.format(address=address))['data']['coin']['value'] |
| 36 | + |
| 37 | + def get_account_sequence_number(self, address: Address): |
| 38 | + return self.get_account(address)['sequence_number'] |
| 39 | + |
| 40 | + def get_account_nonce(self, address: Address): |
| 41 | + return self.get_account_sequence_number(address) |
| 42 | + |
| 43 | + # block |
| 44 | + def get_block_by_height(self, block_height: IntNumber): |
| 45 | + return self.provider.get(APTBlockAPI.GET_BLOCK_BY_HEIGHT.format(block_height=str(block_height))) |
| 46 | + |
| 47 | + def get_block_by_version(self, version: IntNumber): |
| 48 | + return self.provider.get(APTBlockAPI.GET_BLOCK_BY_VERSION.format(version=str(version))) |
| 49 | + |
| 50 | + # event |
| 51 | + def get_event_by_creation_number(self, address: Address, creation_number: int): |
| 52 | + return self.provider.get( |
| 53 | + APTEventAPI.GET_EVENT_BY_CREATION_NUMBER.format(address=address, creation_number=str(creation_number))) |
| 54 | + |
| 55 | + def get_event_by_event_handle(self, address: Address, event_handle: str): |
| 56 | + return self.provider.get( |
| 57 | + APTEventAPI.GET_EVENT_BY_EVENT_HANDLE.format(address=address, event_handle=event_handle)) |
| 58 | + |
| 59 | + # general |
| 60 | + def show_openapi_explorer(self): |
| 61 | + url = self.provider.patch_url([self.provider.base_url, APTGeneralAPI.SHOW_OPENAPI_EXPLORER]) |
| 62 | + return self.provider.client.get(url) |
| 63 | + |
| 64 | + def check_node_health(self): |
| 65 | + return self.provider.get(APTGeneralAPI.CHECK_NODE_HEALTH) |
| 66 | + |
| 67 | + check_health = check_node_health |
| 68 | + |
| 69 | + def get_ledger_info(self): |
| 70 | + return self.provider.get(APTGeneralAPI.GET_LEDGER_INFO) |
| 71 | + |
| 72 | + # tables |
| 73 | + def get_table_item(self, table_handle: str): |
| 74 | + return self.provider.post(APTTablesAPI.GET_TABLE_ITEM.format(table_handle=table_handle)) |
| 75 | + |
| 76 | + # transactions |
| 77 | + def get_transactions(self): |
| 78 | + return self.provider.get(APTTransactionsAPI.GET_TRANSACTIONS) |
| 79 | + |
| 80 | + def get_transaction_by_hash(self, txn_hash: TXHash): |
| 81 | + return self.provider.get(APTTransactionsAPI.GET_TRANSACTION_BY_HASH.format(txn_hash=txn_hash)) |
| 82 | + |
| 83 | + get_txn_by_hash = get_transaction_by_hash |
| 84 | + |
| 85 | + def get_transaction_by_version(self, txn_version: IntNumber): |
| 86 | + return self.provider.get(APTTransactionsAPI.GET_TRANSACTION_BY_VERSION.format(txn_version=str(txn_version))) |
| 87 | + |
| 88 | + def get_account_transactions(self, address): |
| 89 | + return self.provider.get(APTTransactionsAPI.GET_ACCOUNT_TRANSACTIONS.format(address=address)) |
| 90 | + |
| 91 | + def submit_transaction(self, txn_dict: Dict): |
| 92 | + return self.provider.post(APTTransactionsAPI.SUBMIT_TRANSACTION, params_dict=txn_dict) |
| 93 | + |
| 94 | + submit = submit_transaction |
| 95 | + |
| 96 | + def submit_batch_transactions(self, batch_txn_dict: Dict): |
| 97 | + return self.provider.post(APTTransactionsAPI.SUBMIT_BATCH_TRANSACTIONS, params_dict=batch_txn_dict) |
| 98 | + |
| 99 | + submit_batch = submit_batch_transactions |
| 100 | + |
| 101 | + def simulate_transaction( |
| 102 | + self, txn_dict, estimate_gas_unit_price: str = "false", |
| 103 | + estimate_max_gas_amount: str = "false", |
| 104 | + estimate_prioritized_gas_unit_price: str = "false" |
| 105 | + ): |
| 106 | + j = self.provider.post( |
| 107 | + f'{APTTransactionsAPI.SIMULATE_TRANSACTION}' |
| 108 | + f'?estimate_gas_unit_price={estimate_gas_unit_price}' |
| 109 | + f'&estimate_max_gas_amount={estimate_max_gas_amount}' |
| 110 | + f'&estimate_prioritized_gas_unit_price={estimate_prioritized_gas_unit_price}', |
| 111 | + txn_dict |
| 112 | + ) |
| 113 | + return self._handle_simulate_error(j) |
| 114 | + |
| 115 | + simulate = simulate_transaction |
| 116 | + |
| 117 | + def encode_submission(self, txn_dict: Dict): |
| 118 | + j = self.provider.post(APTTransactionsAPI.ENCODE_SUBMISSION, params_dict=txn_dict) |
| 119 | + return bytes.fromhex(j[2:]) |
| 120 | + |
| 121 | + encode = encode_submission |
| 122 | + |
| 123 | + @staticmethod |
| 124 | + def _handle_simulate_error(j: Response): |
| 125 | + if isinstance(j, list): |
| 126 | + j = j[0] |
| 127 | + else: |
| 128 | + raise ParamsError(f"Check Txn dict Params. RPC Response: {j}") |
| 129 | + |
| 130 | + if j['success']: |
| 131 | + return j |
| 132 | + else: |
| 133 | + raise RPCRequestError(f"RPC Error: {j['vm_status']}") |
0 commit comments