-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.py
69 lines (44 loc) · 1.7 KB
/
wallet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from replit import db
from stocks import get_price
class InsufficientFunds(Exception):
""" Insufficient funds for this transaction. """
class InsufficientStocks(Exception):
""" Insufficient stocks for this transaction. """
class Wallet:
def __init__(self, name):
self.name = name
self.id = f"wallet_{name}"
@property
def value(self):
return db.setdefault(self.id, 0)
@value.setter
def value(self, amount):
db.setdefault(self.id, amount)
def buy_stock(self, ticker_symbol, amount):
total_price = get_price(ticker_symbol)["ask"] * amount
if total_price > self.value:
raise InsufficientFunds(f"Not enough funds to purchase {amount} {ticker_symbol} for {total_price}")
self.value -= total_price
db_name = f"{self.id}_{ticker_symbol}"
db.setdefault(db_name, 0)
db[db_name] += amount
return db[db_name]
def sell_stock(self, ticker_symbol, amount):
total_gain = get_price(ticker_symbol)["bid"] * amount
db_name = f"{self.id}_{ticker_symbol}"
current_stock_count = db.setdefault(db_name, 0)
if amount < current_stock_count:
raise InsufficientFunds(f"Not enough stocks to purchase {ticker_symbol}")
db[db_name] -= amount
self.value += total_gain
return db[db_name]
if __name__ == "__main__":
# Tests
wallet = Wallet("foo")
gmc_price = get_price("AMC")
buy, sell = gmc_price['ask'], gmc_price['bid']
start = buy * 10
wallet.value = start
cost = buy * 5
wallet.buy_stock("GMC", 5)
assert wallet.value == start - cost, "Wallet cash didn't go down"