-
Notifications
You must be signed in to change notification settings - Fork 48
/
csvtools.py
310 lines (263 loc) · 15 KB
/
csvtools.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import csv
import datetime
import decimal
import sys
from rpcclient import *
from omniutils import *
def dumpblocks_csv(csvwb, block_data, Protocol, block_height, txcount):
BlockTime = datetime.datetime.utcfromtimestamp(block_data['result']['time'])
version = block_data['result']['version'];
if block_height > 0:
prevblockhash = block_data['result']['previousblockhash'];
else:
prevblockhash = '0000000000000000000000000000000000000000000000000000000000000000'
merkleroot = block_data['result']['merkleroot'];
blockhash = block_data['result']['hash'];
bits = block_data['result']['bits'];
nonce = block_data['result']['nonce'];
size = block_data['result']['size'];
row={'BlockNumber': block_height, 'Protocol': Protocol, 'BlockTime': BlockTime, 'Version': version, 'BlockHash': blockhash,
'PrevBlock': prevblockhash, 'MerkleRoot': merkleroot, 'Bits': bits, 'Nonce': nonce, 'Size': size,'TxCount': txcount}
csvwb.writerow(row)
def dumptxaddr_csv(csvwb, rawtx, Protocol, TxDBSerialNum):
TxHash = rawtx['result']['txid']
if Protocol == "Bitcoin":
PropertyID=0
#process all outputs
for output in rawtx['result']['vout']:
#Make sure we have readable output addresses to actually use
if 'addresses' in output['scriptPubKey']:
AddressRole="recipient"
AddressTxIndex=output['n']
#store values as satoshi/willits etc''. Client converts
BalanceAvailableCreditDebit=int(decimal.Decimal(output['value'])*decimal.Decimal("1e8"))
#multisigs have more than 1 address, make sure we find/credit all multisigs for a tx
for addr in output['scriptPubKey']['addresses']:
row={'Address': addr, 'PropertyID': PropertyID, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': BalanceAvailableCreditDebit}
csvwb.writerow(row)
#process all inputs, Start AddressTxIndex=0 since inputs don't have a Index number in json and iterate for each input
AddressTxIndex=0
for input in rawtx['result']['vin']:
#check if we have previous input txids we need to lookup or if its a coinbase (newly minted coin ) which needs to be skipped
if 'txid' in input:
AddressRole="sender"
#existing json doesn't have raw address only prev tx. Get prev tx to decipher address/values
prevtx=getrawtransaction(input['txid'])
BalanceAvailableCreditDebit=int(decimal.Decimal(prevtx['result']['vout'][input['vout']]['value'])*decimal.Decimal("1e8")*decimal.Decimal(-1))
#BalanceAvailableCreditDebit=int(prevtx['result']['vout'][input['vout']]['value'] * 1e8 * -1)
#multisigs have more than 1 address, make sure we find/credit all multisigs for a tx
for addr in prevtx['result']['vout'][input['vout']]['scriptPubKey']['addresses']:
row={'Address': addr, 'PropertyID': PropertyID, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': BalanceAvailableCreditDebit}
csvwb.writerow(row)
AddressTxIndex+=1
elif Protocol == "Mastercoin":
AddressTxIndex=0
AddressRole="sender"
type=get_TxType(rawtx['result']['type'])
BalanceAvailableCreditDebit=""
BalanceReservedCreditDebit=""
BalanceAcceptedCreditDebit=""
Address = rawtx['result']['sendingaddress']
#Check if we are a DEx Purchase/payment. Format is a littler different and variables below would fail if we tried.
if type != -22:
PropertyID= rawtx['result']['propertyid']
if rawtx['result']['divisible']:
value=int(decimal.Decimal(rawtx['result']['amount'])*decimal.Decimal(1e8))
else:
value=int(rawtx['result']['amount'])
value_neg=(value*-1)
if type == 0:
#Simple Send
#debit the sender
row={'Address': Address, 'PropertyID': PropertyID, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': value_neg,
'BalanceReservedCreditDebit': BalanceReservedCreditDebit, 'BalanceAcceptedCreditDebit': BalanceAcceptedCreditDebit}
csvwb.writerow(row)
#credit the receiver
Address = rawtx['result']['referenceaddress']
AddressRole="recipient"
BalanceAvailableCreditDebit=value
#elif type == 2:
#Restricted Send does nothing yet?
#elif type == 3:
#Send To Owners
#Do something smart
#return
elif type == 20:
#DEx Sell Offer
#Move the amount from Available balance to reserved for Offer
##Sell offer cancel doesn't display an amount from core, not sure what we do here yet
AddressRole='seller'
BalanceAvailableCreditDebit = value_neg
BalanceReservedCreditDebit = value
#elif type == 21:
#MetaDEx: Offer/Accept one Master Protocol Coins for another
#return
elif type == 22:
#DEx Accept Offer
#Move the amount from Reserved for Offer to Reserved for Accept
## Mastercore doesn't show payments as MP tx. How do we credit a user who has payed?
#update the buyer
AddressRole='buyer'
BalanceAcceptedCreditDebit = value
row={'Address': Address, 'PropertyID': PropertyID, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': BalanceAvailableCreditDebit,
'BalanceReservedCreditDebit': BalanceReservedCreditDebit, 'BalanceAcceptedCreditDebit': BalanceAcceptedCreditDebit }
csvwb.writerow(row)
AddressRole='seller'
Address = rawtx['result']['referenceaddress']
BalanceAcceptedCreditDebit = value
BalanceReservedCreditDebit = value_neg
elif type == -22:
#DEx Accept Payment
Sender = Address
#process all purchases in the transaction
for payment in rawtx['result']['purchases']:
Receiver = payment['referenceaddress']
PropertyIDBought = payment['propertyid']
#Right now payments are only in btc
#we already insert btc payments in btc processing might need to skip this
PropertyIDPaid = 0
#AddressTxIndex = Do we need to change this?
if getdivisible_MP(PropertyIDBought):
AmountBought=int(decimal.Decimal(payment['amountbought'])*decimal.Decimal(1e8))
else:
AmountBought=int(payment['amountbought'])
AmountBoughtNeg=(AmountBought * -1)
#if (PropertyIDPaid == 0 ) or getdivisible_MP(PropertyIDPaid):
# AmountPaid=int(decimal.Decimal(payment['amountpaid'])*decimal.Decimal(1e8))
#else:
# AmountPaid=int(payment['amountpaid'])
#AmountPaidNeg=(AmountPaid * -1)
#deduct payment from buyer
#AddressRole = 'buyer'
#BalanceAvailableCreditDebit=AmountPaidNeg
#row={'Address': Sender, 'PropertyID': PropertyIDPaid, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
# 'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': BalanceAvailableCreditDebit,
# 'BalanceReservedCreditDebit': BalanceReservedCreditDebit, 'BalanceAcceptedCreditDebit': BalanceAcceptedCreditDebit }
#csvwb.writerow(row)
#Credit payment to seller
#AddressRole = 'seller'
#BalanceAvailableCreditDebit=AmountPaid
#row={'Address': Receiver, 'PropertyID': PropertyIDPaid, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
# 'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': BalanceAvailableCreditDebit,
# 'BalanceReservedCreditDebit': BalanceReservedCreditDebit, 'BalanceAcceptedCreditDebit': BalanceAcceptedCreditDebit }
#csvwb.writerow(row)
#deduct tokens from seller
AddressRole = 'seller'
BalanceAvailableCreditDebit=AmountBoughtNeg
row={'Address': Receiver, 'PropertyID': PropertyIDBought, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': BalanceAvailableCreditDebit,
'BalanceReservedCreditDebit': BalanceReservedCreditDebit, 'BalanceAcceptedCreditDebit': BalanceAcceptedCreditDebit }
csvwb.writerow(row)
#Credit tokens to buyer and reduce their accepted amount by amount bought
AddressRole = 'buyer'
BalanceAvailableCreditDebit=AmountBought
BalanceAcceptedCreditDebut=AmountBoughtNeg
row={'Address': Sender, 'PropertyID': PropertyIDBought, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': BalanceAvailableCreditDebit,
'BalanceReservedCreditDebit': BalanceReservedCreditDebit, 'BalanceAcceptedCreditDebit': BalanceAcceptedCreditDebit }
csvwb.writerow(row)
#end //for payment in rawtx['result']['purchases']
#We've updated all the records in the DEx payment, don't let the last write command run, not needed
return
elif type == 50:
#Fixed Issuance, create property
AddressRole = "issuer"
#update smart property table
insertProperty(rawtx, Protocol)
elif type == 51:
AddressRole = "issuer"
#update smart property table
insertProperty(rawtx, Protocol)
elif type == -51:
#Participating in crowdsale
#dbc.execute("insert into PropertyHistory (Protocol, PropertyID, TxDBSerialNum) Values(%s, %s, %s)", (Protocol, PropertyID, TxDBSerialNum))
#First deduct the amount the participant sent to 'buyin'
AddressRole = 'participant'
BalanceAvailableCreditDebit = value_neg
row={'Address': Address, 'PropertyID': PropertyID, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': BalanceAvailableCreditDebit,
'BalanceReservedCreditDebit': BalanceReservedCreditDebit, 'BalanceAcceptedCreditDebit': BalanceAcceptedCreditDebit }
csvwb.writerow(row)
#Credit the buy in to the issuer
AddressRole = 'issuer'
BalanceAvailableCreditDebit = value
Address= rawtx['result']['referenceaddress']
row={'Address': Address, 'PropertyID': PropertyID, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': BalanceAvailableCreditDebit,
'BalanceReservedCreditDebit': BalanceReservedCreditDebit, 'BalanceAcceptedCreditDebit': BalanceAcceptedCreditDebit }
csvwb.writerow(row)
#Now start updating the crowdsale propertyid balance info
PropertyID = rawtx['result']['purchasedpropertyid']
#add additional functionalty to check/credit the issue when there is a % bonus to issuer
cstx = getcrowdsale_MP(PropertyID)
if cstx['result']['percenttoissuer'] > 0:
if getdivisible_MP(PropertyID):
BalanceAvailableCreditDebit = int(decimal.Decimal(rawtx['result']['amount'])*decimal.Decimal(cstx['result']['tokensperunit'])*(decimal.Decimal(cstx['result']['percenttoissuer'])/decimal.Decimal(100))*decimal.Decimal(1e8))
else:
BalanceAvailableCreditDebit = int(decimal.Decimal(rawtx['result']['amount'])*decimal.Decimal(cstx['result']['tokensperunit'])*decimal.Decimal((cstx['result']['percenttoissuer'])/decimal.Decimal(100)))
row={'Address': Address, 'PropertyID': PropertyID, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': BalanceAvailableCreditDebit,
'BalanceReservedCreditDebit': BalanceReservedCreditDebit, 'BalanceAcceptedCreditDebit': BalanceAcceptedCreditDebit }
csvwb.writerow(row)
#now update with crowdsale specific property details
Address = rawtx['result']['sendingaddress']
if getdivisible_MP(PropertyID):
value=int(decimal.Decimal(rawtx['result']['purchasedtokens'])*decimal.Decimal(1e8))
else:
value=int(rawtx['result']['purchasedtokens'])
value_neg=(value*-1)
BalanceAvailableCreditDebit=value
#elif type == 52:
#promote crowdsale does what?
elif type == 53:
#Close Crowdsale
AddressRole = "issuer"
#update smart property table
insertProperty(rawtx, Protocol)
#write output of the address details
row={'Address': Address, 'PropertyID': PropertyID, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'AddressTxIndex': AddressTxIndex,
'AddressRole': AddressRole, 'BalanceAvailableCreditDebit': BalanceAvailableCreditDebit,
'BalanceReservedCreditDebit': BalanceReservedCreditDebit, 'BalanceAcceptedCreditDebit': BalanceAcceptedCreditDebit }
csvwb.writerow(row)
def dumptx_csv(csvwb, rawtx, Protocol, block_height, seq, dbserialnum):
TxHash = rawtx['result']['txid']
TxBlockTime = datetime.datetime.utcfromtimestamp(rawtx['result']['blocktime'])
TxErrorCode = rawtx['error']
TxSeqInBlock= seq
TxDBSerialNum = dbserialnum
if Protocol == "Bitcoin":
#Bitcoin is only simple send, type 0
TxType=0
TxVersion=rawtx['result']['version']
TxState= "valid"
Ecosystem= ""
TxSubmitTime = datetime.datetime.utcfromtimestamp(rawtx['result']['time'])
elif Protocol == "Mastercoin":
#currently type a text output from mastercore 'Simple Send' and version is unknown
TxType= get_TxType(rawtx['result']['type'])
TxVersion=0
#!!temp workaround, Need to update for DEx Purchases after conversation with MasterCore team
if TxType == -22:
TxState=getTxState(rawtx['result']['purchases'][0]['valid'])
Ecosystem=getEcosystem(rawtx['result']['purchases'][0]['propertyid'])
else:
TxState= getTxState(rawtx['result']['valid'])
Ecosystem=getEcosystem(rawtx['result']['propertyid'])
#Use block time - 10 minutes to approx
#TxSubmitTime = TxBlockTime-datetime.timedelta(minutes=10)
TxSubmitTime=""
#if rawtx['result']['propertyid'] == 2 or ( rawtx['result']['propertyid'] >= 2147483651 and rawtx['result']['propertyid'] <= 4294967295 ):
# Ecosystem= "Test"
#else:
# Ecosystem= "Production"
else:
print "Wrong Protocol? Exiting, goodbye."
exit(1)
row={'TxHash': TxHash, 'Protocol': Protocol, 'TxDBSerialNum': TxDBSerialNum, 'TxType': TxType, 'TxVersion': TxVersion, 'Ecosystem': Ecosystem,
'TxSubmitTime': TxSubmitTime, 'TxState': TxState, 'TxErrorCode': TxErrorCode, 'TxBlockNumber': block_height,
'TxSeqInBlock': TxSeqInBlock}
csvwb.writerow(row)