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 []
- ArrayuserId
to get the indexCURRENCY_IDS []
- ArraycurrencyId
to get the indexBalances {}
- To store the balance for the user in the form of:Balances[userNum][currencyNum][account]
userNum
- user index inUSER_IDS
currencyNum
- currency index inCURRENCY_IDS
account
- the account you want to see
USER_BALANCE_CALCULATION_QUEUE [SET]
-A SET that contains theuserId
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
When you enter the function, it is checked if there is a
userId
inUSER_IDS
If there is such an
id
, then callingBalances[userNum][currencyNum][account]
returns its value
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
inUSER_BALANCE_CALCULATION_QUEUE
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.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]
If balance calculation is not running, it writes to the
USER_BALANCE_CALCULATION_QUEUE
of the user and starts calculating balances by callingloadUserBalances
The asynchronous calculation of the balance for each currency is started by calling
userCurrencyBalance
Retrieve the balance data from the database for accounts
[201, 202, 203, 204, 205, 206, 230]
and return it
Add a user to
USER_IDS
In
Balances
, add data on balancesRemove the user from the
USER_BALANCE_CALCULATION_QUEUE
Returning balances
Balances[userNum][currencyNum][account]
Â