def transactions(): body = request.get_json() user_id = body.get('user_id') fiat_id = body.get('fiat_id') coin_id = body.get('coin_id') coinamt = body.get('coinamt') fiatprice = body.get('fiatprice') purchase = body.get('purchase') coinamt = round(coinamt, 8) date = datetime.datetime.now() vault = Vault.query.filter_by(user_id=user_id).first() vault = vault.to_dict() coin = VaultCoin.query.filter_by(vault_id=vault['id']).filter_by( coin_id=coin_id).first() if purchase == True: coin.amount = coin.amount + coinamt db.session.commit() else: if (coin.amount < coinamt): return {'errors': ['Insufficient tokens']} coin.amount = coin.amount - coinamt db.session.commit() new_transfer = Transaction(user_id=user_id, fiat_id=fiat_id, coin_id=coin_id, coinamt=coinamt, fiatprice=fiatprice, purchase=purchase, date=date) db.session.add(new_transfer) db.session.commit() new_transfer = new_transfer.to_dict() return new_transfer
def create_transaction(): data = request.get_json() or {} if 'sender' not in data or not isinstance(data['sender'], int): return bad_request('Sender must not be empty or incorrect value') sender = User.query.get_or_404(data['sender']) if token_auth.current_user().id != sender.id: abort(403) if 'recipient' not in data or data['recipient'] == sender.email or \ not User.query.filter_by(email=data['recipient']).first(): return bad_request('You can not transfer yourself or empty value') if 'transfer_amount' not in data or not isinstance(data['transfer_amount'], (float, int)) or \ data['transfer_amount'] <= 0.: return bad_request('Incorrect value of transfer amount') recipient = User.query.filter_by(email=data['recipient']).first() sender.bill = sender.bill - float(data['transfer_amount']) transfer_amount_base = float( data['transfer_amount']) / sender.currency_user.rate recipient_bill = recipient.bill / recipient.currency_user.rate transaction = Transaction(sender=sender.id, recipient=recipient.email, transfer_amount_base=transfer_amount_base, sender_rate=sender.currency_user.rate) recipient.bill = (recipient_bill + transfer_amount_base) * recipient.currency_user.rate db.session.add(transaction) db.session.commit() return jsonify(transaction.to_dict())
def post_transaction(): print(request.json) data = request.json coin = Coin.query.filter_by(symbol=data['symbol'].upper()).one() print(coin) transaction = Transaction(type=data['type'], price=data['price'], quantity=data['quantity'], coin=coin, wallet_id=data['wallet_id']) wallet = Wallet.query.get(data['wallet_id']) wallet.balance += data['type'] db.session.add(transaction) db.session.add(wallet) db.session.commit() return transaction.to_dict()
def add_user_account_transaction(username, accountid): user = User.query.filter_by(username=username).first_or_404() account = Account.query.get_or_404(accountid) try: transaction = Transaction(date=datetime.strptime( request.json.get('date'), '%Y-%m-%d').date(), amount=request.json.get('amount'), type=request.json.get('type'), description=request.json.get('description'), category=request.json.get('category'), tag=request.json.get('tag'), status=request.json.get('status'), accountid=account.id, payee=request.json.get('payee')).save() except: abort(400) return jsonify(transaction.to_dict()), 201