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

BingX handle_balance() keyError, called in watch_orders() #22264

Open
chazman2 opened this issue Apr 24, 2024 · 2 comments
Open

BingX handle_balance() keyError, called in watch_orders() #22264

chazman2 opened this issue Apr 24, 2024 · 2 comments
Assignees

Comments

@chazman2
Copy link

Operating System

Mac

Programming Languages

Python

CCXT Version

4.2.76

Description

In bingx.py, handle_balance() has a dictionary keyError. Namely, it calls self.balance[type]['info'] = data, before self.balance has been defined as a dictionary. I suggest adding a line self.balance[type] = {} before making this call. The code below is the unedited version.

I found this error because handle_balance() is called in watch_orders() (watching swaps), and it constantly drew a keyError.

Code

      def handle_balance(self, client: Client, message):
        # spot
        #     {
        #         "e":"ACCOUNT_UPDATE",
        #         "E":1696242817000,
        #         "T":1696242817142,
        #         "a":{
        #            "B":[
        #               {
        #                  "a":"USDT",
        #                  "bc":"-1.00000000000000000000",
        #                  "cw":"86.59497382000000050000",
        #                  "wb":"86.59497382000000050000"
        #               }
        #            ],
        #            "m":"ASSET_TRANSFER"
        #         }
        #     }
        # swap
        #     {
        #         "e":"ACCOUNT_UPDATE",
        #         "E":1696244249320,
        #         "a":{
        #            "m":"WITHDRAW",
        #            "B":[
        #               {
        #                  "a":"USDT",
        #                  "wb":"49.81083984",
        #                  "cw":"49.81083984",
        #                  "bc":"-1.00000000"
        #               }
        #            ],
        #            "P":[
        #            ]
        #         }
        #     }
        #
        a = self.safe_value(message, 'a', {})
        data = self.safe_value(a, 'B', [])
        timestamp = self.safe_integer_2(message, 'T', 'E')
        type = 'swap' if ('P' in a) else 'spot'
        self.balance[type]['info'] = data
        self.balance[type]['timestamp'] = timestamp
        self.balance[type]['datetime'] = self.iso8601(timestamp)
        for i in range(0, len(data)):
            balance = data[i]
            currencyId = self.safe_string(balance, 'a')
            code = self.safe_currency_code(currencyId)
            account = self.balance[type][code] if (code in self.balance[type]) else self.account()
            account['free'] = self.safe_string(balance, 'wb')
            balanceChange = self.safe_string(balance, 'bc')
            if account['used'] is not None:
                account['used'] = Precise.string_sub(self.safe_string(account, 'used'), balanceChange)
            self.balance[type][code] = account
        self.balance[type] = self.safe_balance(self.balance[type])
        client.resolve(self.balance[type], type + ':balance')

@carlosmiei
Copy link
Collaborator

Hello @chazman2, thanks for reporting it, we will take a look

@carlosmiei carlosmiei self-assigned this Apr 24, 2024
@carlosmiei
Copy link
Collaborator

@chazman2 Locally I can't reproduce this issue, can you please update to the latest version and try again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants