Example #1
0
    def __init__(self):

        self.env = BinanceEnvironment(
            api_url='https://dex-asiapacific.binance.org',
            wss_url='wss://dex.binance.org/api/',
            hrp='bnb')
        with open('bnb_real_key.txt', 'r') as f:
            key = f.readline()
        # for testing
        # self.env = BinanceEnvironment.get_testnet_env()
        # with open('bnb_key.txt', 'r') as f:
        #     key = f.readline()
        f.close()
        self.client = HttpApiClient(env=self.env)
        self.wallet = Wallet(key, env=self.env)
        self.address = self.wallet.address
        self.ftx = ccxt.ftx({
            'apiKey':
            '6p37l5AXOzIgFzfeSzkcuPhuaYcw3GcpJrU83ROy',
            'secret':
            '3SphoJJ6Gl_w5pPPkGmQpKbVGN9oPiUgxqs4ob_H'
        })
        #self.assets = ['USD', 'USDT']
        self.assets = ['USD']
        self.BNB_BUSD = 'BNB.BUSD-BD1'
        self.pool = THORChain()
 def __init__(self):
     self.env = BinanceEnvironment.get_testnet_env()
     self.client = HttpApiClient(env=self.env)
     with open('secret/bnb_key.txt', 'r') as f:
         key = f.readline()
     self.wallet = Wallet(key, env=self.env)
     self.address = self.wallet.address
     driver_logger.info(f'bnb address: {self.address}')
Example #3
0
    def test_wallet_sequence_decrement(self, private_key, env):
        wallet = Wallet(private_key=private_key, env=env)

        wallet._sequence = 100

        wallet.decrement_account_sequence()

        assert wallet.sequence == 99
Example #4
0
    def test_sign_message(self, private_key, env):
        wallet = Wallet(private_key=private_key, env=env)

        expected = (
            b'\xd9\x01\x02\xab\x13\xfd^4Ge\x82\xa9\xee\x82\xb5\x8c\xa9\x97}\xf9t\xa9\xc7\nC\xee\xfd\x8bG'
            b'\x95N\xe84\xfc\x17\xc0JE\x9a.\xe2\xbb\xa3\x14\xde$\x07\t\xbbB\xeb\xe2\xfb\x1e\xa1dc\x9d\xba'
            b'\xd2\xfa\xe3\xb6\xc1')
        assert wallet.sign_message(b"testmessage") == expected
Example #5
0
    def test_generate_order_id(self, private_key, env):
        wallet = Wallet(private_key=private_key, env=env)

        wallet.initialise_wallet()

        order_id = wallet.generate_order_id()

        assert order_id == f"7F756B1BE93AA2E2FDC3D7CB713ABC206F877802-{wallet.sequence + 1}"
Example #6
0
    def test_wallet_initialise(self, private_key, env):

        wallet = Wallet(private_key=private_key, env=env)

        wallet.initialise_wallet()

        assert wallet.sequence is not None
        assert wallet.account_number is not None
        assert wallet.chain_id is not None
Example #7
0
    def test_initialise_from_mnemonic(self, private_key, mnemonic, env):

        wallet = Wallet.create_wallet_from_mnemonic(mnemonic, env=env)

        assert wallet.private_key == private_key
        assert wallet.public_key_hex == b'02cce2ee4e37dc8c65d6445c966faf31ebfe578a90695138947ee7cab8ae9a2c08'
        assert wallet.address == 'tbnb10a6kkxlf823w9lwr6l9hzw4uyphcw7qzrud5rr'
Example #8
0
    async def multi_send(self, coins, recipient, memo=''):
        """Broadcast multi-send transaction 

        :param coins: contains assets and amounts
        :type coins: Array of Coin
        :param recipient: destination address
        :type recipient: str
        :param memo: optional memo for transaction
        :type memo: str
        :returns: the transaction hash
        :raises: raises if coins was not a list or destination address not provided
        """
        if not isinstance(coins, list):
            raise Exception('coins should be a list of Coin objects')

        wallet = Wallet(self.get_private_key(), env=self.env)

        transfers = [
            Transfer(symbol=coin.asset.symbol, amount=coin.amount)
            for coin in coins
        ]

        try:
            multi_transfer_msg = MultiTransferMsg(wallet=wallet,
                                                  transfers=transfers,
                                                  to_address=recipient,
                                                  memo=memo)
            transfer_result = await self.client.broadcast_msg(
                multi_transfer_msg)
            return transfer_result[0]['hash']

        except Exception as err:
            raise Exception(str(err))
