tdex-network / tdex-daemon-alpha

💰 Alpha Daemon implementation to run automated market making strategies on top of TDEX
MIT License
0 stars 2 forks source link

Query utxos from local database #25

Closed tiero closed 4 years ago

tiero commented 4 years ago

Before this PR, we would have to query the external explorer on-demand (most of the time via the public internet) to get utxos and balances, thus making the response time slower.

To solve this we introduce an abstraction Unspent and new collections Unspents that takes care of having a local cache. The services now query from the datastore, with a failover to explorer network call only in case of an error on reading.

The crawler has been refactored to always fetching the utxos and updating the local datastore using a new event of type BALANCE. If the daemon stops and restart, the crawlers will be started again against both the fee account and all the registered markets.

When a tradePropose is executed as the last thing we flag the consumed utxos with spent boolean and we reference those with the ID of the SwapAccept message, so in case of failure in tradeComplete step (basically no actual swap executed on-chain), we unlock those utxos so we can use it again.

Minor changes:

This closes #22

tiero commented 4 years ago

@altafan This is very important to be seen, even if merged already because it's a substantial change and increase the overall complexity, but it prepares for a smooth transition to support both a better fee management #17 and most importantly it helps to reason in preparation for concurrent swap requests #15