forked from miracle2k/ripple-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_transaction.py
68 lines (57 loc) · 1.86 KB
/
parse_transaction.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
from __future__ import print_function
import os
import sys
import json
import websocket
from ripple import Transaction, PaymentTransaction, TransactionSubscriptionMessage
def main():
# Provide transaction via stdin
if not sys.stdin.isatty():
analyze_transaction(sys.stdin.read())
return
# Or file
if len(sys.argv) > 1:
analyze_transaction(open(sys.argv[1]).read())
return
# Or follow the server stream
ws = websocket.create_connection(
os.environ.get('RIPPLE_URI', 'wss://s1.ripple.com'), timeout=20)
ws.send('{"command":"subscribe","streams":["transactions"]}')
print(ws.recv())
while True:
message = ws.recv()
try:
analyze_transaction(message)
print()
except:
print(json.dumps(message, indent=2))
raise
def analyze_transaction(txstr):
txdata = json.loads(txstr)
if 'transaction' in txdata:
tx = TransactionSubscriptionMessage(txdata).transaction
elif 'result' in txdata:
tx = Transaction(txdata['result'])
else:
tx = Transaction(txdata)
data = [tx.type.__name__]
if not tx.successful:
data.append('unsuccessful')
else:
if tx.type == PaymentTransaction:
data.append(tx.hash)
data.append('{0} {1}/{2}'.format(*tx.amount_received))
data.append('to: %s' % tx.Destination)
if not tx.is_xrp_received:
data.append('new balance: %s' % tx.recipient_balance)
data.append('of limit: %s' % tx.recipient_trust_limit)
data.append('path: intermediaries={intermediaries}, offers={offers}'.format(
**tx.analyze_path()
))
else:
data.append('TODO: parse')
# Print the collected info
print(data[0])
for line in data[1:]:
print(' %s' % line)
main()