Example #9
0
    async def transfer(self, asset: Asset, amount, recipient, memo=''):
        """transfer balances

        :param asset: asset object containing : chain , symbol , ticker(optional)
        :type asset: Asset
        :param amount: amount of asset to transfer (don't multiply by 10**8)
        :type amount: int, float, decimal
        :param recipient: destination address
        :type recipient: str
        :param memo: optional memo for transaction
        :type memo: str
        :returns: the transaction hash
        :raises: raises if asset or amount or destination address not provided
        """
        wallet = Wallet(self.get_private_key(), env=self.env)

        if not asset:
            raise Exception('Asset must be provided')
        if not amount:
            raise Exception('Amount must be provided')
        if not recipient:
            raise Exception('Destination address must be provided')

        try:
            transfer_msg = TransferMsg(wallet=wallet,
                                       symbol=asset.symbol,
                                       amount=amount,
                                       to_address=recipient,
                                       memo=memo)
            transfer_result = await self.client.broadcast_msg(transfer_msg)
            return transfer_result[0]['hash']

        except Exception as err:
            return err
Example #10
0
 def __init__(self, env, private_key_string, symbol):
     self.test_net_url = "https://testnet-dex.binance.org/"
     self.main_net_url = "https://dex.binance.org/"
     self.symbol = symbol
     self.env = env
     self.private_key_string = private_key_string
     self.wallet = Wallet(private_key=private_key_string, env=self.env)
     self.cli = HttpApiClient(env=self.env, api_url=self.main_net_url)
Example #11
0
    def test_initialise_from_random_mnemonic(self, env):
        wallet = Wallet.create_random_wallet(env=env)

        assert wallet
        assert wallet.private_key is not None
        assert wallet.public_key is not None
        assert wallet.public_key_hex is not None
        assert wallet.address is not None
Example #12
0
 def __init__(self, loop, env, private_key_string, symbol):
     self.loop = loop
     self.env = env
     self.test_net_url = "https://testnet-dex.binance.org/"
     self.main_net_url = "https://dex.binance.org/"
     self.symbol = symbol
     self.private_key_string = private_key_string
     self.wallet = Wallet(private_key=private_key_string, env=self.env)
Example #13
0
    def test_wallet_reload_sequence(self, private_key, env):
        wallet = Wallet(private_key=private_key, env=env)

        wallet.initialise_wallet()
        account_sequence = wallet.sequence

        wallet.increment_account_sequence()

        wallet.reload_account_sequence()

        assert wallet.sequence == account_sequence
Example #14
0
    def __init__(self, wallet_settings: WalletSettings):

        self._settings = wallet_settings

        w_env = BinanceEnvironment.get_production_env()
        if wallet_settings.env_name == 'TESTNET':
            w_env = BinanceEnvironment.get_testnet_env()

        if wallet_settings.private_key:
            log_init_type = 'private_key'
            self._wallet = Wallet(private_key=wallet_settings.private_key.get_secret_value(), env=w_env)
        elif wallet_settings.mnemonic:
            log_init_type = 'mnemonic'
            self._wallet = Wallet.create_wallet_from_mnemonic(wallet_settings.mnemonic.get_secret_value(), env=w_env)
        else:
            raise Exception(f"Unable to initialise wallet {wallet_settings.name} no private_key or mnemonic set")

        self._http_client: Optional[AsyncHttpApiClient] = None

        logging.info(f"Initialised wallet {wallet_settings.name} with {log_init_type}")
