Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binancep #58

Open
wants to merge 23 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
=======================================
Welcome to python-binance-chain v0.1.19
Welcome to python-binance-chain v0.1.20
=======================================

.. image:: https://img.shields.io/pypi/v/python-binance-chain.svg
Expand Down Expand Up @@ -89,8 +89,7 @@ If using the production server there is no need to pass the environment variable
# Alternatively pass no env to get production
prod_client = HttpApiClient()

# connect client to different URL
client = HttpApiClient(api_url='https://yournet.com')
# connect client to different URL using custom environments, see below

# get node time
time = client.get_time()
Expand Down Expand Up @@ -128,16 +127,16 @@ If using the production server there is no need to pass the environment variable
# get open orders
open_orders = client.get_open_orders('tbnb185tqzq3j6y7yep85lncaz9qeectjxqe5054cgn')

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

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

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

# get open orders
# get trades
trades = client.get_trades()

# get transactions
Expand All @@ -162,6 +161,7 @@ All methods are otherwise the same as the HttpApiClient

from binance_chain.http import AsyncHttpApiClient
from binance_chain.environment import BinanceEnvironment
import asyncio

loop = None

Expand Down Expand Up @@ -226,6 +226,8 @@ You may also use the `Ledger Wallet class <#ledger>`_ to utilise your Ledger Har

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.

Expand Down Expand Up @@ -254,7 +256,10 @@ see examples below
from binance_chain.environment import BinanceEnvironment

testnet_env = BinanceEnvironment.get_testnet_env()
wallet = Wallet.create_wallet_from_mnemonic('mnemonic word string', env=testnet_env)
wallet = Wallet.create_wallet_from_mnemonic('mnemonic word string',
passphrase='optional passphrase',
child=0,
env=testnet_env)
print(wallet.address)
print(wallet.private_key)
print(wallet.public_key_hex)
Expand Down Expand Up @@ -292,6 +297,8 @@ General case
from binance_chain.http import HttpApiClient
from binance_chain.messages import NewOrderMsg
from binance_chain.wallet import Wallet
from binance_chain.constants import TimeInForce, OrderSide, OrderType
from decimal import Decimal

