-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwallet.py
167 lines (134 loc) · 4.95 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import json
from util import send_request
def query_supported_chains():
"""
查询支持的区块链
https://www.okx.com/zh-hans/web3/build/docs/waas/walletapi-api-get-supported-blockchain
"""
req_path = "/api/v5/wallet/chain/supported-chains"
resp = send_request(req_path, method="GET")
return resp.json()
def save_supported_chain():
chains = query_supported_chains()['data']
data = [{x["name"]: x for x in chains}]
with open('chains.json', 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
def query_token_price(chainIndex, tokenAddress=""):
"""
批量查询币种的实时价格。
:return:
"""
req_path = "/api/v5/wallet/token/current-price"
params = [{
"chainIndex": chainIndex,
"tokenAddress": tokenAddress
}]
resp = send_request(req_path, method="POST", params=params)
return resp.json()
def query_historical_price():
"""
查询某个币种的历史价格。
https://www.okx.com/zh-hans/web3/build/docs/waas/walletapi-api-get-historical-price
:return:
"""
req_path = "/api/v5/wallet/token/historical-price"
params = {
"chainIndex": "1",
"limit": "5",
"begin": "1700040600000",
"period": "5m"
}
resp = send_request(req_path, method="GET", params=params)
return resp.json()
def query_token_detail(chainIndex, tokenAddress=""):
"""
查询单个币种的其他信息,诸如币种合约地址、代币官网 URL、社媒信息。
https://www.okx.com/zh-hans/web3/build/docs/waas/walletapi-api-token-detail
:return:
"""
req_path = "/api/v5/wallet/token/token-detail"
params = {
"chainIndex": chainIndex,
"tokenAddress": tokenAddress
}
resp = send_request(req_path, method="GET", params=params)
return resp.json()
def query_total_value_by_address(address, chains):
"""
获取地址下全量 token 和 Defi 资产总余额,支持过滤垃圾空投代币。
https://www.okx.com/zh-hans/web3/build/docs/waas/walletapi-api-token-detail
:return:
"""
req_path = "/api/v5/wallet/asset/total-value-by-address"
params = {
"address": address,
"chains": chains
}
resp = send_request(req_path, method="GET", params=params)
return resp.json()
def query_all_token_balances_by_address(address, chains):
"""
查询地址持有的多个链或指定链的 token 余额列表。
https://www.okx.com/zh-hans/web3/build/docs/waas/walletapi-api-all-token-balances-by-address
:return:
"""
req_path = "/api/v5/wallet/asset/all-token-balances-by-address"
params = {
"address": address,
"chains": chains
}
resp = send_request(req_path, method="GET", params=params)
return resp.json()
def query_token_balances_by_address(address, token_addresses):
"""
查询地址持有的多个链或指定链的 token 余额列表。
token_addresses: [{chainIndex: str}, {address: str}]
https://www.okx.com/zh-hans/web3/build/docs/waas/walletapi-api-specific-token-balance-by-address
:return:
"""
req_path = "/api/v5/wallet/asset/token-balances-by-address"
params = {
"address": address,
"tokenAddresses": token_addresses
}
resp = send_request(req_path, method="POST", params=params)
return resp.json()
def query_approvals(addresses):
"""
分页查询单个地址,授权了哪些项目。以及每个授权项目下,授权的资产和额度。
addresses: [{chainIndex: str}, {address: str}]
https://www.okx.com/zh-hans/web3/build/docs/waas/walletapi-api-specific-token-balance-by-address
:return:
"""
req_path = "/api/v5/wallet/security/approvals"
params = {
"addresses": addresses
}
resp = send_request(req_path, method="POST", params=params)
return resp.json()
def query_transactions_by_address(address, chains):
"""
查询地址维度下的交易历史,按时间倒序排序。
:param address:
:param chains:
:return:
"""
req_path = "/api/v5/wallet/post-transaction/transactions-by-address"
data = {
"address": address,
"chains": chains
}
resp = send_request(req_path, method="GET", params=data)
return resp.json()
if __name__ == '__main__':
# data = query_token_price("1")
# data = query_historical_price()
# data = query_token_detail(chainIndex="501", tokenAddress="GJtJuWD9qYcCkrwMBmtY1tpapV1sKfB2zUv9Q4aqpump")
# data = query_total_value_by_address(address='8yNxK642RR1vyx7WoLou8Jxjxhwsedv8BfTYc833KNRg', chains='501')
address = "0x4679239cad2e33c7516924f73cec1365ab01a274"
chainIndex = "1"
# data = query_all_token_balances_by_address(address, chainIndex)
# addresses = [{"chainIndex": chainIndex, "address": address}]
# data = query_approvals(addresses=addresses)
data = query_transactions_by_address(address, chainIndex)
print(json.dumps(data, indent=2, ensure_ascii=False))