Example #15
0
 def __init__(self, key, test=False):
     self.BUSD = 'BUSD-BD1'
     self.BNB = 'BNB'
     self.pairs = []
     self.bnb_pairs = {}
     self.busd_pairs = {}
     self.api_instance = binance_client.DefaultApi()
     if test:
         self.env = BinanceEnvironment.get_testnet_env()
         self.RUNE = 'RUNE-67C'
         self.api_instance.api_client.configuration.host = self.env.api_url + '/api'
     else:
         self.env = BinanceEnvironment(api_url='https://dex-european.binance.org',
                                       wss_url='wss://dex.binance.org/api/',
                                       hrp='bnb')
         self.RUNE = 'RUNE-B1A'
     binance_logger.info(f'Binance connected to node: {self.env.api_url}')
     self.client = HttpApiClient(env=self.env)
     self.wallet = Wallet(private_key=key, env=self.env)
     self.wallet.reload_account_sequence()
     self.account_info()
Example #16
0
    def __init__(self, wallet_settings: WalletSettings):

        self._settings = wallet_settings

        w_env = BinanceEnvironment.get_production_env()
        if wallet_settings.env_name == 'TESTNET':
            w_env = BinanceEnvironment.get_testnet_env()

        self._wallet = Wallet(
            private_key=wallet_settings.private_key.get_secret_value(),
            env=w_env)

        self._http_client: Optional[AsyncHttpApiClient] = None
class Binance():
    def __init__(self):
        self.env = BinanceEnvironment.get_testnet_env()
        self.client = HttpApiClient(env=self.env)
        with open('secret/bnb_key.txt', 'r') as f:
            key = f.readline()
        self.wallet = Wallet(key, env=self.env)
        self.address = self.wallet.address
        driver_logger.info(f'bnb address: {self.address}')

    def thor_swap(self, i_symbol, amount, to_address, memo):
        transfer_msg = TransferMsg(
            wallet=self.wallet,
            symbol=i_symbol,
            amount=amount,
            to_address=to_address,
            memo=memo
        )
        res = self.client.broadcast_msg(transfer_msg, sync=True)
        tx_hash = res[0]['hash']
        driver_logger.debug(f'boardcasted hash {tx_hash}')
        self.wallet.reload_account_sequence()
        return tx_hash
Example #18
0
def freeze_bnb_wish(amount):
    freeze_env = BinanceEnvironment.get_production_env()
    if NETWORK_SIGN_TRANSACTION_BWISH == 'testnet':
        freeze_env = BinanceEnvironment.get_testnet_env()

    client = HttpApiClient(env=freeze_env)
    bep_wallet = Wallet(BINANCE_PAYMENT_PASSWORD,
                        env=freeze_env)  # from settings
    value = float(amount / 10**18)
    freeze_msg = TransferMsg(wallet=bep_wallet,
                             symbol=COLD_TOKEN_SYMBOL_BNB,
                             amount=value,
                             to_address=COLD_BNB_ADDRESS,
                             memo='freeze bnb wish')
    res = client.broadcast_msg(freeze_msg, sync=True)
    print('result', res, flush=True)
    if not res[0]['ok']:
        raise Exception('cannot make bnb wish freeze tx')
Example #19
0
    async def transfer(self, asset: Asset, amount, recipient, memo=''):
        """transfer balances

        :param asset: asset object containing : chain , symbol , ticker(optional)
        :type asset: Asset
        :param amount: amount of asset to transfer (don't multiply by 10**8)
        :type amount: int, float, decimal
        :param recipient: destination address
        :type recipient: str
        :param memo: optional memo for transaction
        :type memo: str
        :returns: the transaction hash
        :raises: raises if asset or amount or destination address not provided
        """
        wallet = Wallet(self.get_private_key(), env=self.env)

        if not asset:
            raise Exception('Asset must be provided')
        if not amount:
            raise Exception('Amount must be provided')
        if not recipient:
            raise Exception('Destination address must be provided')

        before_balance = await self.get_balance()
        before_balance_amount = before_balance[0].amount
        fee = await self.get_transfer_fee()
        fee = fee['fixed_fee_params']['fee'] * 10**-8
        if (amount + fee) > float(before_balance_amount):
            raise Exception(
                'input asset amout is higher than current (asset balance - transfer fee)'
            )

        try:
            transfer_msg = TransferMsg(wallet=wallet,
                                       symbol=asset.symbol,
                                       amount=amount,
                                       to_address=recipient,
                                       memo=memo)
            transfer_result = await self.client.broadcast_msg(transfer_msg)
            return transfer_result[0]['hash']

        except Exception as err:
            raise Exception(str(err))