wallet = Wallet('private_key_string')
client = HttpApiClient()
Expand All @@ -300,7 +307,7 @@ General case
new_order_msg = NewOrderMsg(
wallet=wallet,
symbol="ANN-457_BNB",
time_in_force=TimeInForce.GTE,
time_in_force=TimeInForce.GOOD_TILL_EXPIRE,
order_type=OrderType.LIMIT,
side=OrderSide.BUY,
price=Decimal(0.000396000),
Expand Down Expand Up @@ -363,6 +370,7 @@ General case
from binance_chain.http import HttpApiClient
from binance_chain.messages import FreezeMsg
from binance_chain.wallet import Wallet
from decimal import Decimal

wallet = Wallet('private_key_string')
client = HttpApiClient()
Expand All @@ -384,6 +392,7 @@ General case
from binance_chain.http import HttpApiClient
from binance_chain.messages import UnFreezeMsg
from binance_chain.wallet import Wallet
from decimal import Decimal

wallet = Wallet('private_key_string')
client = HttpApiClient()
Expand Down Expand Up @@ -510,7 +519,7 @@ See `API <https://python-binance-chain.readthedocs.io/en/latest/binance-chain.ht
print(msg)

# connect to testnet env
bcsm = await BinanceChainSocketManager.create(loop, handle_evt, address2, env=testnet_env)
bcsm = await BinanceChainSocketManager.create(loop, handle_evt, address, env=testnet_env)

# subscribe to relevant endpoints
await bcsm.subscribe_orders(address)
Expand Down Expand Up @@ -599,6 +608,7 @@ All methods are the same as the binance_chain.node_rpc.http.HttpRpcClient.
from binance_chain.node_rpc.http import AsyncHttpRpcClient
from binance_chain.http import AsyncHttpApiClient, PeerType
from binance_chain.environment import BinanceEnvironment
import asyncio

loop = None

Expand Down Expand Up @@ -858,6 +868,7 @@ to create our own signing service.

from binance_chain.messages import NewOrderMsg
from binance_chain.signing.http import HttpApiSigningClient
from binance_chain.constants import TimeInForce, OrderSide, OrderType

signing_client = HttpApiSigningClient('http://localhost:8000', username='sam', password='mypass')

Expand Down Expand Up @@ -902,6 +913,7 @@ To sign and broadcast an order use the `broadcast_order` method. This returns th

from binance_chain.messages import NewOrderMsg
from binance_chain.signing.http import HttpApiSigningClient
from binance_chain.constants import TimeInForce, OrderSide, OrderType

signing_client = HttpApiSigningClient('http://localhost:8000', username='sam', password='mypass')

Expand All @@ -928,6 +940,8 @@ Like all other libraries there is an async version.
from binance_chain.signing.http import AsyncHttpApiSigningClient
from binance_chain.http import AsyncHttpApiClient, PeerType
from binance_chain.environment import BinanceEnvironment
from binance_chain.constants import TimeInForce, OrderSide, OrderType
import asyncio

loop = None

Expand Down Expand Up @@ -1104,12 +1118,6 @@ Other Exchanges

If you use `Binance <https://www.binance.com/?ref=10099792>`_ check out my `python-binance <https://github.com/sammchardy/python-binance>`_ library.

If you use `Kucoin <https://www.kucoin.com/ucenter/signup?rcode=E42cWB>`_ check out my `python-kucoin <https://github.com/sammchardy/python-kucoin>`_ library.

If you use `Allcoin <https://www.allcoin.com/Account/RegisterByPhoneNumber/?InviteCode=MTQ2OTk4MDgwMDEzNDczMQ==>`_ check out my `python-allucoin <https://github.com/sammchardy/python-allcoin>`_ library.
If you use `Kucoin <https://www.kucoin.com/?rcode=E42cWB>`_ check out my `python-kucoin <https://github.com/sammchardy/python-kucoin>`_ library.

If you use `IDEX <https://idex.market>`_ check out my `python-idex <https://github.com/sammchardy/python-idex>`_ library.

If you use `BigONE <https://big.one>`_ check out my `python-bigone <https://github.com/sammchardy/python-bigone>`_ library.

.. image:: https://analytics-pixel.appspot.com/UA-111417213-1/github/python-kucoin?pixel
2 changes: 1 addition & 1 deletion binance_chain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

"""

__version__ = '0.1.19'
__version__ = '0.1.20'
2 changes: 1 addition & 1 deletion binance_chain/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def get_markets(self):
:return: API Response

"""
return self._get("markets")
return self._get("markets?limit=1000")

def get_fees(self):
"""Gets the current trading fees settings
Expand Down
2 changes: 1 addition & 1 deletion binance_chain/utils/encode_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def encode_number(num: Union[float, Decimal]) -> int:
if type(num) == Decimal:
return int(num * (Decimal(10) ** 8))
else:
return int(num * math.pow(10, 8))
return int(round(num * 1e8))


def varint_encode(num):
Expand Down
56 changes: 43 additions & 13 deletions binance_chain/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from secp256k1 import PrivateKey
from mnemonic import Mnemonic
from pywallet.utils.bip32 import Wallet as Bip32Wallet
from pycoin.symbols.btc import network

from binance_chain.utils.segwit_addr import address_from_public_key, decode_address
from binance_chain.environment import BinanceEnvironment
Expand All @@ -23,7 +23,7 @@ class MnemonicLanguage(str, Enum):

class BaseWallet:

HD_PATH = "44'/714'/0'/0/0"
HD_PATH = "44'/714'/0'/0/{id}"

def __init__(self, env: Optional[BinanceEnvironment] = None):
self._env = env or BinanceEnvironment.get_production_env()
Expand Down Expand Up @@ -103,8 +103,21 @@ def sign_message(self, msg_bytes):


class Wallet(BaseWallet):
"""
Usage example:

HD_PATH = "44'/714'/0'/0/0"
m = Wallet.create_random_mnemonic() # 12 words
p = 'my secret passphrase' # bip39 passphrase

# Store <m> and <p> somewhere safe

wallet1 = Wallet.create_wallet_from_mnemonic(m, passphrase=p, child=0, env=testnet_env)
wallet2 = Wallet.create_wallet_from_mnemonic(m, passphrase=p, child=1, env=testnet_env)
...

"""

HD_PATH = "44'/714'/0'/0/{id}"

def __init__(self, private_key, env: Optional[BinanceEnvironment] = None):
super().__init__(env)
Expand All @@ -118,22 +131,39 @@ def create_random_wallet(cls, language: MnemonicLanguage = MnemonicLanguage.ENGL
env: Optional[BinanceEnvironment] = None):
"""Create wallet with random mnemonic code

:return:
:return: initialised Wallet
"""
m = Mnemonic(language.value)
phrase = m.generate()
phrase = cls.create_random_mnemonic(language)
return cls.create_wallet_from_mnemonic(phrase, env=env)

@classmethod
def create_wallet_from_mnemonic(cls, mnemonic: str, env: Optional[BinanceEnvironment] = None):
"""Create wallet with random mnemonic code
def create_wallet_from_mnemonic(cls, mnemonic: str,
passphrase: Optional[str] = '',
child: Optional[int] = 0,
env: Optional[BinanceEnvironment] = None):
"""Create wallet from mnemonic, passphrase and child wallet id

:return:
:return: initialised Wallet
"""
seed = Mnemonic.to_seed(mnemonic)
new_wallet = Bip32Wallet.from_master_secret(seed=seed, network='BTC')
child = new_wallet.get_child_for_path(Wallet.HD_PATH)
return cls(child.get_private_key_hex().decode(), env=env)
if type(child) != int:
raise TypeError("Child wallet id should be of type int")

seed = Mnemonic.to_seed(mnemonic, passphrase)
new_wallet = network.keys.bip32_seed(seed)
child_wallet = new_wallet.subkey_for_path(Wallet.HD_PATH)
# convert secret exponent (private key) int to hex
key_hex = format(child_wallet.secret_exponent(), 'x')
return cls(key_hex, env=env)

@classmethod
def create_random_mnemonic(cls, language: MnemonicLanguage = MnemonicLanguage.ENGLISH):
"""Create random mnemonic code

:return: str, mnemonic phrase
"""
m = Mnemonic(language.value)
phrase = m.generate()
return phrase

@property
def private_key(self):
Expand Down
11 changes: 11 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
Changelog
=========

v0.1.20 - 2019-06-29
^^^^^^^^^^^^^^^^^^^^

**Added**

- Multi Transfer Msg option

**Fixed**

- Response code of 0 for http requests

v0.1.19 - 2019-04-30
^^^^^^^^^^^^^^^^^^^^

Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pywallet>=0.1.0
pycoin>=0.90.20201031
requests>=2.21.0
aiohttp>=3.5.4
websockets>=7.0
secp256k1>=0.13.2
mnemonic>=0.18
protobuf>=3.6.1
protobuf>=3.6.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def find_version(*file_paths):
def install_requires():

requires = [
'pywallet>=0.1.0', 'requests>=2.21.0', 'websockets>=7.0', 'aiohttp>=3.5.4',
'pycoin>=0.90.20201031', 'requests>=2.21.0', 'websockets>=7.0', 'aiohttp>=3.5.4',
'secp256k1>=0.13.2', 'protobuf>=3.6.1', 'mnemonic>=0.18', 'ujson>=1.35'
]
return requires
Expand Down