DEV Community

maxloop
maxloop

Posted on

Best Approach to Processing Bitcoin Deposits Using External APIs

Hello!

I would like to process deposits, but I am not quite sure how to best go about it. Unfortunately, I cannot and do not want to operate my own node at the moment (because then I have to download the entire blockchain and it is complicated to work directly with the node), to process deposits, so only an external API comes into question.

Here's the scenario: I have only one address to which all deposits should go. My service works without accounts and the sender's address is the UserID. The user should be directly shown on the website that the transaction has been received. In addition, the confirmations of the transactions should be updated and displayed promptly. But only up to 6 confirmations. After that, the deposit is successful, and we do not need to query the additional confirmations. It is important to me that if my web server goes offline, the system also processes the deposits during the time the server was offline.

My solution with which I am not quite satisfied and I hope there is a better and more efficient solution: I query all transactions of my deposit address every 2 minutes. But this also gives me the old deposits that have already been processed. Therefore, the server must first look at which deposits are new to include them in the database. Then I still run a websocket, which immediately informs me when there is a new block. And if that is the case, I query the transaction details for all deposits that still do not have 6 confirmations, to get the current number of confirmations. And I only do that whenever a new block was calculated, since no new confirmation can happen before that. And then I query all transactions of my deposit address again every 2 minutes and look for the new deposits that are not yet in the system. And again and again.

My problem with my solution: The API I currently use only outputs the last 10,000 transactions. This means that as soon as 10,001 transactions come in new within 2 minutes, then one transaction is lost. Although that is already a very unrealistic scenario to receive that many deposits in such a short time. However, it also bothers me that it is not really efficient. Because I always query all transactions and then have to sort out again and again, although I only need the new transactions. I hope that there might be a better way. I thought I could ask Blockchair for transactions within a certain period of time. So if the last query was for the period 12:04:00 to 12:06:00, then I query the transactions in the time between 12:06:00 to 12:08:00 in the next API call. And if there is ever downtime, then I just take the time of the last retrieval and the current time. But Blockchair somehow does not allow filtering by date. And other providers do not offer that either...

My question: How would you proceed? What is the best and most efficient way to process deposits? Deposits should always go to just one address and be booked as successful at 6 confirmations. In addition, users should always be promptly informed about new confirmations.

Maybe you also know directly a provider with a good and affordable API... I would be very grateful. I'm still a bit new to the topic.

Many, many thanks and have a good day!

Top comments (0)