Example #20
0
async def binance_packer(config):
    loop = asyncio.get_event_loop()
    # TODO: testnet perhaps? When we get testnet coins.
    env = BinanceEnvironment.get_production_env()
    target_addr = config.binancechain.sync_address.value

    client = AsyncHttpApiClient(env=env)
    wallet = Wallet(config.binancechain.private_key.value, env=env)
    LOGGER.info("BNB Connector set up with address %s" % wallet.address)
    try:
        await loop.run_in_executor(None, wallet.reload_account_sequence)
    except KeyError:
        pass

    i = 0
    while True:
        if (i >= 100):
            try:
                await loop.run_in_executor(None,
                                           wallet.reload_account_sequence)
            except KeyError:
                pass
            # utxo = await get_utxo(config, address)
            i = 0

        messages = [
            message async for message in (await Message.get_unconfirmed_raw(
                limit=500, for_chain=CHAIN_NAME))
        ]
        if len(messages):
            content = await get_chaindata(messages, bulk_threshold=0)
            content = json.dumps(content)
            tx = await loop.run_in_executor(None, prepare_transfer_tx,
                                            wallet, target_addr,
                                            content.encode('utf-8'))
            # tx_hash = await tx.get_hash()
            LOGGER.info("Broadcasting TX")
            await client.broadcast_msg(tx, sync=True)

        await asyncio.sleep(35)

        i += 1
Example #21
0
async def increment_wallet_sequence(wallet: Wallet):
    wallet.increment_account_sequence()
Example #22
0
def make_binance_wallet():
    wallet = Wallet.create_random_wallet(env=testnet_env)
    print(wallet.address)
    print(wallet.private_key)
    print(wallet.public_key_hex)
    return wallet.address
