async def receive_all(self, request: web.Request, request_json: dict): """receive every pending block in wallet""" if 'wallet' not in request_json: return self.generic_error() # Retrieve wallet try: wallet = await Wallet.get_wallet(request_json['wallet']) except WalletNotFound: return self.json_response(data={'error': 'wallet not found'}) except WalletLocked: return self.json_response(data={'error': 'wallet locked'}) balance_json = await RPCClient.instance().accounts_balances( [a.address for a in await wallet.get_all_accounts()]) if 'balances' not in balance_json: return self.json_response( data={'error': 'failed to retrieve balances'}) received_count = 0 for k, v in balance_json['balances'].items(): if int(v['pending']) > 0: wallet_util = WalletUtil(await wallet.get_account(k), wallet) received_count += await wallet_util.receive_all() return self.json_response(data={'received': received_count})
async def block_arrival_handler(self, data: dict): """invoked when we receive a new block""" log.server_logger.debug("Received Callback") is_send = False if 'block' in data and 'subtype' in data['block'] and data['block'][ 'subtype'] == 'send': is_send = True elif 'is_send' in data and (data['is_send'] == 'true' or data['is_send']): is_send = True if is_send: # Ignore receive_minimum if config.Config.instance().receive_minimum > int(data['amount']): return # Determine if the recipient is one of ours destination = data['block']['link_as_account'] acct = await Account.filter(address=destination ).prefetch_related('wallet').first() if acct is None: acct = await AdHocAccount.filter( address=destination).prefetch_related('wallet').first() if acct is None: return log.server_logger.debug( f"Auto receiving {data['hash']} for {destination}") wu = WalletUtil(acct, acct.wallet) try: await wu.receive(data['hash']) except Exception: log.server_logger.debug(f"Failed to receive {data['hash']}")
async def bulk_representative_update(self, rep: str): """Set all account representatives to rep""" for a in await self.accounts.all(): w = WalletUtil(a, self) try: await w.representative_set(rep, only_if_different=True) except AccountNotFound: pass
async def send(self, request: web.Request, request_json: dict): """RPC send""" if 'wallet' not in request_json or 'source' not in request_json or 'destination' not in request_json or 'amount' not in request_json: return self.generic_error() elif not Validators.is_valid_address(request_json['source']): return self.json_response(data={'error': 'Invalid source'}) elif not Validators.is_valid_address(request_json['destination']): return self.json_response(data={'error': 'Invalid destination'}) id = request_json['id'] if 'id' in request_json else None work = request_json['work'] if 'work' in request_json else None # Retrieve wallet try: wallet = await Wallet.get_wallet(request_json['wallet']) except WalletNotFound: return self.json_response(data={'error': 'wallet not found'}) except WalletLocked: return self.json_response(data={'error': 'wallet locked'}) # Retrieve account on wallet account = await wallet.get_account(request_json['source']) if account is None: return self.json_response(data={'error': 'Account not found'}) # Try to create and publish send block wallet = WalletUtil(account, wallet) try: resp = await wallet.send(int(request_json['amount']), request_json['destination'], id=id, work=work) except AccountNotFound: return self.json_response(data={'error': 'Account not found'}) except BlockNotFound: return self.json_response(data={'error': 'Block not found'}) except WorkFailed: return self.json_response( data={'error': 'Failed to generate work'}) except ProcessFailed: return self.json_response(data={'error': 'RPC Process failed'}) except InsufficientBalance: return self.json_response(data={'error': 'insufficient balance'}) if resp is None: return self.json_response( data={'error': 'Unable to create send block'}) return self.json_response(data=resp)
async def setUpClassAsync(cls): if 'BANANO' in os.environ: del os.environ['BANANO'] await DBConfig(mock=True).init_db() cls.wallet = Wallet( seed="7474F694061FB3E5813986AEC8A65340B5DEDB4DF94E394CB44489BEA6B21FCD", representative='nano_1oa4sipdm679m7emx9npmoua14etkrj1e85g3ujkxmn4ti1aifbu7gx1zrgr', encrypted=False ) await cls.wallet.save() cls.account = Account( wallet=cls.wallet, address='nano_19zdjp6tfhqzcag9y3w499h36nr16ks6gdsfkawzgomrbxs54xaybmsyamza', account_index=0 ) await cls.account.save() cls.adhoc_account = AdHocAccount( wallet=cls.wallet, address='nano_1oa4sipdm679m7emx9npmoua14etkrj1e85g3ujkxmn4ti1aifbu7gx1zrgr', private_key='86A3D926AB6BEBAA678C13823D7A92A97CAFAFD277EBF4B54C42C8BB9806EAEE' ) await cls.adhoc_account.save() cls.wallet_util = WalletUtil(cls.account, cls.wallet) cls.wallet_util_adhoc = WalletUtil(cls.adhoc_account, cls.wallet)
async def account_representative_set(self, request: web.Request, request_json: dict): """RPC account_representative_set""" if 'wallet' not in request_json or 'account' not in request_json or 'representative' not in request_json: return self.generic_error() elif not Validators.is_valid_address(request_json['account']): return self.json_response(data={'error': 'Invalid account'}) elif not Validators.is_valid_address(request_json['representative']): return self.json_response(data={'error': 'Invalid representative'}) work = request_json['work'] if 'work' in request_json else None # Retrieve wallet try: wallet = await Wallet.get_wallet(request_json['wallet']) except WalletNotFound: return self.json_response(data={'error': 'wallet not found'}) except WalletLocked: return self.json_response(data={'error': 'wallet locked'}) # Retrieve account on wallet account = await wallet.get_account(request_json['account']) if account is None: return self.json_response(data={'error': 'Account not found'}) # Try to create and publish CHANGE block wallet = WalletUtil(account, wallet) try: resp = await wallet.representative_set( request_json['representative'], work=work) except AccountNotFound: return self.json_response(data={'error': 'Account not found'}) except WorkFailed: return self.json_response( data={'error': 'Failed to generate work'}) except ProcessFailed: return self.json_response(data={'error': 'RPC Process failed'}) if resp is None: return self.json_response( data={'error': 'Unable to create change block'}) return self.json_response(data=resp)
async def receive(self, request: web.Request, request_json: dict): """RPC receive""" if 'wallet' not in request_json or 'account' not in request_json or 'block' not in request_json: return self.generic_error() elif not Validators.is_valid_address(request_json['account']): return self.json_response(data={'error': 'Invalid address'}) elif not Validators.is_valid_block_hash(request_json['block']): return self.json_response(data={'error': 'Invalid block'}) work = request_json['work'] if 'work' in request_json else None # Retrieve wallet try: wallet = await Wallet.get_wallet(request_json['wallet']) except WalletNotFound: return self.json_response(data={'error': 'wallet not found'}) except WalletLocked: return self.json_response(data={'error': 'wallet locked'}) # Retrieve account on wallet account = await wallet.get_account(request_json['account']) if account is None: return self.json_response(data={'error': 'Account not found'}) # Try to receive block wallet = WalletUtil(account, wallet) try: response = await wallet.receive(request_json['block'], work=work) except BlockNotFound: return self.json_response(data={'error': 'Block not found'}) except WorkFailed: return self.json_response( data={'error': 'Failed to generate work'}) except ProcessFailed: return self.json_response(data={'error': 'RPC Process failed'}) if response is None: return self.json_response( data={'error': 'Unable to receive block'}) return self.json_response(data=response)