-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.py
executable file
·103 lines (79 loc) · 2.83 KB
/
list.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
#!/usr/bin/env python3
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
# pylint: disable=missing-module-docstring
from argparse import ArgumentParser, Namespace
from decimal import Decimal
from typing import Optional
from stellar_sdk import Asset, Server
import yaml
SERVER = Server("https://horizon.stellar.org")
SMITHY_ACCOUNT = "GDPHAKGLJ3B56BK4CZ2VMTYEDI6VZ2CTHUHSFAFSPGSTJHZEI3ATOKEN"
def get_account():
return SERVER.accounts().account_id(SMITHY_ACCOUNT).call()
def get_holder(code: str, issuer: str) -> Optional[str]:
accounts = (
SERVER.accounts().for_asset(Asset(code, issuer)).call()["_embedded"]["records"]
)
holders = [
account["id"]
for account in accounts
for balance in account["balances"]
if balance["balance"] == "0.0000001"
and balance["asset_code"] == code
and balance["asset_issuer"] == issuer
]
return holders[0] if holders else None
def parse_args() -> Namespace:
arp = ArgumentParser()
arp.add_argument("-i", "--issuer", action="store_true", help="Show token issuers")
arp.add_argument("-o", "--other", action="store_true", help="Show other holders")
return arp.parse_args()
def check_insert(adict: dict[str, str], k: str, value: str) -> None:
if k in adict:
raise ValueError("Asset code conflict")
adict[k] = value
def shorten_address(address: str) -> str:
return "*" + address[-4:]
def main() -> None:
args = parse_args()
smithy = get_account()
balances = smithy["balances"]
trust: dict[str, str] = {}
hold: dict[str, str] = {}
for asset in balances:
if asset["asset_type"] == "native":
continue
code = asset["asset_code"]
balance_raw = int(Decimal(asset["balance"]) * Decimal(10_000_000))
issuer = asset["asset_issuer"]
if balance_raw == 0:
check_insert(trust, code, issuer)
elif balance_raw == 1:
check_insert(hold, code, issuer)
else:
raise NotImplementedError
print(
yaml.dump(
{
"Smithy trusts": ", ".join(
(f"{code}-{issuer}" for code, issuer in trust.items())
if args.issuer
else trust.keys()
),
"Smithy holds": ", ".join(
(f"{code}-{issuer}" for code, issuer in hold.items())
if args.issuer
else hold.keys()
),
}
)
)
if args.other:
print("Other holders:")
for code, issuer in trust.items():
asset = f"{code}-{issuer}" if args.issuer else code
print(f" {asset:11} is held by ", end="")
print(shorten_address(get_holder(code, issuer)))
if __name__ == "__main__":
main()