def get_balance(self, address_account, account_from=None): """ Get a balance of an account. At the moment the account needs to have a balance to get the balance of it's account or any other account. Event though this is using a query request. :param address_account: Address or :class:`.Account` object to get the funds for. :type address_account: Account, int, str :param account_from: Optional :class:`.Account` object or account address to make the request. :type account_from: Account, int, str, optional :returns: Return the current balance of the address or account `address_account` .. code-block:: python >>> # Create a new account with new public/priavte keys and address >>> account = convex_api.create_account() >>> # get the balance of the contract >>> print(convex_api.get_balance(account)) 0 >>> print(convex_api.request_funds(100000, account)) 100000 >>> print(convex_api.get_balance(account)) 100000 """ value = 0 if is_address(address_account): address = to_address(address_account) else: address = address_account.address address_from = address if account_from: if is_address(account_from): address_from = to_address(account_from) else: address_from = account_from.address line = f'(balance #{address})' """ if self._language == API.LANGUAGE_SCRYPT: line = f'balance(#{address})' """ try: result = self._transaction_query(address_from, line) except ConvexAPIError as error: if error.code != 'NOBODY': raise else: value = result['value'] return value
def owner_asset_list(self, account_address: AccountAddress): if is_address(account_address): address = account_address else: address = account_address.address command = f'(owner-list {address})' result = self.query(command, address) if result and 'value' in result: return result['value'] return result
def event_list(self, asset_id: str, account_address: AccountAddress): command = f'(event-list {add_0x_prefix(asset_id)})' if is_address(account_address): address = account_address else: address = account_address.address result = self.query(command, address) if result and 'value' in result: return ProvenanceContract.convert_event_list(result['value']) return result
def owner(self, did: str, account_address: AccountAddress): did_id = did_to_id(did).lower() command = f'(owner {did_id})' if is_address(account_address): address = account_address else: address = account_address.address result = self.query(command, address) if result and 'value' in result: return to_address(result['value']) return result
def register_account_name(self, name, address_account, account=None): """ Register or update an account address with an account name. This call will submit to the CNS (Convex Name Service), a name in the format "`account.<your_name>`". You need to have some convex balance in your account, and a valid account address. :param str name: name of the account to register. :param number|Account account_address: Account or address to register. :param Account account: :class:`.Account` object to register the account name. .. code-block:: python >>> # load the register account >>> register_account = convex.load_account('register_account', key_pair) >>> account = convex.create_account(key_pair) >>> print(account.address) 1024 >>> account = convex.register_account('my_new_account', account.address, register_account) >>> print(account.address) 1024 # or you can call with only one account, this will use the address of that account >>> print(register_account.address) 404 >>> account = convex.register_account('my_new_account', register_account) >>> print(account.address) 404 """ # is the address_account field only an address? if is_address(address_account): address = to_address(address_account) else: # if account then use the account address, and also see if we can use it for the # registration address = address_account.address if account is None: account = address_account # we must have a valid account to do the registration if not account: raise ValueError('you need to provide a registration account to register an account name') if not address: raise ValueError('You need to provide a valid address to register an account name') self._registry.register(f'account.{name}', address, account) return Account(account.key_pair, address=address, name=name)
def transfer(self, to_address_account, amount, account): """ Transfer funds from on account to another. :param to_address_account: Address or :class:`.Account` object to send the funds too :type to_address_account: Account, int, str :param number amount: Amount to transfer :param Account account: :class:`.Account` object to send the funds from :returns: The transfer record sent back after the transfer has been made .. code-block:: python >>> # Create a new account with new public/priavte keys and address >>> account = convex_api.create_account() >>> print(convex_api.request_funds(10000000, account)) 10000000 >>> print(convex_api.get_balance(account)) 10000000 >>> my_account = convex_api.create_account() >>> # transfer some funds to my_account >>> print(convex_api.transfer(my_account, 100, account)) 100 >>> print(convex_api.get_balance(my_account)) 100 >>> print(convex_api.get_balance(account)) 9998520 """ if is_address(to_address_account): transfer_to_address = to_address(to_address_account) else: transfer_to_address = to_address_account.address if not to_address: raise ValueError(f'You must provide a valid to account/address ({transfer_to_address}) to transfer funds too') line = f'(transfer #{transfer_to_address} {amount})' """ if self._language == API.LANGUAGE_SCRYPT: line = f'transfer(#{transfer_to_address}, {amount})' """ result = self.send(line, account) if result and 'value' in result: return result['value'] return 0
def test_utils_is_address(): address_int = secrets.randbelow(pow(2, 1024)) + 1 assert (is_address(address_int)) address_str = str(address_int) assert (is_address(address_str)) address = to_address(f'#{address_str}') assert (address == address_int) assert (not is_address('test')) assert (not is_address(' #')) assert (is_address('#0')) assert (not is_address('#-1'))
def get_account_info(self, address_account): """ Get account information. This will only work with an account that has a balance or has had some transactions processed on the convex network. New accounts with no transfer or transactions will raise: ConvexRequestError(404, 'The Account for this Address does not exist.') error The returned information is dictionary of account information. :param address_account: :class:`.Account` object or address of an account to get current information on. :type address_account: Account, int, str :returns: dict of information, such as .. code-block:: python >>> # Create a new account with new public/priavte keys and address >>> account = convex_api.create_account() >>> # get the balance of the contract >>> print(convex_api.get_account_info(account)) {'environment': {}, 'address': 1178, 'memorySize': 0, 'balance': 0, 'isLibrary': False, 'isActor': False, 'allowance': 0, 'sequence': 0, 'type': 'user'} """ if is_address(address_account): address = to_address(address_account) else: address = address_account.address account_url = urljoin(self._url, f'/api/v1/accounts/{address}') logger.debug(f'get_account_info {account_url}') response = requests.get(account_url) if response.status_code != 200: raise ConvexRequestError('get_account_info', response.status_code, response.text) result = response.json() logger.debug(f'get_account_info repsonse {result}') return result
def query(self, transaction, address_account, language=None): """ Run a query transaction on the block chain. Since this does not change the network state, and the account does not need to sign the transaction. No funds will be used when executing this query. For this reason you can just pass the account address, or if you want to the :class:`.Account` object. :param str transaction: Transaction to execute. This can only be a read only transaction. :param address_account: :class:`.Account` object or int address of an account to use for running this query. :type address_account: Account, int, str :param language: The type of language to use, if not provided the default language set will be used. :type language: str, optional :returns: Return the resultant query transaction .. code-block:: python >>> # Create a new account with new public/priavte keys and address >>> account = convex_api.create_account() >>> # submit a query transaction using the account address >>> print(convex_api.query(f'(balance {account.address})', account.address)) {'value': 0} >>> # request some funds to do stuff >>> print(convex_api.request_funds(100000, account)) 100000 >>> print(convex_api.query(f'(balance {account.address})', account.address)) {'value': 100000} """ if is_address(address_account): address = to_address(address_account) else: address = address_account.address return self._transaction_query(address, transaction, language)
def is_address(self, value): return is_address(value)