-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapiobjs.py
145 lines (109 loc) · 4.49 KB
/
apiobjs.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from datetime import datetime, timedelta
from decimal import Decimal
from coininfo import COINS
from models import AutomaticPayment
class _NoDefault(object): pass
NoDefault = _NoDefault()
def get_value(container, name, default=NoDefault):
if container is not None and name in container and container[name] is not None:
return container[name]
if default == NoDefault:
raise ValueError('Missing "%s"' % name)
return default
class Destination(object):
TYPES = []
def __init__(self):
self.wallet = None
self.coin = None
@classmethod
def register(cls, dest_type):
cls.TYPES.append(dest_type)
@classmethod
def parse(cls, json):
destination_type = get_value(json, 'type')
for destination_cls in cls.TYPES:
if destination_cls.TYPE == destination_type:
return destination_cls(json)
raise ValueError('Invalid destination type "%s"' % destination_type)
def set_context_info(self, wallet, coin):
self.wallet = wallet
self.coin = coin
if not self.coin.valid_address(self.address):
raise ValueError('Invalid destination address: %s' % self.address)
class AccountDestination(Destination):
TYPE = 'account'
def __init__(self, json):
super(AccountDestination, self).__init__()
self.user = get_value(json, 'user')
self.allow_creation = bool(get_value(json, 'allowCreateNew', False))
self._address = None
self.created = False
@property
def address(self):
if self._address is None:
if self.wallet is None:
raise Exception('Cannot get destination address without context information')
account = self.wallet.account(self.user)
if account == None:
if self.allow_creation:
account = self.wallet.create_account(self.user)
self.created = True
else:
raise ValueError('Unknown account/user: %s' % self.user)
self._address = account.addresses[self.coin.ticker].preferred_address
return self._address
def __iter__(self):
yield 'type', self.TYPE
yield 'address', self.address
yield 'user', self.user
yield 'created', self.created
class AddressDestination(Destination):
TYPE = 'address'
def __init__(self, json):
super(AddressDestination, self).__init__()
self.address = str(get_value(json, 'address'))
def __iter__(self):
yield 'type', self.TYPE
yield 'address', self.address
Destination.register(AccountDestination)
Destination.register(AddressDestination)
class SendRequest(object):
def __init__(self, json):
self.destination = Destination.parse(get_value(json, 'destination'))
self.coin = str(get_value(json, 'coin')).lower()
self.amount = Decimal(get_value(json, 'amount'))
self.priority = str(get_value(json, 'priority', 'normal')).lower()
if self.coin not in [ coin.ticker.lower() for coin in COINS ]:
raise ValueError('Invalid coin "%s"' % self.coin)
self.coin = { coin.ticker.lower(): coin for coin in COINS }[self.coin].ticker
if self.priority not in ['normal', 'low', 'high']:
raise ValueError('Invalid priority "%s"' % self.priority)
@property
def low_priority(self):
self.priority == 'low'
class SetAutoPayInfoRequest(object):
def __init__(self, json):
self.address = str(get_value(json, 'address'))
self.transaction = get_value(json, 'transaction')
self.interval = get_value(json, 'interval')
self.nextpayment = get_value(json, 'nextpayment', None)
self.coin = None
self.account = None
if self.nextpayment is None:
self.nextpayment = datetime.now()
else:
self.nextpayment = datetime(1970, 1, 1) + timedelta(seconds=int(self.nextpayment))
def set_context_info(self, account, coin):
self.account = account
self.coin = coin
if not self.coin.valid_address(self.address):
raise ValueError('Invalid address "%s" for %s' % (self.address, self.coin.ticker))
def dbobject(self):
info = AutomaticPayment()
info.account_id = self.account.model.id
info.coin = self.coin.ticker
info.address = self.address
info.transaction = self.transaction
info.interval = self.interval
info.nextpayment = self.nextpayment
return info