Skip to content

Commit

Permalink
Replace Blockchair API with BlocksDecoded proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelekol committed Feb 27, 2024
1 parent 1cc8b7d commit e7d223c
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class BitcoinCashKit : AbstractKit {
is NetworkType.MainNet -> {

if (syncMode is SyncMode.Blockchair) {
val blockchairApi = BlockchairApi(syncMode.key, network.blockchairChainId)
val blockchairApi = BlockchairApi(network.blockchairChainId)
val blockchairBlockHashFetcher = BlockchairBlockHashFetcher(blockchairApi)
val blockHashFetcher = BlockHashFetcher(hsBlockHashFetcher, blockchairBlockHashFetcher, checkpoint.block.height)
val blockchairProvider = BlockchairTransactionProvider(blockchairApi, blockHashFetcher)
Expand Down Expand Up @@ -305,7 +305,7 @@ class BitcoinCashKit : AbstractKit {
"BitcoinCash-${networkType.description}-$walletId-${syncMode.javaClass.simpleName}"

fun clear(context: Context, networkType: NetworkType, walletId: String) {
for (syncMode in listOf(SyncMode.Api(), SyncMode.Full(), SyncMode.Blockchair(""))) {
for (syncMode in listOf(SyncMode.Api(), SyncMode.Full(), SyncMode.Blockchair())) {
try {
SQLiteDatabase.deleteDatabase(context.getDatabasePath(getDatabaseName(networkType, walletId, syncMode)))
} catch (ex: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ class BitcoinCore(
sealed class SyncMode {
class Full : SyncMode()
class Api : SyncMode()
class Blockchair(val key: String) : SyncMode()
class Blockchair : SyncMode()
}

sealed class StateError : Exception() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ class BitcoinCoreBuilder {
val blockchairApi = if (apiTransactionProvider is BlockchairTransactionProvider) {
apiTransactionProvider.blockchairApi
} else {
BlockchairApi(syncMode.key, network.blockchairChainId)
BlockchairApi(network.blockchairChainId)
}
val lastBlockProvider = BlockchairLastBlockProvider(blockchairApi)
apiSyncer = BlockchairApiSyncer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import java.util.Locale
import java.util.TimeZone

class BlockchairApi(
private val secretKey: String,
private val chainId: String,
) {
private val apiManager = ApiManager("https://api.blockchair.com")
private val apiManager = ApiManager("https://api.blocksdecoded.com/v1/blockchair")
private val limit = 10000
private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())

Expand Down Expand Up @@ -64,7 +63,7 @@ class BlockchairApi(
}

fun lastBlockHeader(): BlockHeaderItem {
val params = "?limit=0&key=$secretKey"
val params = "?limit=0"
val url = "$chainId/stats"
val response = apiManager.doOkHttpGet(url + params).asObject()
val data = response.get("data").asObject()
Expand All @@ -84,7 +83,7 @@ class BlockchairApi(
receivedTransactionItems: List<Transaction> = emptyList()
): Pair<List<AddressItem>, List<Transaction>> {
try {
val params = "?transaction_details=true&limit=$limit,0&offset=${receivedTransactionItems.size}&key=$secretKey"
val params = "?transaction_details=true&limit=$limit,0&offset=${receivedTransactionItems.size}"
val url = "$chainId/dashboards/addresses/${addresses.joinToString(separator = ",")}"
val response = apiManager.doOkHttpGet(url + params).asObject()
val data = response.get("data").asObject()
Expand Down Expand Up @@ -134,7 +133,7 @@ class BlockchairApi(

private fun fetchBlockHashes(heights: List<Int>): Map<Int, String> {
try {
val params = "?limit=0&key=$secretKey"
val params = "?limit=0"
val url = "$chainId/dashboards/blocks/${heights.joinToString(separator = ",")}"
val response = apiManager.doOkHttpGet(url + params).asObject()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class BitcoinKit : AbstractKit {
NetworkType.MainNet -> {
val hsBlockHashFetcher = HsBlockHashFetcher("https://api.blocksdecoded.com/v1/blockchains/bitcoin")
if (syncMode is SyncMode.Blockchair) {
val blockchairApi = BlockchairApi(syncMode.key, network.blockchairChainId)
val blockchairApi = BlockchairApi(network.blockchairChainId)
val blockchairBlockHashFetcher = BlockchairBlockHashFetcher(blockchairApi)
val blockHashFetcher = BlockHashFetcher(hsBlockHashFetcher, blockchairBlockHashFetcher, checkpoint.block.height)
val blockchairProvider = BlockchairTransactionProvider(blockchairApi, blockHashFetcher)
Expand Down Expand Up @@ -367,7 +367,7 @@ class BitcoinKit : AbstractKit {
* @param walletId The string wallet ID of the BitcoinKit.
*/
fun clear(context: Context, networkType: NetworkType, walletId: String) {
for (syncMode in listOf(SyncMode.Api(), SyncMode.Full(), SyncMode.Blockchair(""))) {
for (syncMode in listOf(SyncMode.Api(), SyncMode.Full(), SyncMode.Blockchair())) {
for (purpose in Purpose.values()) try {
SQLiteDatabase.deleteDatabase(context.getDatabasePath(getDatabaseName(networkType, walletId, syncMode, purpose)))
} catch (ex: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class DashKit : AbstractKit, IInstantTransactionDelegate, BitcoinCore.Listener {
val insightApiProvider = InsightApi("https://insight.dash.org/insight-api")

if (syncMode is SyncMode.Blockchair) {
val blockchairApi = BlockchairApi(syncMode.key, network.blockchairChainId)
val blockchairApi = BlockchairApi(network.blockchairChainId)
val blockchairBlockHashFetcher = BlockchairBlockHashFetcher(blockchairApi)
val blockchairProvider = BlockchairTransactionProvider(blockchairApi, blockchairBlockHashFetcher)

Expand Down Expand Up @@ -384,7 +384,7 @@ class DashKit : AbstractKit, IInstantTransactionDelegate, BitcoinCore.Listener {
}

fun clear(context: Context, networkType: NetworkType, walletId: String) {
for (syncMode in listOf(SyncMode.Api(), SyncMode.Full(), SyncMode.Blockchair(""))) {
for (syncMode in listOf(SyncMode.Api(), SyncMode.Full(), SyncMode.Blockchair())) {
try {
SQLiteDatabase.deleteDatabase(context.getDatabasePath(getDatabaseNameCore(networkType, walletId, syncMode)))
SQLiteDatabase.deleteDatabase(context.getDatabasePath(getDatabaseName(networkType, walletId, syncMode)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class ECashKit : AbstractKit {
NetworkType.MainNet -> {
val chronikApiProvider = ChronikApi()
if (syncMode is SyncMode.Blockchair) {
val blockchairApi = BlockchairApi(syncMode.key, network.blockchairChainId)
val blockchairApi = BlockchairApi(network.blockchairChainId)
val blockchairBlockHashFetcher = BlockchairBlockHashFetcher(blockchairApi)
val blockchairProvider = BlockchairTransactionProvider(blockchairApi, blockchairBlockHashFetcher)

Expand Down Expand Up @@ -280,7 +280,7 @@ class ECashKit : AbstractKit {
"ECash-${networkType.name}-$walletId-${syncMode.javaClass.simpleName}"

fun clear(context: Context, networkType: NetworkType, walletId: String) {
for (syncMode in listOf(SyncMode.Api(), SyncMode.Full(), SyncMode.Blockchair(""))) {
for (syncMode in listOf(SyncMode.Api(), SyncMode.Full(), SyncMode.Blockchair())) {
try {
SQLiteDatabase.deleteDatabase(context.getDatabasePath(getDatabaseName(networkType, walletId, syncMode)))
} catch (ex: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class LitecoinKit : AbstractKit {
val bCoinApiProvider = BCoinApi("https://ltc.blocksdecoded.com/api")

if (syncMode is SyncMode.Blockchair) {
val blockchairApi = BlockchairApi(syncMode.key, network.blockchairChainId)
val blockchairApi = BlockchairApi(network.blockchairChainId)
val blockchairBlockHashFetcher = BlockchairBlockHashFetcher(blockchairApi)
val blockchairProvider = BlockchairTransactionProvider(blockchairApi, blockchairBlockHashFetcher)

Expand Down Expand Up @@ -301,7 +301,7 @@ class LitecoinKit : AbstractKit {
"Litecoin-${networkType.name}-$walletId-${syncMode.javaClass.simpleName}-${purpose.name}"

fun clear(context: Context, networkType: NetworkType, walletId: String) {
for (syncMode in listOf(SyncMode.Api(), SyncMode.Full(), SyncMode.Blockchair(""))) {
for (syncMode in listOf(SyncMode.Api(), SyncMode.Full(), SyncMode.Blockchair())) {
for (purpose in Purpose.values())
try {
SQLiteDatabase.deleteDatabase(context.getDatabasePath(getDatabaseName(networkType, walletId, syncMode, purpose)))
Expand Down

0 comments on commit e7d223c

Please sign in to comment.