示例#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}')
示例#3
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)
示例#4
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')
示例#5
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()
    async def test_depthcache_create(self, event_loop, env):

        async def callback(_depth_cache):
            pass

        client = HttpApiClient(env=env)

        dcm1 = await DepthCacheManager.create(client, event_loop, "MITH-C76_BNB", callback, env=env)

        assert dcm1

        assert dcm1.get_depth_cache()

        await dcm1.close()
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
 def httpclient(self, env):
     return HttpApiClient(env=env)
示例#9
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
示例#10
0
from scanner.blockchain_common.wrapper_block import WrapperBlock
from scanner.blockchain_common.wrapper_network import WrapperNetwork
from scanner.blockchain_common.wrapper_output import WrapperOutput
from scanner.blockchain_common.wrapper_transaction import WrapperTransaction
from scanner.eventscanner.queue.pika_handler import send_to_backend
from binance_chain.exceptions import BinanceChainAPIException

from binance_chain.http import HttpApiClient
from binance_chain.http import PeerType
from binance_chain.constants import KlineInterval
from binance_chain.environment import BinanceEnvironment
from binance_chain.node_rpc.http import HttpRpcClient

#Setting Binance-Chain params, testnet_env for testnet
client = HttpApiClient(request_params={"verify": False, "timeout": 60})
#client = HttpApiClient()
#testnet_env = BinanceEnvironment.get_testnet_env()
#client = HttpApiClient(env=testnet_env, request_params={"verify": False, "timeout": 60})
peers = client.get_node_peers()
listen_addr = peers[0]['listen_addr']
rpc_client = HttpRpcClient(listen_addr)
processed = []
commited = []


class BinNetwork(WrapperNetwork):
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.append(BASE_DIR)
    base_dir = 'scanner/settings'
示例#11
0
 def httpclient(self, env):
     return HttpApiClient(env=env, requests_params={'timeout': 1})
示例#12
0
 def _get_http_client(self):
     if not self._http_client:
         from binance_chain.http import HttpApiClient
         self._http_client = HttpApiClient(self._env)
     return self._http_client
示例#13
0
from binance_chain.environment import BinanceEnvironment
from binance_chain.http import HttpApiClient
from binance_chain.wallet import Wallet

testnet_env = BinanceEnvironment.get_testnet_env()
client = HttpApiClient(env=testnet_env)


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
示例#14
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)
from blockchain_common.eth_tokens import erc20_abi
from blockchain_common.wrapper_block import WrapperBlock
from blockchain_common.wrapper_network import WrapperNetwork
from blockchain_common.wrapper_output import WrapperOutput
from blockchain_common.wrapper_transaction import WrapperTransaction
from blockchain_common.wrapper_transaction_receipt import WrapperTransactionReceipt
from settings.settings_local import NETWORKS, ERC20_TOKENS

from binance_chain.http import HttpApiClient
from binance_chain.http import PeerType
from binance_chain.constants import KlineInterval
from binance_chain.environment import BinanceEnvironment
from binance_chain.node_rpc.http import HttpRpcClient

client = HttpApiClient(request_params={"verify": False, "timeout": 20})
httpapiclient = HttpApiClient()
peers = httpapiclient.get_node_peers()
listen_addr = peers[0]['listen_addr']
rpc_client = HttpRpcClient(listen_addr)
processed = []
commited = []


class BinNetwork(WrapperNetwork):
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.append(BASE_DIR)
    base_dir = 'settings'

    def __init__(self, type):
        super().__init__(type)
