Skip to content

wally-yu/binance-dex

Repository files navigation

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.

Dex Links:

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:

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

from binance_dex.api import BinanceChainClient

# create API Client instance
api_client = BinanceChainClient(is_test_net=True)

# call corresponding methods
print(api_client.get_block_time())

Sample return:

{'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

from binance_dex.crypto import BinanceChainCrypto

# Create crypto instance
crypto_instance = BinanceChainCrypto(is_test_net=True)

# 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)

Sample return:

Generating Mnemonic Words: 
early solid bronze civil version orange prize curve glory cricket ticket already weekend home early buyer zebra olive melody enrich park jeans apart tower

Generating Private Key / Public Key / Mnemonic words: 
{'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

from binance_dex.sockets import BinanceChainSocket

# --- Notice: Need to provide customized Call Back function to handle socket return data ---

# Sample of Customized Callback function to handle received data
def customized_call_back(ws, received_message):
    ''' Simply print out '''
    print('----- Customized handler -----')
    print(str(received_message))


# Create Socket Instance
socket_instance = BinanceChainSocket(IS_TEST_NET)

# 24hr Ticker statistics for a single symbol, push every second
socket_instance.fetch_ticker_streams(trading_pair='100K-9BC_BNB',
                                     is_full_data=True,
                                     one_off=False, # long lived connection
                                     callback_function=customized_call_back)

Sample return:

----- Customized handler -----
{"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}}

----- Customized handler -----
{"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}}

----- Customized handler -----
{"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

from binance_dex.node_rpc import BinanceChainNodeRPC

# Create Instance

# OPTION 1: using existing RPC node
node_rpc_instance = BinanceChainNodeRPC(is_test_net=True,
                                        node_rpc_url=None)
                                        
# OPTION 2: using your own node
# node_rpc_instance = BinanceChainNodeRPC(node_rpc_url='https://seed-pre-s3.binance.org')

# Get number of unconfirmed transactions
print(node_rpc_instance.num_unconfirmed_txs())                                        

Sample return

Using Existing RPC server, trying to find a healthy node server...

Request URL: https://seed-pre-s3.binance.org:443/health ... ...

Successfully found healthy node RPC server: https://seed-pre-s3.binance.org:443

Request URL: https://seed-pre-s3.binance.org:443/num_unconfirmed_txs ... ...
{'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.

Releases

No releases published

Packages

No packages published

Languages