-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsample.py
267 lines (222 loc) · 9.22 KB
/
sample.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Global setting to indicate if test net
# If not specify and pass to "Class", default value would be "False"
IS_TEST_NET = False
# ---------------- API Sample -------------------
from binance_dex.api import BinanceChainClient, api_types_instance
# Create API Client instance
api_client = BinanceChainClient(is_test_net=IS_TEST_NET)
# # Get Block time
# print('Block Time: ')
# print(api_client.get_block_time())
#
# # Get Node Info
# print('Node Info: ')
# print(api_client.get_node_info())
#
# # Get Validator
# print('Validators: ')
# print(api_client.get_validators())
# # Get Validator
# print('Peers: ')
# print(api_client.get_peers())
#
# # Get Listing Tokens
# print('Token Listed: ')
# print(api_client.get_tokens())
#
# # Get Account Info By Address
# address = 'tbnb1r4gc5ftrkr9ez2khph4h5xxd0mf0hd75jf06gw'
# print('Account info for %s:' % address)
# print(api_client.get_account_info_by_address(address=address))
# # Get Account Sequence By Address
# address = 'tbnb1r4gc5ftrkr9ez2khph4h5xxd0mf0hd75jf06gw'
# print('Account sequence for %s:' % address)
# print(api_client.get_account_sequence_by_address(address=address))
#
# # Get Transaction Info By TxId
# tx_id = '35B8D4070200FFBE045432AC9D87232BEC1FFAD9E6A6C8979CE2FE631B644B9E'
# print("Transaction details with txid: %s:" % tx_id)
# print(api_client.get_transaction(tx_hash=tx_id))
#
# # Get Market Trading Pairs List
# print('Trading Pairs:')
# print(api_client.get_markets())
#
# # Get Binance Chain Fees
# print('Fees:')
# print(api_client.get_fees())
# # Get order book
# print('Orders:')
# print(api_client.get_depth("NNB-0AD_BNB", 5))
# # Get KLines
# trading_pair = 'DEX.B-C72_BNB'
# print("Last 100 4hour Klines with trading Pair '%s':" % trading_pair)
# print(api_client.get_klines(trading_pair=trading_pair,
# interval=api_types_instance.KLine.interval_4hour,
# limit=100))
# # Get order by ID.
# order_id = ''
# print('Order ID %s:' % order_id)
# print(api_client.get_order_by_id(order_id))
# # Get open orders.
# address = 'tbnb1r4gc5ftrkr9ez2khph4h5xxd0mf0hd75jf06gw'
# print('Open orders for %s:' % address)
# print(api_client.get_order_open(address))
#
# # Get closed orders.
# address = 'tbnb1r4gc5ftrkr9ez2khph4h5xxd0mf0hd75jf06gw'
# print('Closed orders for %s:' % address)
# print(api_client.get_order_closed(address))
#
# Get Transactions
from datetime import datetime
address = 'bnb1jxfh2g85q3v0tdq56fnevx6xcxtcnhtsmcu64m'
print('Transfer Transactions for BNB during 2019.5.10 - 2019.5.16 for address %s' % address)
print(api_client.transactions(address=address,
tx_asset='BNB',
tx_type=api_types_instance.Transactions.type_transfer,
start_time=datetime(2019, 5, 10),
end_time=datetime(2019, 5, 17)))
# ------------ End of API Sample ----------------
# -------------- Crypto Sample ------------------
from binance_dex.crypto import BinanceChainCrypto
# Create crypto instance
crypto_instance = BinanceChainCrypto(is_test_net=IS_TEST_NET)
# # Generate Mnemonic words
# mnemonic_words = crypto_instance.generate_mnemonic()
# print("Generating Mnemonic Words: ")
# print(mnemonic_words)
#
# Generate Private Key, Public Address and mnemonic
# key = crypto_instance.generate_key()
# print('Generating Private Key / Public Key / Mnemonic words: ')
# print(key)
#
# # Generate Private Key, Public Address from mnemonic
# mnemonic_words = crypto_instance.generate_mnemonic()
# key = crypto_instance.generate_key_from_mnemonic(mnemonic_words)
# print("Mnemonic Words: %s" % mnemonic_words)
# print("Keys: %s" % key)
#
# # Generat Bunch of Private Keys, Public Address from one set of Mnemonic words
# mnemonic_words = crypto_instance.generate_mnemonic()
# keys = crypto_instance.generate_keys_from_mnemonic(mnemonic_words, 10)
# print("Mnemonic Words: %s" % mnemonic_words)
# print("Keys: ")
# for key in keys:
# print(key)
# ------------ End of Crypto Sample ---------------
# -------------- Socket Sample ------------------
from binance_dex.sockets import BinanceChainSocket
# Sample of Customized Callback function to handle received data
def customized_msg_handler(ws, received_message):
''' Simply print out '''
print('----- Customized handler -----')
print(str(received_message))
# Create Socket Instance
socket_instance = BinanceChainSocket(IS_TEST_NET)
# # -- Sample of Short-lived (one-off / send-receive) Connection ----
# print(socket_instance.fetch_block_height_updates())
#
# # -- Sample of Long Lived Connection WITHOUT customized Callback ----
# # If callback function not provided, will simply print out
# socket_instance.fetch_block_height_updates(one_off=False)
#
# # If callback function provided, can customized handle received data
# socket_instance.fetch_block_height_updates(one_off=False, callback_function=customized_msg_handler)
#
#
# # !!! Note: below samples only provide "long-lived" calls. But actually both are supported !!!
# # <--- Here we go --->
# Fetch Account Updates, Including: Orders, Transfer, Balance
# socket_instance.fetch_account_updates(user_address='tbnb1fn9z9vn4f44ekz0a3pf80dcy2wh4d5988phjds',
# one_off=False,
# callback_function=customized_msg_handler)
# Fetch Market Trades Information by Trading Pair
# socket_instance.fetch_trades_updates(trading_pairs='100K-9BC_BNB',
# one_off=False,
# callback_function=customized_msg_handler)
# Fetch Market Trading Depth Stream by Trading Pair
# socket_instance.fetch_market_diff_stream(trading_pairs='100K-9BC_BNB',
# one_off=False,
# callback_function=customized_msg_handler)
# Fetch Market Top 20 Levels of Bids and Asks
# socket_instance.fetch_market_depth_stream(trading_pairs='100K-9BC_BNB',
# one_off=False,
# callback_function=customized_msg_handler)
# Fetch fetch_kline_updates
# from binance_dex.api import api_types_instance
# kline_intervals = api_types_instance.KLine()
# socket_instance.fetch_kline_updates(trading_pair='100K-9BC_BNB',
# interval=kline_intervals.interval_1min,
# callback_function=customized_msg_handler)
# # 24hr Ticker statistics for a single symbol are pushed every second
# socket_instance.fetch_ticker_streams(trading_pair='100K-9BC_BNB',
# is_full_data=True,
# one_off=False,
# callback_function=customized_msg_handler)
# # 24hr Ticker statistics for ALL symbols are pushed every second
# socket_instance.fetch_ticker_streams(one_off=False,
# callback_function=customized_msg_handler)
# -------------- Node RPC Sample ------------------
from binance_dex.node_rpc import BinanceChainNodeRPC
# Create Instance
# # OPTION 1: using existing RPC node
node_rpc_instance = BinanceChainNodeRPC(is_test_net=False,
node_rpc_url=None)
# #OPTION 2: using your own node
# node_rpc_instance = BinanceChainNodeRPC(node_rpc_url='https://seed-pre-s3.binance.org')
# # Get available RPC endpoints (HTML format)
# print(node_rpc_instance.get_list())
#
# # Get info about the application
# print(node_rpc_instance.abci_info())
#
# # Get block at a given height. If no height is provided, it will fetch the latest block
# print(node_rpc_instance.block(height=6228826))
#
# # Get block headers for minHeight <= height <= maxHeight
# print(node_rpc_instance.blockchain(8, 10))
#
# # gets ABCIResults at a given height
# print(node_rpc_instance.block_results(height=100))
#
# # Broadcast Transaction (Async),
# print(node_rpc_instance.broadcast_tx_async(tx_id='123'))
#
# # Broadcast Transaction Sync,
# print(node_rpc_instance.broadcast_tx_sync(tx_id='123'))
#
# # Broadcast Transaction Commit
# print(node_rpc_instance.broadcast_tx_commit(tx_id='123'))
#
# # Get block commit at a given height.
# print(node_rpc_instance.get_commit(height=10))
#
# # Get the consensus parameters at the given block height
# print(node_rpc_instance.consensus_params(height=100))
#
# # ConsensusState returns a concise summary of the consensus state.
# print(node_rpc_instance.consensus_state())
#
# # Get genesis file
# print(node_rpc_instance.genesis())
#
# # Get network info
# print(node_rpc_instance.net_info())
#
# Get number of unconfirmed transactions
# print(node_rpc_instance.num_unconfirmed_txs())
#
# # Get Tendermint status including node info, pubkey, latest block hash, app hash, block height and time
# print(node_rpc_instance.status())
#
# # Query the transaction results by Hash
# print(node_rpc_instance.transaction(hash='C3FF309D7226768FC48B5E2D2D91719D77BAFA66DF7D3C53FCB212075DA83EA3'))
#
# # Get unconfirmed transactions (maximum ?limit entries) including their number
# print(node_rpc_instance.unconfirmed_txs())
#
# # Get the validator set at the given block height. If no height is provided, it will fetch the current validator set
# print(node_rpc_instance.validators(height=10))
# ----------- End of Node RPC Sample --------------