Skip to main content

Custom Binance client and utilities used by XChainPY clients

Project description

xchainpy/xchainpy_binance

Binance Module for XChainPy Clients

Modules

  • client - Custom client for communicating with binance_chain
  • models - model wrapper for binance_chain types
  • util - Utitilies for using binance_chain

Following dependencies have to be installed into your project

secp256k1Crypto - py-binance-chain - pywallet - mnemonic

pip install xchainpy_binance

Binance Client module

Initialize a client

from xchainpy_client.models.types import Network, XChainClientParams
from xchainpy_binance.client import Client

# Note: This phrase is created by https://iancoleman.io/bip39/ and will never been used in a real-world
phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
client = Client(XChainClientParams(network=Network.Mainnet, phrase=phrase))

# if you want to change phrase after initialize the client
client.set_phrase('wheel leg dune emerge sudden badge rough shine convince poet doll kiwi sleep labor hello')

# if you want to change network after initialize the client
await client.purge_client()
client.set_network(Network.Mainnet)

# get python-binance-chain client
client.get_bnc_client()

# when you are done with the client, call this
await client.purge_client()

Address methods

from xchainpy_client.models.types import Network, XChainClientParams
from xchainpy_binance.client import Client

phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
client = Client(XChainClientParams(network=Network.Mainnet, phrase=phrase))

address = client.get_address()

is_valid = client.validate_address(address) # bool

print(address)
print(is_valid)

Fees

from xchainpy_client.models.types import Network, XChainClientParams
from xchainpy_binance.client import Client

phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
client = Client(XChainClientParams(network=Network.Mainnet, phrase=phrase))

fees = await client.get_fees()

multi_send_fees = await client.get_multi_send_fees()

single_and_multi_fees = await client.get_single_and_multi_fees()

print(f'''fees: 
average: {fees.average}
fast: {fees.fast}
fastest: {fees.fastest}\n''')

print(f'''multi_send_fees: 
average: {multi_send_fees.average}
fast: {multi_send_fees.fast}
fastest: {multi_send_fees.fastest}\n''')

print(f'''single_and_multi_fees: 
single:
    average: {single_and_multi_fees['single'].average}
    fast: {single_and_multi_fees['single'].fast}
    fastest: {single_and_multi_fees['single'].fastest}
multi:
    average: {single_and_multi_fees['single'].average}
    fast: {single_and_multi_fees['single'].fast}
    fastest: {single_and_multi_fees['single'].fastest}''')

Balance

from xchainpy_client.models.types import Network, XChainClientParams
from xchainpy_binance.client import Client

phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
client = Client(XChainClientParams(network=Network.Testnet, phrase=phrase))

address = client.get_address()

balances = await client.get_balance(address=address)

for balance in balances:
    print(f'asset: {balance.asset}, amount: {balance.amount}')

Transactions and Transaction_data

from xchainpy_client.models.types import Network, XChainClientParams
from xchainpy_binance.client import Client
from xchainpy_client.models.tx_types import TxHistoryParams


phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
client = Client(XChainClientParams(network=Network.Testnet, phrase=phrase))

address = client.get_address()

params = TxHistoryParams(address=address, limit=1)
transactions = await client.get_transactions(params)
# type of transactions is xchainpy_client.models.tx_types.TxPage

t = transactions.txs[0]
print(t.asset)
print(t.tx_from[0].amount)
print(t.tx_from[0].address)
print(t.tx_to[0].amount)
print(t.tx_to[0].address)
print(t.tx_date)
print(t.tx_type)
print(t.tx_hash)

transaction = await client.get_transaction_data(t.tx_hash)
# transaction object is equal by t object

Transfer

from xchainpy_client.models.types import Network, XChainClientParams
from xchainpy_binance.client import Client
from xchainpy_client.models.tx_types import TxParams


phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
client = Client(XChainClientParams(network=Network.Testnet, phrase=phrase))

address = client.get_address()

params = TxParams(asset=Asset('BNB','BNB'), amount=0.0001, recipient=address, memo='memo')
tx_hash = await client.transfer(params)

print(tx_hash)

Explorer url

from xchainpy_client.models.types import Network, XChainClientParams
from xchainpy_binance.client import Client
from xchainpy_client.models.tx_types import TxParams


phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
client = Client(XChainClientParams(network=Network.Testnet, phrase=phrase))

print(client.get_explorer_url())
print(client.get_explorer_address_url('testAddressHere'))
print(client.get_explorer_tx_url('testTxHere'))

await client.purge_client()
client.set_network(Network.Mainnet)

print(client.get_explorer_url())
print(client.get_explorer_address_url('testAddressHere'))
print(client.get_explorer_tx_url('testTxHere'))

Crypto module

    from py_binance_chain.environment import BinanceEnvironment
    from xchainpy_binance import crypto

    # Note: This phrase is created by https://iancoleman.io/bip39/ and will never been used in a real-world
    phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
    env = BinanceEnvironment.get_testnet_env()

    seed = crypto.mnemonic_to_seed(mnemonic=phrase)
    print(seed)

    private_key = crypto.mnemonic_to_private_key(mnemonic=phrase, index=0, env=env)
    print(private_key)

    public_key = crypto.private_key_to_public_key(private_key=private_key)
    print(public_key)

    address = crypto.private_key_to_address(private_key=private_key, prefix='tbnb')
    print(address)

    address = crypto.public_key_to_address(public_key=public_key, prefix='tbnb')
    print(address)

    is_valid = crypto.check_address(address=address, prefix='tbnb')
    print(is_valid)

Tests

These packages needed to run tests:

  • pytest pip install pytest
  • pytest-asyncio pip install pytest-asyncio

How to run test ?

$ python -m pytest xchainpy/xchainpy_binance/tests

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

xchainpy_binance-0.2.tar.gz (13.9 kB view details)

Uploaded Source

Built Distributions

xchainpy_binance-0.2.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

xchainpy_binance-0.2-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file xchainpy_binance-0.2.tar.gz.

File metadata

  • Download URL: xchainpy_binance-0.2.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.10

File hashes

Hashes for xchainpy_binance-0.2.tar.gz
Algorithm Hash digest
SHA256 cd47d3f5fe1aceeed82c6751f86d4e05cc0a903d2bbaed96ba5641f6943d17a8
MD5 e328b2776fe444c08ef3c27ed865c60b
BLAKE2b-256 d4e07eadfdba455b1f0d855af20a4ac87c92a17bf030ff9953edb03ce3a60342

See more details on using hashes here.

File details

Details for the file xchainpy_binance-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: xchainpy_binance-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.10

File hashes

Hashes for xchainpy_binance-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e00870d0e8637d4420ffddc8d68b7b29ef4a7a91f7bfd4372367a496355e3b4f
MD5 237f59f0386f6e948c4d78b4005aa1b5
BLAKE2b-256 c9c41dfbfa5f93d9d2e7cef2509028910ca060a6d4149e964ed802a74d180438

See more details on using hashes here.

File details

Details for the file xchainpy_binance-0.2-py3-none-any.whl.

File metadata

  • Download URL: xchainpy_binance-0.2-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.10

File hashes

Hashes for xchainpy_binance-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 52f18ce48d8b1a66f012efa17e5f66a6cff35abd54a2e20554a11fe5ec2f1a49
MD5 30067aef5d308985626feffdadd8a395
BLAKE2b-256 82cdbddc63aa3af3223be2d26d636f76ec80cd041909ff4bd31de99173b8627b

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