Example #23
0
class Monitor(object):
    def __init__(self):

        self.env = BinanceEnvironment(
            api_url='https://dex-asiapacific.binance.org',
            wss_url='wss://dex.binance.org/api/',
            hrp='bnb')
        with open('bnb_real_key.txt', 'r') as f:
            key = f.readline()
        # for testing
        # self.env = BinanceEnvironment.get_testnet_env()
        # with open('bnb_key.txt', 'r') as f:
        #     key = f.readline()
        f.close()
        self.client = HttpApiClient(env=self.env)
        self.wallet = Wallet(key, env=self.env)
        self.address = self.wallet.address
        self.ftx = ccxt.ftx({
            'apiKey':
            '6p37l5AXOzIgFzfeSzkcuPhuaYcw3GcpJrU83ROy',
            'secret':
            '3SphoJJ6Gl_w5pPPkGmQpKbVGN9oPiUgxqs4ob_H'
        })
        #self.assets = ['USD', 'USDT']
        self.assets = ['USD']
        self.BNB_BUSD = 'BNB.BUSD-BD1'
        self.pool = THORChain()

    def get_bnb_balance(self, asset=None):
        balance = self.client.get_account(self.address)['balances']
        if asset:
            return next(
                filter(lambda symbol: symbol['symbol'] == asset,
                       balance))['free']
        return balance

    def thor_swap(self, i_symbol, amount, to_address, memo):
        self.wallet.reload_account_sequence()
        transfer_msg = TransferMsg(wallet=self.wallet,
                                   symbol=i_symbol,
                                   amount=amount,
                                   to_address=to_address,
                                   memo=memo)
        res = self.client.broadcast_msg(transfer_msg, sync=True)
        tx_hash = res[0]['hash']
        arb_logger.debug(f'broadcasting {tx_hash}')
        return tx_hash

    def round_down(self, number, precision):
        return decimal_to_precision(number_to_string(number), TRUNCATE,
                                    precision)

    def book_oracle_asks(self, book, level, max_output, omega=0.8):
        """ return array of available volume and corresponding unit price """
        book_output_volume = []
        book_output_price = []
        cap = False
        # Book[0-level][0] = Price; Book[0-level][1] = Volume
        for i in range(0, level):
            if cap:
                break
            book_output_price.append(book[i][0])
            out_volume = book[i][1] * omega
            if i > 0:
                out_volume += book_output_volume[i - 1]
            if out_volume >= max_output:
                book_output_volume.append(max_output)
                cap = True
            else:
                book_output_volume.append(out_volume)
        return book_output_price, book_output_volume

    def ftx_price_feed_buy(self, asset, level=7, max_rune=700):
        """Buy Rune on Ftx, sell Rune on Bepswap"""
        ftx_balance = self.get_ftx_balance()
        if ftx_balance['USD'] < 720:
            arb_logger.info(f'need recharge')
            self.deposit_ftx()
            while True:
                time.sleep(1)
                ftx_balance = self.get_ftx_balance()
                if ftx_balance['USD'] > 720:
                    arb_logger.info(f'recharge finished')
                    break
        while True:
            try:
                book = self.ftx.fetch_order_book(f'RUNE/{asset}', level)
                break
            except RequestTimeout as e:
                arb_logger.debug(
                    'Request timeout calling self.ftx.fetch_order_book: {e}')
        arb_logger.debug(f'route 2: fetching asset {asset} \n {book}')
        # Route 2: clearing ask side
        oracle = self.book_oracle_asks(book['asks'], level, max_rune)
        fee = 1.0007
        oracle_out_price = oracle[0]
        oracle_out_volume = oracle[1]
        for i in range(0, len(oracle_out_volume)):
            out_price = oracle_out_price[i]
            out_volume_order = self.round_down(oracle_out_volume[i], 1)
            in_volume = out_price * float(out_volume_order)
            out_volume_real = oracle_out_volume[i] / fee
            # book: asset -> rune
            arb_logger.debug(
                f'book: {in_volume} {asset} := {out_volume_real} RUNE '
                f'RUNE market ask price := {out_price}')
            # pool: rune -> asset
            route = self.pool.get_swap_memo(self.BNB_BUSD,
                                            'sell',
                                            amount=out_volume_real,
                                            limit=in_volume)
            expected = route[0]
            optimal_slice = route[1]
            optimal_expected = route[2]
            pool_address = route[3]
            memo = route[4]
            arb_logger.debug(
                f'pool: {out_volume_real} RUNE := {expected} {asset}')
            arb_logger.debug(f'{memo}')
            diff = expected - in_volume
            if diff > 1.3:
                arb_logger.error(f'profit: {diff}')
                ftx_order = self.ftx.create_order(symbol=f'RUNE/{asset}',
                                                  side='buy',
                                                  amount=out_volume_order,
                                                  price=out_price,
                                                  type='limit')
                tx_hash = self.thor_swap(self.pool.rune,
                                         float(out_volume_real), pool_address,
                                         memo)
                self.pool.get_tx_in(tx_hash=tx_hash)
                database.insert_tx({
                    'thorchain': tx_hash,
                    'ftx': ftx_order['id']
                })
                break
            else:
                arb_logger.warning(f'profit: {diff}')
                time.sleep(0.2)

    def get_ftx_balance(self):
        try:
            return self.ftx.fetch_balance()['total']
        except Exception as e:
            print(e)

    def get_ftx_deposit(self, coin='RUNE'):
        try:
            return self.ftx.fetch_deposit_address(coin)
        except Exception as e:
            print(e)

    def deposit_ftx(self):
        info = self.get_ftx_deposit()
        address = info['address']
        memo = info['tag']
        balance = self.get_bnb_balance(asset='BUSD-BD1')
        print(f'sending {balance} to {address} with memo {memo}')
        self.thor_swap('BUSD-BD1', float(balance) - 1, address, memo)

    def profit_report(self):
        database.add_timestamp()
        txs = database.get_unaccounted_txs()
        rune_gain = 0
        usd_gain = 0
        for tx in txs:
            thor_order = self.pool.get_tx_out(tx_hash=tx['thorchain'])
            thor_rune_in = int(thor_order._in.coins[0]['amount']) / 10**8
            thor_usd_out = int(thor_order.out[0].coins[0]['amount']) / 10**8
            ftx_order = self.ftx.fetch_order(tx['ftx'])
            ftx_usd_in = ftx_order['cost']
            ftx_rune_out = ftx_order['filled']
            ftx_remaining = ftx_order['remaining']
            if ftx_remaining != 0:
                database.add_profit_txs(tx['_id'], 0, 0, 'ftx fail')
            elif thor_order.out[0].coins[0]['asset'] == 'BNB.RUNE-B1A':
                database.add_profit_txs(tx['_id'], -1 * ftx_usd_in,
                                        ftx_rune_out - 1, 'thor fail')
            else:
                temp_rune_gain = ftx_rune_out - thor_rune_in
                temp_usd_gain = thor_usd_out - ftx_usd_in
                database.add_profit_txs(tx['_id'], temp_usd_gain,
                                        temp_rune_gain, 'success')
                rune_gain += temp_rune_gain
                usd_gain += temp_usd_gain
        print(f'rune_gain: {rune_gain}')
        print(f'usd_gain: {usd_gain}')

    def fail_report(self):
        txs = database.get_failed_txs()
        for tx in txs:
            print(tx)
