项目作者: wally-yu

项目描述 :
Python Library for Binance Dex and Binance Chain
高级语言: Python
项目地址: git://github.com/wally-yu/binance-dex.git
创建时间: 2019-03-06T02:55:01Z
项目社区:https://github.com/wally-yu/binance-dex

开源协议:MIT License

下载


Binance DEX / Binance Chain Python Package

Introudction:

This Python package develop based on Binance Chain official doc and tested all functionalities on test-net as well as main-net.

According to official doc, ways to connect to Binance DEX are:

  • REST API
  • CLI
  • Web Socket
  • Node RPC

This python package will provide all methods available as from binance official document except cli:

  • REST API
  • CLI
  • WebSocket
  • Node NRPC

Environment:

Due to time limitation, we didn’t test different python version + os environment combinations, below are what we tested so far:

  • Python3.5 + MacOs
  • Python3.5 + Windows10

we are not sure if there would be any compatibility issues for python2.7, let us know if you encounter any issue.

BTW, we do suggest to use Virtual Environment.

Python SDK Sample Usage:

Install package:

  1. pip install binance-dex
  • Notes: If you are working on Windows platform, compiling tools Microsoft Visual C++ 14.0 is required.

Full Code Example

Find full Sample Usage from code_examples.py


Code Examples Sector by Sector

- API Sample Usage

  1. from binance_dex.api import BinanceChainClient
  2. # create API Client instance
  3. api_client = BinanceChainClient(is_test_net=True)
  4. # call corresponding methods
  5. print(api_client.get_block_time())

Sample return:

  1. {'status': True, 'result': {'ap_time': '2019-04-06T04:43:48Z', 'block_time': '2019-04-06T04:43:47Z'}}

++Find more API information by clicking this link++


- Crypto Sample Usage

  1. from binance_dex.crypto import BinanceChainCrypto
  2. # Create crypto instance
  3. crypto_instance = BinanceChainCrypto(is_test_net=True)
  4. # Generate Mnemonic words
  5. mnemonic_words = crypto_instance.generate_mnemonic()
  6. print("Generating Mnemonic Words: ")
  7. print(mnemonic_words)
  8. # Generate Private Key, Public Address and mnemonic
  9. key = crypto_instance.generate_key()
  10. print('Generating Private Key / Public Key / Mnemonic words: ')
  11. print(key)

Sample return:

  1. Generating Mnemonic Words:
  2. early solid bronze civil version orange prize curve glory cricket ticket already weekend home early buyer zebra olive melody enrich park jeans apart tower
  3. Generating Private Key / Public Key / Mnemonic words:
  4. {'private_key': '65dba225a6965020ff7aae6efc8b9494cbf52bea36e44341d471a7b4b8207e1a', 'public_address': 'tbnb1uvjsrw2pstxqwk45n8k6ke53yw8fsegjery2en', 'mnemonic': 'allow adult frown ivory coffee inhale calm assist galaxy indoor credit oyster tower exclude popular veteran first hint flag boost right zone clown flower'}

++Find more Crypto information by clicking this link++


- Socket Sample Usage

  1. from binance_dex.sockets import BinanceChainSocket
  2. # --- Notice: Need to provide customized Call Back function to handle socket return data ---
  3. # Sample of Customized Callback function to handle received data
  4. def customized_call_back(ws, received_message):
  5. ''' Simply print out '''
  6. print('----- Customized handler -----')
  7. print(str(received_message))
  8. # Create Socket Instance
  9. socket_instance = BinanceChainSocket(IS_TEST_NET)
  10. # 24hr Ticker statistics for a single symbol, push every second
  11. socket_instance.fetch_ticker_streams(trading_pair='100K-9BC_BNB',
  12. is_full_data=True,
  13. one_off=False, # long lived connection
  14. callback_function=customized_call_back)

