Balances

Common info

In Wallets, a request is made to display the balance of wallets:

query { user(id: "<userId>") { currencies { id enabled balance advancedTradingBalance advancedTradingLockedBalance lockedBalance withdrawLockedBalance stakingLockedBalance activeStakingBalance } } }


All these balances directly or indirectly invoke the getBalance method sending the currencyId and account.

Logic getBalance

To get the data, we have the following variables in memory:

  • USER_IDS []- Array userId to get the index

  • CURRENCY_IDS []- Array currencyId to get the index

  • Balances {}- To store the balance for the user in the form of:

    • Balances[userNum][currencyNum][account]

      • userNum - user index in USER_IDS

      • currencyNum - currency index in CURRENCY_IDS

      • account - the account you want to see

  • USER_BALANCE_CALCULATION_QUEUE [SET]-A SET that contains the userId of the user who is currently counting balances so that other operations for obtaining balances do not count the balance as well, but wait for its completion

Steps

  1. When you enter the function, it is checked if there is a userId in USER_IDS

    1. If there is such an id, then calling Balances[userNum][currencyNum][account] returns its value

  2. If the user is not found, it checks if there is already a function running to calculate the balance by checking for the presence of userId in USER_BALANCE_CALCULATION_QUEUE

    1. If it found it in the USER_BALANCE_CALCULATION_QUEUE, it waits until it is removed from it by polling it at a certain interval.

    2. If it does not find it in the set, it returns the balance value by referring to the value in memory Balances[userNum][currencyNum][account]

  3. If balance calculation is not running, it writes to the USER_BALANCE_CALCULATION_QUEUE of the user and starts calculating balances by calling loadUserBalances

    1. The asynchronous calculation of the balance for each currency is started by calling userCurrencyBalance

      1. Retrieve the balance data from the database for accounts [201, 202, 203, 204, 205, 206, 230] and return it

    2. Add a user to USER_IDS

    3. In Balances, add data on balances

    4. Remove the user from the USER_BALANCE_CALCULATION_QUEUE

    5. Returning balances Balances[userNum][currencyNum][account]

Â