Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RpcApiProviderFactory singleton #302

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.horizontalsystems.erc20kit.events.TokenInfo
import io.horizontalsystems.ethereumkit.api.core.IRpcApiProvider
import io.horizontalsystems.ethereumkit.api.core.RpcBlockchain
import io.horizontalsystems.ethereumkit.contracts.ContractMethodHelper
import io.horizontalsystems.ethereumkit.core.EthereumKit
import io.horizontalsystems.ethereumkit.core.RpcApiProviderFactory
import io.horizontalsystems.ethereumkit.models.Address
import io.horizontalsystems.ethereumkit.models.DefaultBlockParameter
import io.horizontalsystems.ethereumkit.models.RpcSource
Expand Down Expand Up @@ -87,7 +87,7 @@ class Eip20Provider(private val provider: IRpcApiProvider) {
companion object {

fun instance(rpcSource: RpcSource.Http): Eip20Provider {
return Eip20Provider(EthereumKit.getNodeApiProvider(rpcSource))
return Eip20Provider(RpcApiProviderFactory.nodeApiProvider(rpcSource))
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import io.horizontalsystems.ethereumkit.core.EthereumKit.SyncState
import io.horizontalsystems.ethereumkit.core.IApiStorage
import io.horizontalsystems.ethereumkit.core.IBlockchain
import io.horizontalsystems.ethereumkit.core.IBlockchainListener
import io.horizontalsystems.ethereumkit.core.RpcApiProviderFactory
import io.horizontalsystems.ethereumkit.core.TransactionBuilder
import io.horizontalsystems.ethereumkit.models.Address
import io.horizontalsystems.ethereumkit.models.DefaultBlockParameter
Expand Down Expand Up @@ -280,13 +281,7 @@ class RpcBlockchain(
gasPrice: GasPrice,
data: ByteArray?
): Single<Long> {
val rpcApiProvider: IRpcApiProvider = when (rpcSource) {
is RpcSource.Http -> {
NodeApiProvider(rpcSource.uris, EthereumKit.gson, rpcSource.auth)
}

is RpcSource.WebSocket -> throw IllegalStateException("Websocket not supported")
}
val rpcApiProvider = RpcApiProviderFactory.nodeApiProvider(rpcSource)

return rpcApiProvider.single(EstimateGasJsonRpc(from, to, amount, gasLimit, gasPrice, data))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.horizontalsystems.ethereumkit.core

import io.horizontalsystems.ethereumkit.api.core.IRpcApiProvider
import io.horizontalsystems.ethereumkit.api.core.NodeApiProvider
import io.horizontalsystems.ethereumkit.api.core.RpcBlockchain
import io.horizontalsystems.ethereumkit.contracts.ContractMethod
import io.horizontalsystems.ethereumkit.models.Address
Expand Down Expand Up @@ -31,7 +30,7 @@ class Eip1155Provider(
companion object {

fun instance(rpcSource: RpcSource.Http): Eip1155Provider {
val apiProvider = NodeApiProvider(rpcSource.uris, EthereumKit.gson, rpcSource.auth)
val apiProvider = RpcApiProviderFactory.nodeApiProvider(rpcSource)

return Eip1155Provider(apiProvider)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import android.content.Context
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import io.horizontalsystems.ethereumkit.api.core.ApiRpcSyncer
import io.horizontalsystems.ethereumkit.api.core.IRpcApiProvider
import io.horizontalsystems.ethereumkit.api.core.IRpcSyncer
import io.horizontalsystems.ethereumkit.api.core.NodeApiProvider
import io.horizontalsystems.ethereumkit.api.core.NodeWebSocket
import io.horizontalsystems.ethereumkit.api.core.RpcBlockchain
import io.horizontalsystems.ethereumkit.api.core.WebSocketRpcSyncer
Expand Down Expand Up @@ -387,13 +385,7 @@ class EthereumKit(
data: ByteArray,
defaultBlockParameter: DefaultBlockParameter = DefaultBlockParameter.Latest
): Single<ByteArray> {
val rpcApiProvider: IRpcApiProvider = when (rpcSource) {
is RpcSource.Http -> {
NodeApiProvider(rpcSource.uris, gson, rpcSource.auth)
}

is RpcSource.WebSocket -> throw IllegalStateException("Websocket not supported")
}
val rpcApiProvider = RpcApiProviderFactory.nodeApiProvider(rpcSource)
val rpc = RpcBlockchain.callRpc(contractAddress, data, defaultBlockParameter)
return rpcApiProvider.single(rpc)
}
Expand Down Expand Up @@ -462,7 +454,7 @@ class EthereumKit(
}

is RpcSource.Http -> {
val apiProvider = NodeApiProvider(rpcSource.uris, gson, rpcSource.auth)
val apiProvider = RpcApiProviderFactory.nodeApiProvider(rpcSource)
ApiRpcSyncer(apiProvider, connectionManager, chain.syncInterval)
}
}
Expand Down Expand Up @@ -516,10 +508,6 @@ class EthereumKit(
EthereumDatabaseManager.clear(context, chain, walletId)
}

fun getNodeApiProvider(rpcSource: RpcSource.Http): NodeApiProvider {
return NodeApiProvider(rpcSource.uris, gson, rpcSource.auth)
}

private fun transactionProvider(transactionSource: TransactionSource, address: Address): ITransactionProvider {
when (transactionSource.type) {
is TransactionSource.SourceType.Etherscan -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.horizontalsystems.ethereumkit.core

import io.horizontalsystems.ethereumkit.api.core.IRpcApiProvider
import io.horizontalsystems.ethereumkit.api.core.NodeApiProvider
import io.horizontalsystems.ethereumkit.models.RpcSource

object RpcApiProviderFactory {

private val providersCache = mutableMapOf<RpcSource, IRpcApiProvider>()

@Synchronized
fun nodeApiProvider(rpcSource: RpcSource) = when (val cachedProvider = providersCache[rpcSource]) {
null -> {
val rpcApiProvider: IRpcApiProvider = when (rpcSource) {
is RpcSource.Http -> {
NodeApiProvider(rpcSource.uris, EthereumKit.gson, rpcSource.auth)
}

is RpcSource.WebSocket -> throw IllegalStateException("Websocket not supported")
}

providersCache[rpcSource] = rpcApiProvider

rpcApiProvider
}

else -> {
cachedProvider
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package io.horizontalsystems.ethereumkit.models

import java.net.URI

sealed class RpcSource() {
class Http(val uris: List<URI>, val auth: String?) : RpcSource()
class WebSocket(val uri: URI, val auth: String?) : RpcSource()
sealed class RpcSource {
data class Http(val uris: List<URI>, val auth: String?) : RpcSource()
data class WebSocket(val uri: URI, val auth: String?) : RpcSource()

companion object {
private fun infuraHttp(subdomain: String, projectId: String, projectSecret: String? = null): Http {
Expand Down
Loading