-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
64 lines (50 loc) · 2.14 KB
/
user.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
from re import L
from mocks import ERC20
class User:
def __init__(self, name, farms, router):
self.name = name
self.farms = farms
self.router = router
self.assets = {}
def mintTokens(self, assets, amounts):
if isinstance(assets, list) and isinstance(amounts, list):
assert(len(assets) == len(amounts))
for asset, amount in zip(assets, amounts):
asset.mint(self, amount)
self.assets[asset] = amount
elif isinstance(assets, ERC20) and isinstance(amounts, int):
assets.mint(self, amounts)
self.assets[assets] = amounts
else:
assert(False)
def createPool(self, tokens, weights, amounts):
self.farms.createPool(self, tokens, weights, amounts)
for token, amount in zip(tokens, amounts):
self.assets[token] -= amount
def deposit(self, pid, tokens, amounts):
self.farms.deposit(self, pid, tokens, amounts)
for token, amount in zip(tokens, amounts):
self.assets[token] -= amount
def harvest(self, pids):
if isinstance(pids, list):
self.farms.harvestAll(self, pids)
elif isinstance(pids, int):
self.farms.harvest(self, pids)
else:
assert(False)
def withdraw(self, pids):
if isinstance(pids, list):
for pid in pids:
lps = self.router.pools[pid]['users'][self]
return_tokens, return_amounts = self.farms.withdrawAndHarvest(self, pid, lps)
for tok, amt in zip(return_tokens, return_amounts):
self.assets.setdefault(tok, 0)
self.assets[tok] += amt
elif isinstance(pids, int):
lps = self.router.pools[pids]['users'][self]
return_tokens, return_amounts = self.farms.withdrawAndHarvest(self, pids, lps)
for tok, amt in zip(return_tokens, return_amounts):
self.assets.setdefault(tok, 0)
self.assets[tok] += amt
def getAssets(self):
return {tok.name: amt for tok, amt in self.assets.items()}