def save_changes(self, changes: Dict[str, List[tuple]]) -> Dict[str, int]: """Persist a change to the database.""" trans = Transaction() new_rows_id_map = [] # type: List[Tuple[int, int]] try: for row in changes['deleted']: trans.execute( self.table.delete_row(row[self.table.primary_key_index])) for row in changes['added']: id = row[self.table.primary_key_index] new_id = trans.execute(self.table.add_row(values=list(row))) new_rows_id_map.append((id, new_id)) for row in changes['updated']: id = row[self.table.primary_key_index] trans.execute(self.table.update_row(pk=id, values=list(row))) results = trans.commit() results['new_rows_id_map'] = new_rows_id_map return results except: raise
def Verify(self, request, context): trans = Transaction(request) logging.info("verify transfer ({} from {} to {})".format( request.Value, request.FromID, request.ToID)) res = self.database.verify(Transaction(request)) if res == 'SUCCEEDED': return db_pb2.VerifyResponse( Result=db_pb2.VerifyResponse.SUCCEEDED, BlockHash=self.database.blockByTrans(trans).hash) elif res == 'PENDING': return db_pb2.VerifyResponse( Result=db_pb2.VerifyResponse.PENDING, BlockHash=self.database.blockByTrans(trans).hash) else: return db_pb2.VerifyResponse(Result=db_pb2.VerifyResponse.FAILED, BlockHash="")
def Transfer(self, request, context): if not self.database.pushTransaction(Transaction(request)): return db_pb2.BooleanResponse(Success=False) logging.info("transfer {} from {} to {}".format( request.Value, request.FromID, request.ToID)) success = False for stub in self.stubs: try: stub.PushTransaction(request, timeout=10) success = True except grpc.RpcError: continue return db_pb2.BooleanResponse(Success=success)
def monthly_revenue(db): print("Getting Monthly Revenue") year = raw_input("Year: ") try: year = int(year) except ValueError: print("Year must be integer") return month = raw_input("Month: ") try: month = int(month) if month not in range(1, 13): raise ValueError except ValueError: print("Month must be integer between 1 and 12") return tran = Transaction(db) rev = tran.monthly_revenue(year, month) print("Revenue for %d-%d is $%.2f\n" % (year, month, rev))
def insert_transaction(amount_orig, amount_usd, amount_eur, currency,\ investigation, purpose, date, source_file, from_acc_id, to_acc_id): s = Session() # print("from: %d"%from_acc_id) from_acc = _get_account(s, from_acc_id) if from_acc_id else None # print("to: %d"%to_acc_id) to_acc = _get_account(s, to_acc_id) if to_acc_id else None trx = Transaction(amount_orig=amount_orig, amount_usd=amount_usd, amount_eur=amount_eur,\ currency=currency, investigation=investigation,\ purpose=purpose, date=date, source_file=source_file,\ payee=from_acc, beneficiary=to_acc) s.add(trx) s.commit() return trx.id
def transfer_funds(): if not request.is_json: abort(400) recipient = request.json.get('to') sender = request.json.get('from') amount = request.json.get('amount') if not (recipient and sender and amount): return make_response(jsonify({'error': 'from, to and amount fields are required'}), 400) if type(amount) != str or not re.fullmatch(r'\d*?.\d{2}', amount): return make_response( jsonify({'error': 'Amount should be string with two decimal points, separated with dot'}), 400, ) amount = decimal.Decimal(amount) min_transfer_amount = app.config['MIN_TRANSFER_AMOUNT'] if amount < decimal.Decimal(min_transfer_amount): return make_response(jsonify({'error': 'Amount must be at least {}'.format(min_transfer_amount)}), 400) recipient = Wallet.query.filter_by(address=recipient).first() sender = flask_storage.current_user.wallets.filter_by(address=sender).first() if not (recipient and sender): return make_response(jsonify({'error': 'Wallet does not exist'}), 400) if recipient.id == sender.id: return make_response(jsonify({'error': 'Wallets should be not equal'}), 400) try: transaction = Transaction.create(recipient, sender, amount) transaction.process() except InsufficientFunds: return make_response(jsonify({'error': 'insufficient funds'}), 400) except (WalletLimit, TransactionError): return make_response(jsonify({'error': 'transaction failed'}), 400) return make_response(jsonify({'message': 'transaction processed successfully'}), 200)
def save_changes(self, changes: Dict[str, List[tuple]]) -> Dict[str, int]: """Persist a change to the database.""" trans = Transaction() new_rows_id_map = [] # type: List[Tuple[int, int]] try: for row in changes['deleted']: trans.execute(self.table.delete_row(row[self.table.primary_key_index])) for row in changes['added']: id = row[self.table.primary_key_index] new_id = trans.execute(self.table.add_row(values=list(row))) new_rows_id_map.append((id, new_id)) for row in changes['updated']: id = row[self.table.primary_key_index] trans.execute(self.table.update_row(pk=id, values=list(row))) results = trans.commit() results['new_rows_id_map'] = new_rows_id_map return results except: raise
def PushTransaction(self, request, context): self.database.pushTransaction(Transaction(request)) return db_pb2.Null()