Higher quality code with more comments

This commit is contained in:
Jarred
2022-09-07 19:51:00 -05:00
parent 5d77a0e959
commit ba7ba3035a
2 changed files with 52 additions and 43 deletions

View File

@@ -3,6 +3,7 @@ import websockets
import json import json
# Using client libraries for ASYNC functions and websockets are needed in python. # Using client libraries for ASYNC functions and websockets are needed in python.
# To install, use terminal command 'pip install asyncio && pip install websockets'
# Handles incoming messages # Handles incoming messages
async def handler(websocket): async def handler(websocket):

View File

@@ -2,64 +2,72 @@ import json
# Helpers to find accounts in AffectedNodes and see how much the balance changed. # Helpers to find accounts in AffectedNodes and see how much the balance changed.
def FindXRPDifference(tx, address): def FindXRPDifference(tx, address):
# Define empty list to put the Node change type in.
affected_nodes = []
# Loop over the AffectedNodes to build a list of affected nodes
for i in tx['meta']['AffectedNodes']: for i in tx['meta']['AffectedNodes']:
if 'CreatedNode' and 'ModifiedNode' in list(i): i = list(i)[0]
# If a CreatedNode and a ModifiedNode both exist, the monitores account has funded another account affected_nodes.append(i)
else:
for i in tx['meta']['AffectedNodes']:
if 'CreatedNode' in affected_nodes and 'ModifiedNode' in affected_nodes:
# If a CreatedNode and a ModifiedNode both exist, the monitored account has funded another account
# If both exist, CreatedNode always comes after AffectedNode, so we can gather it by getting the second entry in the list by using [1]
ledger_entry = tx['meta']['AffectedNodes'][1]['CreatedNode']
if ledger_entry['LedgerEntryType'] == 'AccountRoot' and ledger_entry['NewFields']['Account'] != address: if ledger_entry['LedgerEntryType'] == 'AccountRoot' and ledger_entry['NewFields']['Account'] != address:
new_address = ledger_entry['NewFields']['Account'] new_address = ledger_entry['NewFields']['Account']
balance_drops = int(ledger_entry['NewFields']['Balance']) balance_drops = int(ledger_entry['NewFields']['Balance'])
xrp_amount = (balance_drops / 1000000) xrp_amount = (balance_drops / 1000000)
print("A new account", new_address, "was funded with", xrp_amount, "XRP") print(f"A new account {new_address} was funded with {xrp_amount} XRP")
break return
elif affected_nodes == ['ModifiedNode']:
elif list(i)[0] == 'ModifiedNode':
ledger_entry = i['ModifiedNode'] ledger_entry = i['ModifiedNode']
if ledger_entry['LedgerEntryType'] == 'AccountRoot' and ledger_entry['FinalFields']['Account'] == address: if ledger_entry['LedgerEntryType'] == 'AccountRoot' and ledger_entry['FinalFields']['Account'] == address:
if not ledger_entry['PreviousFields']['Balance']: if not ledger_entry['PreviousFields']['Balance']:
print('Balance didnt change') print("Balance didn't change")
# Subtracts the previous balance from the new balance # Subtracts the previous balance from the new balance
old_balance = int(ledger_entry['PreviousFields']['Balance']) old_balance = int(ledger_entry['PreviousFields']['Balance'])
new_balance = int(ledger_entry['FinalFields']['Balance']) new_balance = int(ledger_entry['FinalFields']['Balance'])
diff_in_drops = (new_balance - old_balance) diff_in_drops = (new_balance - old_balance)
xrp_amount = (diff_in_drops / 1000000) xrp_amount = (diff_in_drops / 1000000)
if xrp_amount > 0: if xrp_amount > 0:
print("Received", xrp_amount, "XRP") print(f"Received {xrp_amount} XRP")
break return
else: else:
print("Spent", abs(xrp_amount), "XRP") print("Spent", abs(xrp_amount), "XRP")
break return
elif list(i)[0] == 'CreatedNode':
elif affected_nodes == ['CreatedNode']:
# If there is no outgoing payment, but an account was created, the account most likely just got funded # If there is no outgoing payment, but an account was created, the account most likely just got funded
ledger_entry = i['CreatedNode'] ledger_entry = i['CreatedNode']
if ledger_entry['LedgerEntryType'] == 'AccountRoot' and ledger_entry['NewFields']['Account'] == address: if ledger_entry['LedgerEntryType'] == 'AccountRoot' and ledger_entry['NewFields']['Account'] == address:
balance_drops = int(ledger_entry['NewFields']['Balance']) balance_drops = int(ledger_entry['NewFields']['Balance'])
xrp_amount = (balance_drops / 1000000) xrp_amount = (balance_drops / 1000000)
print("Received", xrp_amount, "XRP (account funded)") print(f"Received {xrp_amount} XRP (account funded)")
break return
else: else:
print("Did not find address in affected nodes.") print("Did not find address in affected nodes.")
# Check how much XRP was received, if any
def CountXRPReceived(tx, address): def CountXRPReceived(tx, address):
if tx['meta']['TransactionResult'] != 'tesSUCCESS': if tx['meta']['TransactionResult'] != 'tesSUCCESS':
print('Transaction failed') print("Transaction failed")
return return
if tx['transaction']['TransactionType'] == 'Payment': if tx['transaction']['TransactionType'] == 'Payment':
if tx['transaction']['Destination'] != address: if tx['transaction']['Destination'] != address:
print('Not the destination of this payment.') print("Not the destination of this payment.")
return return
if tx['meta']['delivered_amount'] is str: if tx['meta']['delivered_amount'] is int or str:
amount_in_drops = tx['transaction']['Amount'] amount_in_drops = int(tx['transaction']['Amount'])
xrp_amount = (amount_in_drops / 1000000) xrp_amount = (amount_in_drops / 1000000)
print('Received', xrp_amount, 'XRP') print(f"Received {xrp_amount} XRP")
return return
else: else:
print('Received non-XRP currency') print("Received non-XRP currency")
elif tx['transaction']['TransactionType'] == 'PaymentChannelClaim' or 'PaymentChannelFund' or'OfferCreate' or 'CheckCash' or 'EscrowFinish': elif tx['transaction']['TransactionType'] == 'PaymentChannelClaim' or 'PaymentChannelFund' or'OfferCreate' or 'CheckCash' or 'EscrowFinish':
FindXRPDifference(tx, address) FindXRPDifference(tx, address)
else: else:
print('Not a currency-delivering transaction type', tx['transaction']['TransactionType']) print("Not a currency-delivering transaction type", tx['transaction']['TransactionType'])
CountXRPReceived(tx=transaction, address='rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe') CountXRPReceived(tx=transaction, address='rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe')