Browse Source

Fix trades

Eren Yilmaz 5 years ago
parent
commit
d0c0eb2417
1 changed files with 11 additions and 12 deletions
  1. 11 12
      model.py

+ 11 - 12
model.py

@@ -137,26 +137,25 @@ def own(user_id, ownable_name, amount=0):
                 ''', (user_id, ownable_name, amount))
 
 
-def send_ownable(from_user_id, to_user_id, ownable_name, amount):
+def send_ownable(from_user_id, to_user_id, ownable_id, amount):
     connect()
 
     if amount < 0:
-        return False
+        raise AssertionError('Can not send negative amount')
 
-    if from_user_id != bank_id():
-        cursor.execute('''
-                    UPDATE ownership
-                    SET amount = amount - ?
-                    WHERE user_id = ?
-                    AND ownable_id = (SELECT rowid FROM ownables WHERE name = ?)
-                    ''', (amount, from_user_id, ownable_name,))
+    cursor.execute('''
+                UPDATE ownership
+                SET amount = amount - ?
+                WHERE user_id = ?
+                AND ownable_id = ?
+                ''', (amount, from_user_id, ownable_id,))
 
     cursor.execute('''
                 UPDATE ownership
                 SET amount = amount + ?
                 WHERE user_id = ?
-                AND ownable_id = (SELECT rowid FROM ownables WHERE name = ?)
-                ''', (amount, to_user_id, ownable_name))
+                AND ownable_id = ?
+                ''', (amount, to_user_id, ownable_id,))
     return True
 
 
@@ -723,7 +722,7 @@ def execute_orders(ownable_id):
             return AssertionError()
 
         # actually execute the order, but the bank does not send or receive anything
-        send_ownable(buyer_id, seller_id, ownable_id, price * amount)
+        send_ownable(buyer_id, seller_id, currency_id(), price * amount)
         send_ownable(seller_id, buyer_id, ownable_id, amount)
 
         # update order execution state