forked from PeerAssets/pypeerassets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example of finding balance of a given address
- Loading branch information
Showing
1 changed file
with
54 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,58 @@ | ||
"""find <address> card balance on this <deck>""" | ||
|
||
import sys | ||
import pypeerassets as pa | ||
from pypeerassets.protocol import validate_card_issue_modes | ||
provider = pa.RpcNode(testnet=True) | ||
|
||
print("Searching cards on deck {deck}.".format(deck=sys.argv[1])) | ||
deck = pa.find_deck(provider, sys.argv[1])[0] | ||
cards = [i.__dict__ for i in pa.find_card_transfers(provider, deck)] | ||
my_addr = str(sys.argv[2]) # mkXdyMPX8D7EH7CZcYfQEBvoZhp8MEUgdB | ||
|
||
|
||
class Balance: | ||
|
||
received = [] | ||
sent = [] | ||
|
||
@classmethod | ||
def balance(cls): | ||
return sum((i["amount"][0] for i in cls.received)) - sum((i["amount"][0] for i in cls.sent)) | ||
|
||
|
||
# first figure out if cards issued are legit | ||
issues = (i for i in cards if i["type"] == "CardIssue") | ||
issues = list(validate_card_issue_modes(deck, issues)) | ||
c = (i for i in cards if i in issues) # drop invalid issued cards | ||
|
||
Balance.received = [i for i in c if i["receiver"][0] == my_addr] | ||
Balance.sent = [i for i in c if i["sender"] == my_addr] | ||
|
||
print("Balance of {address} address is {balance}".format(address=my_addr, | ||
balance=Balance.balance())) | ||
from pypeerassets.provider import RpcNode | ||
from pypeerassets.protocol import DeckState | ||
|
||
provider = RpcNode(testnet=True) | ||
state = None | ||
|
||
|
||
|
||
def get_state(deck): | ||
deck = pa.find_deck(provider,deck)[0] | ||
cards = pa.find_card_transfers(provider,deck) | ||
return DeckState(cards) | ||
|
||
def get_balance(state, address): | ||
print(state.balances[address]) | ||
|
||
def get_balances(state): | ||
print(state.balances) | ||
|
||
def get_total(state): | ||
print(state.total) | ||
|
||
def get_checksum(state): | ||
print(state.checksum) | ||
|
||
def get_burned(state): | ||
print(state.burned) | ||
|
||
while True: | ||
try: | ||
deck = input("Deck Identifier: ") | ||
state = get_state(deck) | ||
print("**** {} Deck loaded ****".format(deck)) | ||
print("To print all address balances type 'all' in Address input") | ||
break | ||
except IndexError: | ||
print('{"error": "Deck not found, try again"}') | ||
continue | ||
|
||
while True: | ||
|
||
try: | ||
address = input("Address: ") | ||
|
||
if address == 'all': | ||
get_balances(state) | ||
else: | ||
get_balance(state,address) | ||
continue | ||
except KeyError: | ||
print('{"error": "Address not found, try another or subscribe to deck"}') | ||
continue | ||
else: | ||
break | ||
|