def user_get(userId): request = app.current_request user = Database.find_one("User", {'id': int(userId)}) if not user: raise NotFoundError('user not found with id {}'.format(userId)) user['bankConnected'] = not not user['truelayerAccessToken'] user = refresh_user_token(user) accounts = TL.get_accounts(user) return toObject(accounts)
def truelayer_callback(): request = app.current_request code = request.query_params['code'] nonce = request.query_params['state'] user = Database.find_one("User", {'nonce': nonce}) if not user: raise NotFoundError('user not found') user = Database.update('User', {"id": user["id"]}, TL.get_access_token(user, code), return_updated=True)[0] accounts = TL.get_accounts(user) if not len(accounts): raise NotFoundError('No accounts found for user') # update the permissions contract # Web3Helper.transact(permissions_contract,'setAuthorized',Web3Helper.toChecksumAddress(user['address']),1) return Response( body=None, status_code=302, headers={"Location": "{}investor/profile".format(front_end_url)})
def truelayer(): request = app.current_request if 'id' not in request.query_params: raise NotFoundError('id') user_id = request.query_params['id'] user = Database.find_one('User', {'id': user_id}) nonce = ''.join( random.choice("qwertyuioplkjhgfdsazxvbnm") for _ in range(10)) Database.update('User', {"id": int(user["id"])}, {"nonce": nonce}) url = TL.get_auth_url(nonce) return Response(body=None, status_code=302, headers={"Location": url})
def checkKyc(address): user = Database.find_one("User", {'address': address}) if not user: raise NotFoundError( 'user not found with address {}'.format(address)) user = refresh_user_token(user) accounts = TL.get_accounts(user) if not (len(accounts)): raise ForbiddenError('Not KYC') else: return {'message': 'Is KYC', 'status': 200}
def checkBalance(address): request = app.current_request amount = int(request.query_params['amount']) user = Database.find_one("User", {'address': Web3.toChecksumAddress(address)}) if not user: raise NotFoundError( 'user not found with address {}'.format(address)) user = refresh_user_token(user) balance = TL.get_balance(user) balance_small = int(balance['available'] * 100) if (balance_small < amount): raise ForbiddenError('Not enough funds') else: return {'message': 'Has funds', 'status': 200}
def refresh_user_token(user): return Database.update('User', {"id": user["id"]}, TL.get_refresh_token(user), return_updated=True)[0]