@@ -18,6 +18,8 @@ public class BlockCypher : ApiBase, IBalanceApi
1818 public async Task < Response > UpdateBalancesAsync ( List < BitcoinAddress > addrList )
1919 {
2020 Response resp = new ( ) ;
21+ int count = 0 ;
22+ Stopwatch timer = Stopwatch . StartNew ( ) ;
2123 foreach ( BitcoinAddress addr in addrList )
2224 {
2325 string url = $ "https://api.blockcypher.com/v1/btc/main/addrs/{ addr . Address } /balance";
@@ -39,6 +41,20 @@ public async Task<Response> UpdateBalancesAsync(List<BitcoinAddress> addrList)
3941 decimal bal = t . Value * Constants . Satoshi ;
4042 addr . Difference = bal - addr . Balance ;
4143 addr . Balance = bal ;
44+
45+ // Blockcypher limits calls to 3 per second
46+ if ( count == 3 )
47+ {
48+ count = 0 ;
49+ timer . Stop ( ) ;
50+ if ( timer . Elapsed < TimeSpan . FromSeconds ( 1 ) )
51+ {
52+ int miliSecDelay = TimeConstants . MilliSeconds . OneSec - timer . Elapsed . Milliseconds ;
53+ Debug . Assert ( miliSecDelay > 0 ) ;
54+ await Task . Delay ( miliSecDelay + 100 ) ;
55+ }
56+ timer . Restart ( ) ;
57+ }
4258 }
4359
4460 resp . IsSuccess = true ;
@@ -49,6 +65,8 @@ public async Task<Response> UpdateBalancesAsync(List<BitcoinAddress> addrList)
4965 public async Task < Response > UpdateTransactionListAsync ( List < BitcoinAddress > addrList )
5066 {
5167 Response resp = new ( ) ;
68+ int count = 0 ;
69+ Stopwatch timer = Stopwatch . StartNew ( ) ;
5270 foreach ( var addr in addrList )
5371 {
5472 string url = "https://api.blockcypher.com/v1/btc/main/addrs/" + addr . Address + "?limit=2000" ;
@@ -113,9 +131,22 @@ public async Task<Response> UpdateTransactionListAsync(List<BitcoinAddress> addr
113131 }
114132
115133 addr . TransactionList = temp ;
134+
135+ // Blockcypher limits calls to 3 per second
136+ if ( count == 3 )
137+ {
138+ count = 0 ;
139+ timer . Stop ( ) ;
140+ if ( timer . Elapsed < TimeSpan . FromSeconds ( 1 ) )
141+ {
142+ int miliSecDelay = TimeConstants . MilliSeconds . OneSec - timer . Elapsed . Milliseconds ;
143+ Debug . Assert ( miliSecDelay > 0 ) ;
144+ await Task . Delay ( miliSecDelay + 100 ) ;
145+ }
146+ timer . Restart ( ) ;
147+ }
116148 }
117149 return resp ;
118150 }
119-
120151 }
121152}
0 commit comments