1

What is the best way to keep my dapp state synced with that of the blockchain? Specifically, I'm interested in the best practices around handling updates to the blockchain that happen outside of my application.

A couple scenarios that I'm trying to understand how best to handle:

  • A user of my application performs an action which triggers a blockchain interaction. If the user remains on the application, I can use the same library I did to submit the transaction to also watch for the results. When the results are observed, I update app state (or database state). But what happens when a transaction is initiated on the blockchain and my user navigates away from the application or closes the browser window entirely? How would I best keep track of the results of that transaction so that I can update things accordingly on my side (dapp/db)?

  • A user of my application is provided with a list of NFTs that exist in a contract I've deployed. A third party (someone not using the dapp) interacts with the contract (mints a new token, lets say) and subsequently updates its state. What is the best practice currently for monitoring those external changes so that I can reflect them inside the application?

Thanks in advance!

2 Answers 2

2

I'd recommend looking into indexers, such as TzKT or TzPro, for such data.

For example, TzKT has a collection of websocket APIs usings C#'s SignalR. This allows you to subscribe to accounts or operation types, and be notified whenever anything changes onchain. Here is a link to the docs on how to subscribe to an account: https://api.tzkt.io/#section/SubscribeToAccounts . Once your user logs in, your dApp could start subscribing to this socket, passing the users wallet address. You will be notified when the user performs an action on the blockchain, allowing you to then refresh the state/balances of the account. You could do this from the frontend for each user that logs in, or you could monitor many addresses from 1 connection on your server (if one exists) and control it all via bulk updates. Which path to choose would be heavily dependant on your usecase

Similarly they offer another another endpoint for monitoring token balances of accounts or contracts: https://api.tzkt.io/#section/SubscribeToTokenBalances . Again same thing, set up a subscription to this for your contract(s) and update your DB/frontend whenever it updates

Note that TzKT provide a lot of access to free tooling, but if you are going to be building something that has a lot of users, you may need to deploy your own TzKT instance, or speak to them about gaining access to a managed instance

1

Managing the state of your application and updating it to reflect the state of a blockchain works the same way as with any other external state, whether it's a connection to an SQL database or a REST endpoint.

Here are a couple things to keep in mind.

  1. Blockchain as the source of truth: The blockchain is always the ultimate source of truth. However, direct queries to it can be slow and resource-intensive.
  2. Indexed data for performance: Retrieving indexed data from the blockchain is generally faster but comes with caveats:
    • Not all data might be indexed, which can limit the information available for quick retrieval.
    • There's a slight chance that indexed data might not fully represent the latest state due to network delays or inconsistencies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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