-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWallet.py
129 lines (100 loc) · 3.87 KB
/
Wallet.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
from configparser import ConfigParser
from web3 import Web3
from tabulate import tabulate
from defy.Utilities import Utilities
from defy.PriceFinder import PriceFinder
import requests
import json
import os
class Wallet:
def __init__(self, priceFinder):
self.config = ConfigParser()
self.config.read("./config.ini")
self.networkProvider = self.config["DEFAULT"]["network_provider"]
self.transactionEndpoint = self.config["DEFAULT"][
"bscscan_transaction_endpoint"
]
self.headers = ["Wallet", "Price", "Balance", "Balance ($)"]
self.web3 = Web3(Web3.HTTPProvider(self.networkProvider))
self.priceFinder = priceFinder
with open("abis/token_abi.json", "r") as abi_definition:
self.tokenAbi = json.load(abi_definition)
def getTokenBalance(self, contractAddress, walletAddress):
contract = self.web3.eth.contract(
abi=self.tokenAbi, address=Web3.toChecksumAddress(contractAddress)
)
return Utilities.formatBalance(
self.web3, contract.functions.balanceOf(walletAddress).call()
)
def getTokenName(self, contractAddress):
contract = self.web3.eth.contract(
abi=self.tokenAbi, address=Web3.toChecksumAddress(contractAddress)
)
return contract.functions.name().call()
def getSymbol(self, contractAddress):
contract = self.web3.eth.contract(
abi=self.tokenAbi, address=Web3.toChecksumAddress(contractAddress)
)
return contract.functions.symbol().call()
def getWalletTokens(self, walletAddress):
transactions = {}
endpoint = self.transactionEndpoint + "&address=" + walletAddress
r = requests.get(endpoint)
for item in r.json()["result"]:
transactions[item["tokenSymbol"]] = item["contractAddress"]
return transactions
def getBNBBalance(self, walletAddress, hideSmallBal=True):
bal = Utilities.formatBalance(
self.web3, self.web3.eth.get_balance(walletAddress)
)
if bal > 0:
price = self.priceFinder.getTokenPrice("BNB")
balInDollar = bal * price
if not (hideSmallBal and balInDollar < 1):
return [
{
"symbol": "BNB",
"price": price,
"bal": bal,
"balInDollar": balInDollar,
}
]
return []
def getWallet(self, walletAddress, hideSmallBal=True):
wallet = []
tokens = self.getWalletTokens(walletAddress)
walletAddress = Web3.toChecksumAddress(walletAddress)
bnbBalance = self.getBNBBalance(walletAddress, hideSmallBal)
for symbol in tokens:
bal = self.getTokenBalance(tokens[symbol], walletAddress)
if bal > 0:
price = self.priceFinder.getTokenPrice(symbol)
balInDollar = bal * price
if hideSmallBal and balInDollar < 1:
continue
wallet.append(
{
"symbol": symbol,
"price": price,
"bal": bal,
"balInDollar": balInDollar,
}
)
return wallet + bnbBalance
def walletToTable(self, wallet):
tabulateWallet = []
for token in wallet:
tabulateWallet.append(
[token["symbol"], token["price"], token["bal"], token["balInDollar"]]
)
return tabulateWallet
def displayWallet(self, wallet):
print(
tabulate(
self.walletToTable(wallet),
self.headers,
floatfmt=(".f", ".2f", ".4f", ".2f"),
tablefmt="simple",
),
"\n",
)