Sample return:

  1. ----- Customized handler -----
  2. {"stream":"ticker","data":{"e":"24hrTicker","E":1555687041,"s":"100K-9BC_BNB","p":"0.00000000","P":"0.00000000","w":"49999.00000000","x":"49999.00000000","c":"49999.00000000","Q":"0.00009820","b":"0.00000000","B":"0.00000000","a":"4700.00000000","A":"0.13197840","o":"49999.00000000","h":"49999.00000000","l":"49999.00000000","v":"0.00000000","q":"0.00000000","O":1555600601881,"C":1555687001881,"F":"8274485-0","L":"8274485-0","n":0}}
  3. ----- Customized handler -----
  4. {"stream":"ticker","data":{"e":"24hrTicker","E":1555687042,"s":"100K-9BC_BNB","p":"0.00000000","P":"0.00000000","w":"49999.00000000","x":"49999.00000000","c":"49999.00000000","Q":"0.00009820","b":"0.00000000","B":"0.00000000","a":"4700.00000000","A":"0.13197840","o":"49999.00000000","h":"49999.00000000","l":"49999.00000000","v":"0.00000000","q":"0.00000000","O":1555600601881,"C":1555687001881,"F":"8274485-0","L":"8274485-0","n":0}}
  5. ----- Customized handler -----
  6. {"stream":"ticker","data":{"e":"24hrTicker","E":1555687043,"s":"100K-9BC_BNB","p":"0.00000000","P":"0.00000000","w":"49999.00000000","x":"49999.00000000","c":"49999.00000000","Q":"0.00009820","b":"0.00000000","B":"0.00000000","a":"4700.00000000","A":"0.13197840","o":"49999.00000000","h":"49999.00000000","l":"49999.00000000","v":"0.00000000","q":"0.00000000","O":1555600601881,"C":1555687001881,"F":"8274485-0","L":"8274485-0","n":0}}

++Find more Web Socket Doc by clicking this link++


- Node RPC Sample Usage

  1. from binance_dex.node_rpc import BinanceChainNodeRPC
  2. # Create Instance
  3. # OPTION 1: using existing RPC node
  4. node_rpc_instance = BinanceChainNodeRPC(is_test_net=True,
  5. node_rpc_url=None)
  6. # OPTION 2: using your own node
  7. # node_rpc_instance = BinanceChainNodeRPC(node_rpc_url='https://seed-pre-s3.binance.org')
  8. # Get number of unconfirmed transactions
  9. print(node_rpc_instance.num_unconfirmed_txs())

Sample return

  1. Using Existing RPC server, trying to find a healthy node server...
  2. Request URL: https://seed-pre-s3.binance.org:443/health ... ...
  3. Successfully found healthy node RPC server: https://seed-pre-s3.binance.org:443
  4. Request URL: https://seed-pre-s3.binance.org:443/num_unconfirmed_txs ... ...
  5. {'status': True, 'result': {'jsonrpc': '2.0', 'id': '', 'result': {'n_txs': '0', 'txs': None}}}

++Find more Node RPC Doc by clicking this link++

SDK Overview

As you might noticed from above code sample, this SDK is composed with 4 parts:

  • API
  • WebSockets
  • Node RPC
  • Crypto

Description

  • API: HTTP API provides access to a Binance Chain node deployment and market data services DETAILED API DOC

  • Web Sockets: The DEX exposes several data streams over standard WebSocket connections, which can be consumed by modern web browsers and server-side WebSocket libraries DETAILED SOCKET DOC

  • Node RPC: May be used to interact with a node directly over HTTP or websockets. Using RPC, you may perform low-level operations like executing ABCI queries, viewing network/consensus state or broadcasting a transaction DETAILED NODE DOC

  • Crypto: Crypto related functions (such as key managment) DETAILED CRYPTO DOC

Availability:










































































































































































































API WebSockets Node RPC Crypto
Key generate mnemonic
generate key
generate keys
Chain get block height
get block info
get consensue info
get network info
get unconfirmed tx
get Tendermint status
Node get block time
get node info
get validators
get peers
get end points
get abci info
Market get listing tokens
get trading pairs
get depth
get klines
get updated ticker statistics
Account get account balance
get account sequence
get account orders
Transaction get transaction info
broadcast transaction
Others view chain fees

Network requirement:

API WebSockets JSONRPC Crypto
Requir Network X

Join us:

You are always welcomed to join us! Leave your suggestions / or submit your codes

License:

We promise will stick to MIT license permanently.