示例#16
0
class Hyf(object):
    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)

    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

    def _create_wallet_by_private_key(self) -> Wallet:
        w = Wallet(private_key=self.private_key_string, env=self.env)
        return w

    def broadcast_transaction(self, order_msg, sync=True):
        res = self.cli.broadcast_msg(order_msg, sync)
        return res

    def new_order_msg(self, order_type, side, symbol, price, quantity):
        """下订单"""
        new_order_msg = NewOrderMsg(
            wallet=self.wallet,
            symbol=symbol,
            time_in_force=TimeInForce.GOOD_TILL_EXPIRE,
            order_type=order_type,
            side=side,
            price=price,
            quantity=Decimal(float(quantity))
        )
        return new_order_msg

    def cancel_order(self, order_id):
        cancel_order_msg = CancelOrderMsg(
            wallet=self.wallet,
            order_id=order_id,
            symbol=self.symbol,
        )
        res = self.broadcast_transaction(cancel_order_msg, sync=True)
        print(res)
        return res

    def cancel_all_orders(self, address):
        """取消所有挂单"""
        open_order = self.get_order_open(address)
        self.get_order_close(address)
        for o in open_order["order"]:
            order_id = o.get("orderId", None)
            if not order_id:
                print("order is none")
                break
            self.cancel_order(order_id)

    def limit_order_msg_buy(self, symbol, price, quantity):
        """Limit Order Buy"""
        limit_order_msg = LimitOrderBuyMsg(
            wallet=self.wallet,
            symbol=symbol,
            price=price,
            quantity=Decimal(float(quantity))
        )
        return limit_order_msg

    def limit_order_msg_sell(self, symbol, price, quantity):
        """Limit Order Sell"""
        limit_order_msg = LimitOrderSellMsg(
            wallet=self.wallet,
            symbol=symbol,
            price=price,
            quantity=Decimal(float(quantity))
        )
        return limit_order_msg

    def get_banlance(self, addr):
        balance = self.cli.get_account(addr)
        print(balance)
        return balance

    def get_order_book(self):
        order_book = self.cli.get_order_book(self.symbol)
        print(order_book)
        return order_book

    def unfreeze_tokens(self, base_symbol, amount):
        """解冻tokens"""
        unfreeze_msg = UnFreezeMsg(
            wallet=self.wallet,
            symbol=base_symbol,
            amount=Decimal(float(amount))
        )
        res = self.cli.broadcast_msg(unfreeze_msg, sync=True)
        print(res)
        return res

    def get_buy_sell_one(self, symbol) -> float:
        """获取买卖单第一个价格"""
        order_book = self.cli.get_order_book(symbol)
        buy_order_list = order_book.get("bids", [])
        sell_order_list = order_book.get("asks", [])
        if len(buy_order_list) == 0 or len(sell_order_list) == 0:
            return 0, 0
        buy_one = buy_order_list[0][0]
        sell_one = sell_order_list[0][0]
        return Decimal(float(buy_one)), Decimal(float(sell_one))

    def get_order_open(self, addr):
        open_orders = self.cli.get_open_orders(addr)
        print("open_orders")
        print(open_orders)
        return open_orders

    def get_order_close(self, addr):
        closed_orders = self.cli.get_closed_orders(addr)
        print("closed_orders")
        print(closed_orders)
        return closed_orders

    def start_order(self, total_quantity, quantity):
        """开始刷单"""
        real_total = 0
        for i in range(0, int(total_quantity/quantity)):
            try:
                start = time.time()
                buy_one, sell_one = self.get_buy_sell_one(self.symbol)
                avg = Decimal((buy_one+sell_one)/2).quantize(Decimal('0.000'))
                print(avg)
                buy_order_msg = self.new_order_msg(order_type=OrderType.LIMIT, side=OrderSide.BUY, symbol=self.symbol,
                                                   price=avg, quantity=quantity)
                sell_order_msg = self.new_order_msg(order_type=OrderType.LIMIT, side=OrderSide.SELL, symbol=self.symbol,
                                                    price=avg, quantity=quantity)
                sell_order_order = self.broadcast_transaction(order_msg=sell_order_msg)
                print(sell_order_order)
                buy_order_order = self.broadcast_transaction(order_msg=buy_order_msg)
                print(buy_order_order)
                end = time.time()
                print("time :" + str(end-start))
                real_total += quantity

            except Exception as e:
                print("start_order error:")
                print(e)
                break
        print(real_total)

        return True