@@ -17,52 +17,98 @@ def offers_response(response_dict):
1717 if len (response_dict [field ]) != 1 :
1818 return (None , 'Multiple values for field ' + field )
1919
20+ filterActive = True
21+ if 'onlyActive' in response_dict :
22+ try :
23+ if response_dict ['onlyActive' ][0 ] in [0 ,False ,'false' ]:
24+ filterActive = False
25+ except :
26+ return (None , 'Query field "onlyActive" accepts value true or false' )
27+
28+
2029 if response_dict ['type' ][0 ].upper () == "TIME" :
2130 time = int (response_dict ['time' ][0 ]) if 'time' in response_dict else 86400
22- data = filterOffersByTime ( response_dict ['currencyType' ][0 ] , time )
31+ data = filterOffersByTime ( response_dict ['currencyType' ][0 ] , time , filterActive )
2332 else :
2433 address_arr = json .loads (response_dict ['address' ][0 ])
25- data = filterOffers (address_arr ) if type ( address_arr ) == type ( [] ) else { 'ERR' : 'Address field must be a list or array type' }
34+ data = filterOffers (address_arr , filterActive ) if type ( address_arr ) == type ( [] ) else { 'ERR' : 'Address field must be a list or array type' }
2635
2736 response_status = 'OK'
2837 response = '{"status":"' + response_status + '", "data":' + str (json .dumps (data )) + '}'
2938
3039 return (response , None )
3140
32- def filterOffersByTime ( currency_type , time_seconds ):
41+ def filterOffersByTime ( currency_type , time_seconds , filter ):
3342 #filter by currency
34- currency = ('1' if (currency_type in ['OMNI' ,'OMN' ,'MSC' ] ) else '2' )
43+
44+ try :
45+ currency = int (currency_type )
46+ except :
47+ try :
48+ if (currency_type in ['OMNI' ,'OMN' ,'MSC' ]):
49+ currency = 1
50+ else :
51+ return []
52+ except :
53+ return []
54+
3555
3656 atleast_now = int ( str ( int (time .time () - time_seconds ) ) + '000' )
3757
58+ eq = ""
59+ if filter :
60+ eq = " and ao.offerstate='active'"
61+
3862 ROWS = dbSelect ("select ao.*,t.txhash,t.protocol,t.txdbserialnum,t.txtype,t.txversion,t.ecosystem,t.txrecvtime,t.txstate,t.txerrorcode,"
3963 "t.txblocknumber,t.txseqinblock,txj.txdbserialnum,txj.protocol,txj.txdata "
4064 "from activeoffers ao, transactions t, txjson txj where ao.propertyidselling=%s and ao.propertyiddesired='0' and "
41- "ao.createtxdbserialnum=t.txdbserialnum and ao.createtxdbserialnum=txj.txdbserialnum" , [currency ])
65+ "ao.createtxdbserialnum=t.txdbserialnum and ao.createtxdbserialnum=txj.txdbserialnum" + str ( eq ) , [currency ])
4266
4367 response = [ mapSchema (row ) for row in ROWS if int (mapSchema (row )['tx_time' ]) > atleast_now ]
4468
4569 return sorted (response , key = lambda x : int (x ['tx_time' ]) )
46-
70+
71+
72+ def getName (propertyid ):
73+ if int (propertyid ) == 1 :
74+ name = 'Omni Token #1'
75+ elif int (propertyid ) == 2 :
76+ name = 'Test Omni Token #2'
77+ else :
78+ try :
79+ ROWS = dbSelect ("select propertyname from smartproperties where protocol='Omni' and propertyid=%s" ,[int (propertyid )])
80+ name = ROWS [0 ][0 ]+ " #" + str (propertyid )
81+ except :
82+ name = "#" + str (propertyid )
83+ return name
84+
4785def mapSchema (row ):
4886 #print row, 'row'
4987 try :
5088 rawdata = json .loads (row [- 1 ])
5189 except TypeError :
5290 rawdata = row [- 1 ]
5391
54- #We only map tx21 and tx22
5592 if row [- 11 ] == 20 :
5693 #print row
5794 ppc = Decimal ( rawdata ['bitcoindesired' ] ) / Decimal ( rawdata ['amount' ] )
5895 color = getcolor (row [10 ])
96+
97+ #print rawdata
98+
99+ if rawdata ['divisible' ]:
100+ amount_available = '%.8f' % ( Decimal (row [1 ]) / Decimal (1e8 ) )
101+ else :
102+ amount_available = str (row [1 ])
103+
59104 response = {
60105 'action' : - 1 , #don't have this data
61106 'block' : str (row [- 5 ]),
62107 'currencyId' : str (rawdata ['propertyid' ]),
63- 'currency_str' : 'Omni Token' if str (rawdata ['propertyid' ]) == '1' else 'Test Omni Token' ,
108+ 'propertyid' : rawdata ['propertyid' ],
109+ 'currency_str' : getName (rawdata ['propertyid' ]),
64110 'formatted_amount' : str (rawdata ['amount' ]),
65- 'formatted_amount_available' : '%.8f' % ( Decimal ( row [ 1 ]) / Decimal ( 1e8 ) ) ,
111+ 'formatted_amount_available' : amount_available ,
66112 'formatted_bitcoin_amount_desired' : '%.8f' % ( Decimal (row [2 ]) / Decimal (1e8 ) ),
67113 'formatted_block_time_limit' : str (rawdata ['timelimit' ]),
68114 'formatted_fee_required' : str (rawdata ['feerequired' ]),
@@ -71,32 +117,40 @@ def mapSchema(row):
71117 'from_address' : rawdata ['sendingaddress' ],
72118 'tx_type_str' : "Sell offer" ,
73119 'color' : color ,
120+ 'status' :row [10 ],
74121 'invalid' : False if rawdata ['valid' ] == True else True ,
75122 'to_address' : "sell offer" ,
76123 'tx_hash' : rawdata ['txid' ],
77124 'tx_time' : str (rawdata ['blocktime' ]) + '000'
78125 }
79126 else :
127+ #We only map tx21 and tx22
80128 sellofferdata = getsell (str (row [3 ]))
81129 try :
82130 selljson = json .loads (sellofferdata [- 1 ])
83131 except TypeError :
84132 selljson = sellofferdata [- 1 ]
85133
86134 ppc = Decimal ( selljson ['bitcoindesired' ] ) / Decimal ( selljson ['amount' ] )
87- remaining = Decimal (row [1 ]) / Decimal (1e8 )
135+
136+ if selljson ['divisible' ]:
137+ remaining = Decimal (row [1 ]) / Decimal (1e8 )
138+ else :
139+ remaining = Decimal (row [1 ])
140+
88141 response = {
89142 'block' : str (row [- 5 ]),
90143 'status' : 'valid' if row [5 ] == 'unpaid' or row [5 ] == 'paid-partial' else 'closed' ,
91144 'currencyId' : str (rawdata ['propertyid' ]),
92- 'currency_str' : 'Omni Token' if str (rawdata ['propertyid' ]) == '1' else 'Test Omni Token' ,
145+ 'propertyid' : rawdata ['propertyid' ],
146+ 'currency_str' : getName (rawdata ['propertyid' ]),
93147 'formatted_amount' : '%.8f' % remaining ,
94148 'sell_offer_txid' : selljson ['txid' ],
95149 #'formatted_amount_available': str( row[1] / Decimal(1e8) ),
96150 #'formatted_bitcoin_amount_desired': str( row[2] / Decimal(1e8) ),
97151 'formatted_price_per_coin' : '%.8f' % ppc ,
98152 'bitcoin_required' : '%.8f' % ( Decimal ( ppc ) * Decimal ( remaining ) ),
99- 'payment_expired' : False ,
153+ 'payment_expired' : row [ 7 ] ,
100154 'from_address' : rawdata ['sendingaddress' ],
101155 'tx_type_str' : 'Sell accept' ,
102156 'color' : "bgc-accept" , #needed for compatibility
@@ -135,18 +189,22 @@ def genQs(prefix, tbl_abbr, field, array):
135189 qs += prefix + ' ' + tbl_abbr + '.' + field + '=\' ' + entry + '\' ' # "and/or" table abbrev "." fieldname = next address
136190 return qs + ') '
137191
138- def filterOffers (addresses ):
192+ def filterOffers (addresses , filter ):
139193 #Returns all *ACTIVE* accepts and offers for a given address
140194 offers = {}
141195
142196 #Query all active offers
143197 qs = genQs ('or' , 'ao' , 'seller' , addresses )
144198
199+ eq = ""
200+ if filter :
201+ eq = " and ao.offerstate='active'"
202+
145203 ROWS = dbSelect ("select ao.*,t.txhash,t.protocol,t.txdbserialnum,t.txtype,t.txversion,t.ecosystem,t.txrecvtime,t.txstate,t.txerrorcode,"
146204 "t.txblocknumber,t.txseqinblock,txj.txdbserialnum,txj.protocol,txj.txdata "
147205 "from activeoffers ao, transactions t, txjson txj where " + qs + \
148- " and offerstate='active' and ao.propertyiddesired='0' and ao.createtxdbserialnum=t.txdbserialnum "
149- "and ao.createtxdbserialnum=txj.txdbserialnum" )
206+ " and ao.propertyiddesired='0' and ao.createtxdbserialnum=t.txdbserialnum "
207+ "and ao.createtxdbserialnum=txj.txdbserialnum" + str ( eq ) )
150208
151209 #print query
152210
@@ -157,22 +215,26 @@ def filterOffers(addresses):
157215 jsondata = row [- 1 ]
158216
159217 address = jsondata ['sendingaddress' ]
160- currency = 'OMNI' if jsondata ['propertyid' ] == 1 else 'T-OMNI'
218+ #pid = jsondata['propertyid']
219+ #currency = 'PROD' if (pid != 2 and pid <2147483650) else 'TEST'
161220
162221 if address not in offers : offers [ address ] = {}
163- if 'offer_tx' not in offers [ address ]: offers [ address ]['offer_tx' ] = {}
222+ if 'offer_tx' not in offers [ address ]: offers [ address ]['offer_tx' ] = []
164223
165- offers [ address ]['offer_tx' ][ currency ] = mapSchema (row )
224+ offers [ address ]['offer_tx' ]. append ( mapSchema (row ) )
166225 #only one active offer per address
167226
168227 #Query all active accepts
169228 qs = genQs ('or' , 'oa' , 'buyer' , addresses )
170229
230+ eq = ""
231+ if filter :
232+ eq = " and expiredstate='f'"
233+
171234 ROWS = dbSelect ("select oa.*,t.txhash,t.protocol,t.txdbserialnum,t.txtype,t.txversion,t.ecosystem,t.txrecvtime,t.txstate,t.txerrorcode,"
172235 "t.txblocknumber,t.txseqinblock,txj.txdbserialnum,txj.protocol,txj.txdata "
173236 "from offeraccepts oa, transactions t, txjson txj where " + qs + \
174- " and expiredstate='f' and oa.linkedtxdbserialnum=t.txdbserialnum "
175- "and oa.linkedtxdbserialnum=txj.txdbserialnum" )
237+ " and oa.linkedtxdbserialnum=t.txdbserialnum and oa.linkedtxdbserialnum=txj.txdbserialnum" + str (eq ))
176238
177239 #print query
178240
@@ -182,12 +244,13 @@ def filterOffers(addresses):
182244 except TypeError :
183245 jsondata = row [- 1 ]
184246 address = jsondata ['referenceaddress' ]
185- currency = 'OMNI' if jsondata ['propertyid' ] == 1 else 'T-OMNI'
247+ #pid = jsondata['propertyid']
248+ #currency = 'PROD' if (pid != 2 and pid <2147483650) else 'TEST'
186249
187250 if address not in offers : offers [ address ] = {}
188- if 'accept_tx' not in offers [ address ]: offers [ address ]['accept_tx' ] = {}
251+ if 'accept_tx' not in offers [ address ]: offers [ address ]['accept_tx' ] = []
189252
190- offers [ address ]['accept_tx' ][ currency ] = mapSchema (row )
253+ offers [ address ]['accept_tx' ]. append ( mapSchema (row ) )
191254 #only one active offer per address
192255
193256 return offers
0 commit comments