Example #24
0
class BinanceApi:
    def __init__(self, key, test=False):
        self.BUSD = 'BUSD-BD1'
        self.BNB = 'BNB'
        self.pairs = []
        self.bnb_pairs = {}
        self.busd_pairs = {}
        self.api_instance = binance_client.DefaultApi()
        if test:
            self.env = BinanceEnvironment.get_testnet_env()
            self.RUNE = 'RUNE-67C'
            self.api_instance.api_client.configuration.host = self.env.api_url + '/api'
        else:
            self.env = BinanceEnvironment(api_url='https://dex-european.binance.org',
                                          wss_url='wss://dex.binance.org/api/',
                                          hrp='bnb')
            self.RUNE = 'RUNE-B1A'
        binance_logger.info(f'Binance connected to node: {self.env.api_url}')
        self.client = HttpApiClient(env=self.env)
        self.wallet = Wallet(private_key=key, env=self.env)
        self.wallet.reload_account_sequence()
        self.account_info()

    def parse_market(self):
        offset = 0
        try:
            logging.info("parsing market pairs")
            while True:
                pairs = self.api_instance.get_pairs(limit=500, offset=offset)
                for pair in pairs:
                    self.pairs.append(pair)
                    if pair["quote_asset_symbol"] == self.BNB:
                        self.bnb_pairs[pair["base_asset_symbol"]] = pair["lot_size"]
                    elif pair["quote_asset_symbol"] == self.BUSD:
                        self.busd_pairs[pair["base_asset_symbol"]] = pair["lot_size"]
                offset += 500
                time.sleep(1)
        except ApiException as e:
            if e.reason == "Bad Request":
                logging.info("parsing finished, %s market pairs" % len(self.pairs))
                logging.debug("bnb pairs: %s" % self.bnb_pairs)
                logging.debug("busd pairs: %s" % self.busd_pairs)
            else:
                logging.info("Exception when calling DefaultApi->getPairs: %s\n" % e)

    def depth(self, symbol, bnb=True):
        if bnb:
            pair = symbol + '_' + self.BNB
        else:
            pair = symbol + '_' + self.BUSD
        try:
            depth = self.api_instance.get_depth(symbol=pair, limit=5)
            binance_logger.debug(f'{pair} market depth bids:{depth.bids[0]} asks:{depth.asks[0]}')
            return depth
        except ApiException as e:
            if e.reason == 'Gateway Time-out':
                logging.info("cloudfront error")
                time.sleep(5)
                logging.info("recalling get_depth()")
                return self.depth(symbol=pair, bnb=bnb)
            binance_logger.debug(f'Exception when calling get_depth() {e}')

    def account_info(self):
        try:
            account_info = self.api_instance.get_account(self.wallet.address)
            binance_logger.info(f'account info: {account_info}')
            return account_info
        except ApiException as e:
            binance_logger.debug(f'Exception when calling get_account() {e}')
        # tracking = list(filter(lambda coin: coin['symbol'] == self.pairs[token], api_response.balances))
        # pnl = (float(tracking[0]['free']) - float(self.balances[token])) / float(self.balances[token])
        # self.balances[token] = float(tracking[0]['free'])

    def get_balance(self):
        try:
            account_info = self.api_instance.get_account(self.wallet.address)
            balance_info = account_info.balances
            binance_logger.info(f'balance info: {balance_info}')
            binance_logger.info(f'sequence number: {account_info.sequence}')
            return balance_info
        except ApiException as e:
            binance_logger.debug(f'Exception when calling get_account() {e}')

    def binance_check_hash(self, hash):
        while True:
            try:
                api_response = self.api_instance.get_closed_orders(self.wallet.address)
                order = list(filter(lambda order: order.transaction_hash == hash, api_response.order))
                if order:
                    binance_logger.debug(f'order detail {order[0]}')
                    binance_logger.info(f'order status {order[0].status}')
                    return order[0]
                time.sleep(0.5)
            except ApiException as e:
                binance_logger.debug(f'Exception when calling get_closed_orders() {e}')

    def dex_buy(self, symbol, quantity, price=None, bnb=True):
        self.wallet.reload_account_sequence()
        if price is None:
            depth = self.depth(symbol, bnb=bnb)
            price = float(depth.asks[0][0])
        if bnb:
            pair = symbol + '_' + self.BNB
        else:
            pair = symbol + '_' + self.BUSD
        buy_msg = NewOrderMsg(
            wallet=self.wallet,
            symbol=pair,
            time_in_force=TimeInForce.IMMEDIATE_OR_CANCEL,
            order_type=OrderType.LIMIT,
            side=OrderSide.BUY,
            price=price,
            quantity=quantity
        )
        res = self.client.broadcast_msg(buy_msg, sync=True)
        binance_logger.info(f'buy response: {res}')
        hash = res[0]['hash']
        time.sleep(0.1)
        return self.binance_check_hash(hash)

    def dex_sell(self, symbol, quantity, price=None, bnb=True):
        self.wallet.reload_account_sequence()
        if price is None:
            depth = self.depth(symbol, bnb=bnb)
            price = float(depth.bids[0][0])
        if bnb:
            pair = symbol + '_' + self.BNB
        else:
            pair = symbol + '_' + self.BUSD
        sell_msg = NewOrderMsg(
            wallet=self.wallet,
            symbol=pair,
            time_in_force=TimeInForce.IMMEDIATE_OR_CANCEL,
            order_type=OrderType.LIMIT,
            side=OrderSide.SELL,
            price=price,
            quantity=quantity
        )
        res = self.client.broadcast_msg(sell_msg, sync=True)
        binance_logger.info(f'sell response: {res}')
        hash = res[0]['hash']
        time.sleep(0.1)
        return self.binance_check_hash(hash)

    def thor_swap(self, chain, i_symbol, o_symbol, amount, to_address, dest_address=None, limit=None):
        if limit:
            memo = 'SWAP:' + chain + '.' + o_symbol + '::' + str(int(limit * 10**8))
        else:
            memo = 'SWAP:' + chain + '.' + o_symbol
        transfer_msg = TransferMsg(
            wallet=self.wallet,
            symbol=i_symbol,
            amount=amount,
            to_address=to_address,
            memo=memo
        )
        res = self.client.broadcast_msg(transfer_msg, sync=True)
        binance_logger.info(f'swap response: {res}')
        hash = res[0]['hash']
        return hash

    def thor_smart_swap(self, chain, i_symbol, o_symbol, amount, to_address, dest_address=None, limit=None, slice=3):
        hashes = []
        piece = float(amount/slice)
        binance_logger.info(f'smart swap with {slice} slices of {piece} {i_symbol}')
        if limit:
            piece_limit = float(limit/slice)
            for part in range(slice):
                hash = self.thor_swap(chain=chain, i_symbol=i_symbol, o_symbol=o_symbol, amount=piece, to_address=to_address, limit=piece_limit)
                hashes.append(hash)
        else:
            for part in range(slice):
                hash = self.thor_swap(chain=chain, i_symbol=i_symbol, o_symbol=o_symbol, amount=piece, to_address=to_address)
                hashes.append(hash)
        return hashes

    def thor_stake(self, chain, symbol, amount, runeamount, to_address):
        memo = 'STAKE:' + chain + '.' + symbol
        multi_transfer_msg = MultiTransferMsg(
            wallet=self.wallet,
            transfers=[
                Transfer(symbol=self.RUNE, amount=runeamount),
                Transfer(symbol=symbol, amount=amount),
            ],
            to_address=to_address,
            memo=memo
        )
        res = self.client.broadcast_msg(multi_transfer_msg, sync=True)
        binance_logger.info(f'stake response: {res}')
        hash = res[0]['hash']
        return hash

    def thor_withdraw(self, chain, symbol, percent, to_address):
        if chain == 'BNB':
            payload = 0.00000001
            payload_token = 'BNB'
        else:
            payload = 0
        percent_str = str(int(percent * 10**2))
        memo = "WITHDRAW:" + chain + '.' + symbol + ':' + percent_str
        transfer_msg = TransferMsg(
            wallet=self.wallet,
            symbol=payload_token,
            amount=payload,
            to_address=to_address,
            memo=memo
        )
        res = self.client.broadcast_msg(transfer_msg, sync=True)
        binance_logger.info(f'withdraw response: {res}')
        hash = res[0]['hash']
        return hash
