def manage_create_channel_event(): create_channel_event_from_orchestrator = channel_dao.get_one_create_channel_event( TransactionStatus.PENDING) if not bool(create_channel_event_from_orchestrator): return wallet_manager = WalletService(connection) payload = json.loads(create_channel_event_from_orchestrator["payload"]) try: wallet_manager.open_channel_by_third_party( order_id=payload['order_id'], sender=payload['sender'], signature=payload['signature'], r=payload['r'], s=payload['s'], v=payload['v'], current_block_no=payload['current_block_no'], group_id=payload['group_id'], org_id=payload["org_id"], recipient=payload['recipient'], amount=payload['amount'], currency=payload['currency'], amount_in_cogs=payload['amount_in_cogs']) channel_dao.update_create_channel_event( create_channel_event_from_orchestrator, TransactionStatus.SUCCESS) except Exception as e: channel_dao.update_create_channel_event( create_channel_event_from_orchestrator, TransactionStatus.FAILED) utils.report_slack( slack_msg= f"Error while submitting blockchain transaction |method_name:: 'open_channel_by_third_party' |network_id: {NETWORK_ID} |error: {repr(e)}", SLACK_HOOK=SLACK_HOOK)
def create_channel_event_consumer(): logger.info("Getting events") try: create_channel_event_details = channel_dao.get_one_create_channel_event( TransactionStatus.PENDING) if create_channel_event_details is None: return wallet_manager = WalletService(connection) payload = json.loads(create_channel_event_details["payload"]) except Exception as e: logger.error( f"Failed to get record for create channel, error:{repr(e)}") traceback.print_exc() utils.report_slack( 1, f"Failed to get record for create channel, module: create_channel_consumer, " f"NETWORK_ID:{NETWORK_ID}, error: {(repr(e))}", SLACK_HOOK) return try: logger.info(f"fetched event:{create_channel_event_details}") response = wallet_manager.open_channel_by_third_party( order_id=payload['order_id'], sender=payload['sender'], signature=payload['signature'], r=payload['r'], s=payload['s'], v=payload['v'], current_block_no=payload['current_block_no'], group_id=payload['group_id'], org_id=payload["org_id"], recipient=payload['recipient'], amount=payload['amount'], currency=payload['currency'], amount_in_cogs=payload['amount_in_cogs']) channel_dao.update_create_channel_event(create_channel_event_details, TransactionStatus.SUCCESS) except Exception as e: logger.error( f"Exception occurred while create channel, event: {create_channel_event_details}, error:{repr(e)}" ) utils.report_slack( 1, f"Exception occurred while create channel, module: create_channel_consumer, " f"event_id: {create_channel_event_details['row_id']} " f"NETWORK_ID:{NETWORK_ID}, error: {(repr(e))}", SLACK_HOOK) traceback.print_exc() channel_dao.update_create_channel_event(create_channel_event_details, TransactionStatus.FAILED) logger.info("done getting events")
def setUp(self): self.NETWORKS_NAME = dict( (NETWORKS[netId]["name"], netId) for netId in NETWORKS.keys()) self.repo = Repository(net_id=NETWORK_ID, NETWORKS=NETWORKS) self.wallet_service = WalletService(repo=self.repo)
class TestWalletService(unittest.TestCase): def setUp(self): self.NETWORKS_NAME = dict( (NETWORKS[netId]["name"], netId) for netId in NETWORKS.keys()) self.repo = Repository(net_id=NETWORK_ID, NETWORKS=NETWORKS) self.wallet_service = WalletService(repo=self.repo) @patch("common.utils.Utils.report_slack") @patch("common.blockchain_util.BlockChainUtil.create_account") def test_create_wallet(self, mock_create_account, mock_report_slack): mock_create_account.return_value = ( "323449587122651441342932061624154600879572532581", "26561428888193216265620544717131876925191237116680314981303971688115990928499", ) response = self.wallet_service.create_and_register_wallet( username="******") self.assertDictEqual( response, { "address": "323449587122651441342932061624154600879572532581", "private_key": "26561428888193216265620544717131876925191237116680314981303971688115990928499", "status": 0, "type": "GENERAL" }) @patch("common.utils.Utils.report_slack") @patch("wallets.dao.wallet_data_access_object.WalletDAO.get_wallet_details" ) @patch("wallets.dao.wallet_data_access_object.WalletDAO.insert_wallet") @patch( "wallets.dao.wallet_data_access_object.WalletDAO.add_user_for_wallet") def test_register_wallet(self, mock_add_user_for_wallet, mock_insert_wallet, mock_get_wallet_details, mock_report_slack): """ insert new wallet for user """ mock_get_wallet_details.return_value = [] address = "323449587122651441342932061624154600879572532581" type = "GENERAL" status = 0 username = "******" response = self.wallet_service.register_wallet(username, address, type, status) assert response self.assertRaises( Exception, self.wallet_service.register_wallet(username, address, type, status)) def test_remove_user_wallet(self): wallet_dao = WalletDAO(self.repo) username = "******" wallet = Wallet(address="32344958712265144", type="GENERAL", status=0) wallet_dao.insert_wallet(wallet) wallet_dao.add_user_for_wallet(wallet, username) self.wallet_service.remove_user_wallet(username) wallet_details = wallet_dao.get_wallet_data_by_username(username) if len(wallet_details) == 0: assert True else: assert False def tearDown(self): self.repo.execute("DELETE FROM wallet") self.repo.execute("DELETE FROM user_wallet")
import traceback from common.constant import StatusCode, ResponseStatus from common.logger import get_logger from common.repository import Repository from common.utils import Utils, validate_dict, generate_lambda_response, make_response_body from wallets.config import NETWORK_ID, NETWORKS, SLACK_HOOK from wallets.error import Error from wallets.service.wallet_service import WalletService NETWORKS_NAME = dict( (NETWORKS[netId]['name'], netId) for netId in NETWORKS.keys()) repo = Repository(net_id=NETWORK_ID, NETWORKS=NETWORKS) utils = Utils() logger = get_logger(__name__) wallet_service = WalletService(repo=repo) def create_channel(event, context): logger.info("Received request to initiate order") try: payload = json.loads(event["body"]) required_keys = [ "order_id", "sender", "signature", "r", "s", "v", "current_block_no", "group_id", "org_id", "amount", "currency", "recipient", "amount_in_cogs" ] if validate_dict(payload, required_keys): logger.info(f"Payload for create channel: {payload}") response = wallet_service.open_channel_by_third_party( order_id=payload['order_id'],
def route_path(path, method, payload_dict, path_parameters): wallet_manager = WalletService(repo=db[NETWORK_ID]) path_exist = True response_data = None if "/wallet" == path and method == "POST": response_data = wallet_manager.create_and_register_wallet( username=payload_dict["username"]) elif "/wallet" == path and method == "GET": username = payload_dict["username"] response_data = wallet_manager.get_wallet_details(username=username) elif "/wallet/channel/deposit" == path and method == 'POST': response_data = wallet_manager.add_funds_to_channel( org_id=payload_dict['org_id'], group_id=payload_dict['group_id'], channel_id=payload_dict['channel_id'], sender=payload_dict['sender'], recipient=payload_dict['recipient'], order_id=payload_dict['order_id'], amount=payload_dict['amount'], currency=payload_dict['currency'], amount_in_cogs=payload_dict['amount_in_cogs']) elif "/wallet/status" == path: response_data = wallet_manager.set_default_wallet( username=payload_dict["username"], address=payload_dict['address']) elif "/wallet/channel/transactions" == path and method == 'GET': order_id = payload_dict.get('order_id', None) username = payload_dict.get('username', None) org_id = payload_dict.get('org_id', None) group_id = payload_dict.get('group_id', None) if order_id is not None: logger.info( f"Received request to fetch transactions against order_id: {order_id}" ) response_data = wallet_manager.get_channel_transactions_against_order_id( order_id=payload_dict["order_id"]) elif username is not None and group_id is not None and org_id is not None: logger.info( f"Received request to fetch transactions for username: {username} " f"group_id: {group_id} " f"org_id: {org_id}") response_data = wallet_manager.get_transactions_from_username_recipient( username=username, group_id=group_id, org_id=org_id) else: raise Exception("Bad Parameters") elif "/wallet/register" == path: username = payload_dict["username"] status = WalletStatus.ACTIVE.value wallet_address = payload_dict["address"] wallet_type = payload_dict['type'] if wallet_type not in WALLET_TYPES_ALLOWED: raise Exception("Wallet type invalid") wallet_manager.register_wallet(wallet_address, wallet_type, status, username) response_data = [] else: path_exist = False return path_exist, response_data