1
- import logging
2
-
3
- from .blockchainobject import BlockchainObject
4
- from .exceptions import AccountDoesNotExistsException
1
+ # -*- coding: utf-8 -*-
2
+ from .amount import Amount
5
3
from .instance import BlockchainInstance
4
+ from graphenecommon .account import (
5
+ Account as GrapheneAccount ,
6
+ AccountUpdate as GrapheneAccountUpdate ,
7
+ )
8
+ from bitsharesbase import operations
6
9
7
10
8
- log = logging .getLogger ()
9
-
10
-
11
- class Account (BlockchainObject ):
11
+ @BlockchainInstance .inject
12
+ class Account (GrapheneAccount ):
12
13
""" This class allows to easily access Account data
13
14
14
15
:param str account_name: Name of the account
@@ -40,76 +41,8 @@ class Account(BlockchainObject):
40
41
"""
41
42
42
43
type_id = 2
43
-
44
- def __init__ (self , * args , ** kwargs ):
45
- self .full = kwargs .pop ("full" , False )
46
- super ().__init__ (* args , ** kwargs )
47
-
48
- def refresh (self ):
49
- """ Refresh/Obtain an account's data from the API server
50
- """
51
- import re
52
-
53
- if re .match ("^1\.2\.[0-9]*$" , self .identifier ):
54
- account = self .blockchain .rpc .get_objects ([self .identifier ])[0 ]
55
- else :
56
- account = self .blockchain .rpc .lookup_account_names ([self .identifier ])[0 ]
57
- if not account :
58
- raise AccountDoesNotExistsException (self .identifier )
59
- self .cache (account ["name" ])
60
-
61
- if self .full :
62
- accounts = self .blockchain .rpc .get_full_accounts ([account ["id" ]], False )
63
- if accounts and isinstance (accounts , list ):
64
- account = accounts [0 ][1 ]
65
- else :
66
- raise AccountDoesNotExistsException (self .identifier )
67
- super (Account , self ).__init__ (
68
- account ["account" ], blockchain_instance = self .blockchain
69
- )
70
- for k , v in account .items ():
71
- if k != "account" :
72
- self [k ] = v
73
- else :
74
- super (Account , self ).__init__ (account , blockchain_instance = self .blockchain )
75
-
76
- @property
77
- def name (self ):
78
- return self ["name" ]
79
-
80
- @property
81
- def is_ltm (self ):
82
- """ Is the account a lifetime member (LTM)?
83
- """
84
- return self ["id" ] == self ["lifetime_referrer" ]
85
-
86
- @property
87
- def balances (self ):
88
- """ List balances of an account. This call returns instances of
89
- :class:`bitshares.amount.Amount`.
90
- """
91
- from .amount import Amount
92
-
93
- balances = self .blockchain .rpc .get_account_balances (self ["id" ], [])
94
- return [
95
- Amount (b , blockchain_instance = self .blockchain )
96
- for b in balances
97
- if int (b ["amount" ]) > 0
98
- ]
99
-
100
- def balance (self , symbol ):
101
- """ Obtain the balance of a specific Asset. This call returns instances of
102
- :class:`bitshares.amount.Amount`.
103
- """
104
- from .amount import Amount
105
-
106
- if isinstance (symbol , dict ) and "symbol" in symbol :
107
- symbol = symbol ["symbol" ]
108
- balances = self .balances
109
- for b in balances :
110
- if b ["symbol" ] == symbol :
111
- return b
112
- return Amount (0 , symbol )
44
+ amount_class = Amount
45
+ operations = operations
113
46
114
47
@property
115
48
def call_positions (self ):
@@ -138,93 +71,9 @@ def openorders(self):
138
71
Order (o , blockchain_instance = self .blockchain ) for o in self ["limit_orders" ]
139
72
]
140
73
141
- @property
142
- def is_fully_loaded (self ):
143
- """ Is this instance fully loaded / e.g. all data available?
144
- """
145
- return self .full and "votes" in self
146
-
147
- def ensure_full (self ):
148
- if not self .is_fully_loaded :
149
- self .full = True
150
- self .refresh ()
151
-
152
- def history (self , first = 0 , last = 0 , limit = - 1 , only_ops = [], exclude_ops = []):
153
- """ Returns a generator for individual account transactions. The
154
- latest operation will be first. This call can be used in a
155
- ``for`` loop.
156
-
157
- :param int first: sequence number of the first
158
- transaction to return (*optional*)
159
- :param int last: sequence number of the last
160
- transaction to return (*optional*)
161
- :param int limit: limit number of transactions to
162
- return (*optional*)
163
- :param array only_ops: Limit generator by these
164
- operations (*optional*)
165
- :param array exclude_ops: Exclude these operations from
166
- generator (*optional*).
167
-
168
- ... note::
169
- only_ops and exclude_ops takes an array of strings:
170
- The full list of operation ID's can be found in
171
- bitsharesbase.operationids.
172
- Example: ['transfer', 'fill_order']
173
- """
174
- from bitsharesbase .operations import getOperationNameForId
175
-
176
- _limit = 100
177
- cnt = 0
178
-
179
- if first < 0 :
180
- first = 0
181
-
182
- while True :
183
- # RPC call
184
- txs = self .blockchain .rpc .get_account_history (
185
- self ["id" ],
186
- "1.11.{}" .format (last ),
187
- _limit ,
188
- "1.11.{}" .format (first - 1 ),
189
- api = "history" ,
190
- )
191
- for i in txs :
192
- if exclude_ops and getOperationNameForId (i ["op" ][0 ]) in exclude_ops :
193
- continue
194
- if not only_ops or getOperationNameForId (i ["op" ][0 ]) in only_ops :
195
- cnt += 1
196
- yield i
197
- if limit >= 0 and cnt >= limit :
198
- return
199
-
200
- if not txs :
201
- log .info ("No more history returned from API node" )
202
- break
203
- if len (txs ) < _limit :
204
- log .info ("Less than {} have been returned." .format (_limit ))
205
- break
206
- first = int (txs [- 1 ]["id" ].split ("." )[2 ])
207
-
208
- def upgrade (self ):
209
- return self .blockchain .upgrade_account (account = self )
210
74
211
- def whitelist (self , account ):
212
- """ Add an other account to the whitelist of this account
213
- """
214
- return self .blockchain .account_whitelist (account , lists = ["white" ], account = self )
215
-
216
- def blacklist (self , account ):
217
- """ Add an other account to the blacklist of this account
218
- """
219
- return self .blockchain .account_whitelist (account , lists = ["black" ], account = self )
220
-
221
- def nolist (self , account ):
222
- """ Remove an other account from any list of this account
223
- """
224
- return self .blockchain .account_whitelist (account , lists = [], account = self )
225
-
226
-
227
- class AccountUpdate (dict , BlockchainInstance ):
75
+ @BlockchainInstance .inject
76
+ class AccountUpdate (GrapheneAccountUpdate ):
228
77
""" This purpose of this class is to keep track of account updates
229
78
as they are pushed through by :class:`bitshares.notify.Notify`.
230
79
@@ -244,26 +93,4 @@ class AccountUpdate(dict, BlockchainInstance):
244
93
245
94
"""
246
95
247
- def __init__ (self , data , * args , ** kwargs ):
248
- BlockchainInstance .__init__ (self , * args , ** kwargs )
249
- if isinstance (data , dict ):
250
- super (AccountUpdate , self ).__init__ (data )
251
- else :
252
- account = Account (data , blockchain_instance = self .blockchain )
253
- update = self .blockchain .rpc .get_objects (
254
- ["2.6.%s" % (account ["id" ].split ("." )[2 ])]
255
- )[0 ]
256
- super (AccountUpdate , self ).__init__ (update )
257
-
258
- @property
259
- def account (self ):
260
- """ In oder to obtain the actual
261
- :class:`bitshares.account.Account` from this class, you can
262
- use the ``account`` attribute.
263
- """
264
- account = Account (self ["owner" ], blockchain_instance = self .blockchain )
265
- account .refresh ()
266
- return account
267
-
268
- def __repr__ (self ):
269
- return "<AccountUpdate: {}>" .format (self ["owner" ])
96
+ account_class = Account
0 commit comments