Example #25
0
 def create_bnb_wallet(self) -> Wallet:
     """generate Mnemonic --> wallet"""
     w = Wallet.create_random_wallet(env=self.env)
     print(w.address)
     print(w.private_key)
     return w
Example #26
0
 def _create_wallet_by_private_key(self) -> Wallet:
     w = Wallet(private_key=self.private_key_string, env=self.env)
     return w
 def wallet(self, private_key, env):
     wallet = Wallet(private_key=private_key, env=env)
     wallet._address = 'tbnb10a6kkxlf823w9lwr6l9hzw4uyphcw7qzrud5rr'
     return wallet
Example #28
0
 def wallet(self, env, private_key):
     return Wallet(private_key=private_key, env=env)
Example #29
0
import os
from binance_chain.http import HttpApiClient
from binance_chain.messages import TransferMsg, Transfer
from binance_chain.wallet import Wallet
from binance_chain.environment import BinanceEnvironment

default_pkey = ""
testnet_env = BinanceEnvironment.get_testnet_env()
print(testnet_env)
testnet_priv_key = os.environ.get("BNB_PKEY", default_pkey)
wallet = Wallet(testnet_priv_key, env=testnet_env)
bnb_address = wallet.address
client = HttpApiClient(env=testnet_env)


class BnbClient:
    def get_address(self):
        return bnb_address

    def get_address_account(self, address):
        return client.get_account(address)

    def get_account(self):
        return self.get_address_account(bnb_address)

    def get_address_balance(self, symbol, address):
        balances = self.get_address_account(address)['balances']
        print(symbol)
        print(balances)
        for b in balances:
            if b['symbol'] == symbol:
Example #30
0
    def test_wallet_create_from_private_key(self, private_key, env):
        wallet = Wallet(private_key=private_key, env=env)

        assert wallet
        assert wallet.public_key_hex == b'02cce2ee4e37dc8c65d6445c966faf31ebfe578a90695138947ee7cab8ae9a2c08'
        assert wallet.address == 'tbnb10a6kkxlf823w9lwr6l9hzw4uyphcw7qzrud5rr'