Skip to main content

Python Library for Binance DEX, including API, Websocket, JSONRPC and Crypto

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

binance_dex-0.1.3.tar.gz (41.3 kB view details)

Uploaded Source

Built Distribution

binance_dex-0.1.3-py3-none-any.whl (42.1 kB view details)

Uploaded Python 3

File details

Details for the file binance_dex-0.1.3.tar.gz.

File metadata

  • Download URL: binance_dex-0.1.3.tar.gz
  • Upload date:
  • Size: 41.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.15

File hashes

Hashes for binance_dex-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4d1f869b0ab7d97a73064bed39f86ad998b7dda1b2a4e52babdf707399079bab
MD5 3151146b624cb39bccc53a1c1fdc260f
BLAKE2b-256 822ee7c8ced6062c6621b7e70f2f24ca6fd987829314156d1b25d53f273a75b9

See more details on using hashes here.

File details

Details for the file binance_dex-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: binance_dex-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 42.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.15

File hashes

Hashes for binance_dex-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1ee89ca0a75d9f2c1183b59ffecaf1ecfeaecc98e5af49b888f39451a07824d8
MD5 152e5c9be60125e0da59e4157756c6ee
BLAKE2b-256 be1ec3561e779c66347c18d7bb027a0d06b527fc0be1215aec026e6c75a5e92b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page