I'm building a simple wallet app and I want to keep the balance of my account up to date in the wallet. I have thought of a couple ways to do this, but neither seem that great.

OPTION 1: Query the account balance each time a transaction comes through on my transaction stream, and store this in my database. CON: If multiple transactions are processed when the app is started, there will be multiple queries fetching the same balance value. Also, I'm now storing the balance in my database, away from the Stellar network and I assume this is not ideal?

OPTION 2: Query the account balance directly (ie not prompted by the transaction stream nor stored in the database). CON: There isn't a balance listener (that I know of) that could keep the value up to date without refreshing the app.

Of these, which would you recommend, or do you have a third, better way?

up vote 1 down vote accepted

Both are feasible, but Option 2 is surely easier: setting up something periodical like a cron job that gets the account balance from Horizon every 5 seconds. No need to do as frequent as every 1 second, as a new ledger is always born every 5 seconds. The bad thing is some wastage when the account balance is idle unchanged for long time.

Option 1 is likely the better approach, but I am not sure what your intention is to store the balance at DB. You will need something like setInterval to do "get balance" at most once every 5 seconds, so that only 1 query will be made when multiple transactions are announced with the same ledger.

  • My reason for wanting to store the balance in the DB is to avoid having to fetch it from Horizon regularly. This way it is only updated on successful transactions. I considered the security implication of this, but given that it would only be for display purposes (all transfers would be based on success on the Stellar network - not involving my stored balance in any way), I figured it was fine. What do you think? – Christopher Mills Dec 5 at 7:38
  • 1
    If your application does not require a 100% precisely up-to-date balance, this implementation will be fine. To me, the most special feature is the 5 second gap between ledgers. If you own the Horizon, you may consider utilizing the Horizon settings x-ratelimit-limit, x-ratelimit-remaining & x-ratelimit-reset to avoid "multiple queries fetching the same balance value". – cesarm Dec 6 at 1:43

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.