sammchardy / python-binance-chain

Binance Chain Exchange API python implementation for automated trading

Home Page:http://python-binance-chain.rtfd.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Welcome to python-binance-chain v0.1.20

image

image

image

image

image

image

This is an unofficial Python3 wrapper for the Binance Chain API. I am in no way affiliated with Binance, use at your own risk.

PyPi

https://pypi.python.org/pypi/python-binance-chain

Source code

https://github.com/sammchardy/python-binance-chain

Features

Read the Changelog

Quick Start

If having issues with secp256k1 check the Installation instructions for the sec256k1-py library

If using the production server there is no need to pass the environment variable.

from binance_chain.http import HttpApiClient
from binance_chain.constants import KlineInterval
from binance_chain.environment import BinanceEnvironment

# initialise with Testnet environment
testnet_env = BinanceEnvironment.get_testnet_env()
client = HttpApiClient(env=testnet_env)

# Alternatively pass no env to get production
prod_client = HttpApiClient()

# connect client to different URL using custom environments, see below

# get node time
time = client.get_time()

# get node info
node_info = client.get_node_info()

# get validators
validators = client.get_validators()

# get peers
peers = client.get_peers()

# get account
account = client.get_account('tbnb185tqzq3j6y7yep85lncaz9qeectjxqe5054cgn')

# get account sequence
account_seq = client.get_account_sequence('tbnb185tqzq3j6y7yep85lncaz9qeectjxqe5054cgn')

# get markets
markets = client.get_markets()

# get fees
fees = client.get_fees()

# get order book
order_book = client.get_order_book('NNB-0AD_BNB')

# get klines
klines = client.get_klines('NNB-338_BNB', KlineInterval.ONE_DAY)

# get closed orders
closed_orders = client.get_closed_orders('tbnb185tqzq3j6y7yep85lncaz9qeectjxqe5054cgn')

# get open orders
open_orders = client.get_open_orders('tbnb185tqzq3j6y7yep85lncaz9qeectjxqe5054cgn')

# get ticker
ticker = client.get_ticker('NNB-0AD_BNB')

# get trades
trades = client.get_trades(limit=2)

# get order
order = client.get_order('9D0537108883C68B8F43811B780327CE97D8E01D-2')

# get trades
trades = client.get_trades()

# get transactions
transactions = client.get_transactions(address='tbnb1n5znwyygs0rghr6rsydhsqe8e6ta3cqatucsqp')

# get transaction
transaction = client.get_transaction('95DD6921370D74D0459590268B439F3DD49F6B1D090121AFE4B2183C040236F3')

See API docs for more information.

Async HTTP Client

An implementation of the HTTP Client above using aiohttp instead of requests

Use the async create classmethod to initialise an instance of the class.

All methods are otherwise the same as the HttpApiClient

Environments

Binance Chain offers a Production system and Testnet.

If using the Production system there is no need to pass an environment as this is the default.

To create and use the Testnet environment is as easy as

You may also create your own custom environments, this may be useful such as connecting to a Node RPC client

See API docs for more information.

Wallet

See API docs for more information.

The wallet is required if you want to place orders, transfer funds or freeze and unfreeze tokens.

You may also use the Ledger Wallet class to utilise your Ledger Hardware Wallet for signing.

It can be initialised with your private key or your mnemonic phrase.

You can additionally provide BIP39 passphrase and derived wallet id.

Note that the BinanceEnvironment used for the wallet must match that of the HttpApiClient, testnet addresses will not work on the production system.

The Wallet class can also create a new account for you by calling the Wallet.create_random_wallet() function, see examples below

Initialise from Private Key

Initialise from Mnemonic

Initialise by generating a random Mneomonic

Broadcast Messages on HttpApiClient

See API docs for more information.

Requires a Wallet to have been created.

The Wallet will increment the request sequence when broadcasting messages through the HttpApiClient.

If the sequence gets out of sync call wallet.reload_account_sequence(client), where client is an instance of HttpApiClient.

Place Order

General case

Limit Order Buy

Limit Order Sell

Cancel Order

Freeze Tokens

Unfreeze Tokens

Transfer Tokens

Transfer Multiple Tokens

Vote for proposal

Sign Transaction

If you want to simply sign a transaction you can do that as well.

This is a transfer example

Websockets

See API docs for more information.

Unsubscribe

Close Connection

Node RPC HTTP

See API docs for more information.

The binance_chain.http.HttpApiClient has a helper function get_node_peers() which returns a list of peers with Node RPC functionality

Node RPC HTTP Async

An aiohttp implementation of the Node RPC HTTP API.

Use the async create classmethod to initialise an instance of the class.

All methods are the same as the binance_chain.node_rpc.http.HttpRpcClient.

Broadcast Messages on Node RPC HTTP Client

Requires a Wallet to have been created

The Wallet will increment the request sequence when broadcasting messages through the HttpApiClient.

If the sequence gets out of sync call wallet.reload_account_sequence(client), where client is an instance of HttpApiClient.

Place Order

Other messages can be constructed similar to examples above

Pooled Node RPC Client

This client connects to all available peer nodes in the network and spreads requests across them.

This helps reduce API rate limit errors.

The interface is the same as the above HttpRpcClient and AsyncHttpRpcClient classes for consistency.

Requests can be sent using asyncio gather note to check the number of clients connected to and not exceed that amount

To keep the peer connections up to date you may re-initialise the list of peers by calling the initialise_peers function

Node RPC Websockets

See API docs for more information.

For subscribe query examples see the documentation here

Unsubscribe

Unsubscribe All

Depth Cache

Follow the order book for a specified trading pair.

Note: This may not be 100% reliable as the response info available from Binance Chain may not always match up

Signing Service

A Service to sign and optionally also broadcast messages for you.

The service holds the private keys of the accounts and supplies a username and password to interact with these accounts.

This client re-uses the binance_chain.messages types. In this case no wallet parameter is required.

This client interacts with the binance-chain-signing-service read the docs there to create our own signing service.

Signing and then broadcasting

the sign_order method can also take a binance_chain.messages.LimitOrderBuyMsg or binance_chain.messages.LimitOrderSellMsg instance.

This hex can then be broadcast using the normal HTTP Client like so

The signing service supports binance_chain.messages types NewOrderMsg, CancelOrderMsg, FreezeMsg, UnFreezeMsg and TransferMsg

Signing and broadcasting in one

To sign and broadcast an order use the broadcast_order method. This returns the response from the Binance Chain exchange.

Async Signing Service

Like all other libraries there is an async version.

Ledger

Sign transactions with your Ledger wallet, supports Ledger Blue, Nano S and Nano X.

Make sure you have registered on Binance Chain with your Ledger address.

Make sure that you have connected your Ledger and are in the Binance Chain app.

Install python-binance-chain with this optional library like so pip install python-binance-chain[ledger]

Uses the btchip-python library if having issues installing check their github page

Create a Wallet to use with the HTTP and Node RPC clients

Requests and AioHTTP Settings

python-binance-chain uses requests and aiohttp libraries.

You can set custom requests parameters for all API calls when creating any of the http clients.

Check out either the requests documentation or aiohttp documentation for all options.

Proxy Settings

You can use the settings method above

Or set an environment variable for your proxy if required to work across all requests.

An example for Linux environments from the requests Proxies documentation is as follows.

For Windows environments

Running Tests

Donate

If this library helped you out feel free to donate.

  • ETH: 0xD7a7fDdCfA687073d7cC93E9E51829a727f9fE70
  • NEO: AVJB4ZgN7VgSUtArCt94y7ZYT6d5NDfpBo
  • LTC: LPC5vw9ajR1YndE1hYVeo3kJ9LdHjcRCUZ
  • BTC: 1Dknp6L6oRZrHDECRedihPzx2sSfmvEBys

Thanks

Sipa <https://github.com/sipa/bech32> for python reference implementation for Bech32 and segwit addresses

Other Exchanges

If you use Binance check out my python-binance library.

If you use Kucoin check out my python-kucoin library.

If you use IDEX check out my python-idex library.

About

Binance Chain Exchange API python implementation for automated trading

http://python-binance-chain.rtfd.io

License:MIT License


Languages

Language:Python 100.0%