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

Replace URL with URI for Rpc sources #295

Merged
merged 1 commit into from
Sep 27, 2023
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 @@ -16,12 +16,11 @@ import retrofit2.http.Headers
import retrofit2.http.POST
import retrofit2.http.Url
import java.net.URI
import java.net.URL
import java.util.concurrent.atomic.AtomicInteger
import java.util.logging.Logger

class NodeApiProvider(
private val urls: List<URL>,
private val uris: List<URI>,
private val gson: Gson,
auth: String? = null
) : IRpcApiProvider {
Expand All @@ -47,7 +46,7 @@ class NodeApiProvider(
.addInterceptor(headersInterceptor)

val retrofit = Retrofit.Builder()
.baseUrl("${urls.first()}/")
.baseUrl("${uris.first()}/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
Expand All @@ -57,17 +56,17 @@ class NodeApiProvider(
service = retrofit.create(InfuraService::class.java)
}

override val source: String = urls.first().host
override val source: String = uris.first().host

override fun <T> single(rpc: JsonRpc<T>): Single<T> {
rpc.id = currentRpcId.addAndGet(1)

return Single.create { emitter ->
var error: Throwable = ApiProviderError.ApiUrlNotFound

for (url in urls) {
for (uri in uris) {
try {
val rpcResponse = service.single(url.toURI(), gson.toJson(rpc)).blockingGet()
val rpcResponse = service.single(uri, gson.toJson(rpc)).blockingGet()
val response = rpc.parseResponse(rpcResponse, gson)

emitter.onSuccess(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import okhttp3.Credentials
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.net.URL
import java.net.URI
import java.util.logging.Logger

class NodeWebSocket(
url: URL,
private val gson: Gson,
auth: String? = null
uri: URI,
private val gson: Gson,
auth: String? = null
) : IRpcWebSocket {
private val logger = Logger.getLogger(this.javaClass.simpleName)
private var disposables = CompositeDisposable()
Expand Down Expand Up @@ -68,7 +68,7 @@ class NodeWebSocket(
.build()

scarlet = Scarlet.Builder()
.webSocketFactory(okHttpClient.newWebSocketFactory(url.toString()))
.webSocketFactory(okHttpClient.newWebSocketFactory(uri.toString()))
.addMessageAdapterFactory(GsonMessageAdapter.Factory(gson))
.addStreamAdapterFactory(RxJava2StreamAdapterFactory())
.backoffStrategy(backoffStrategy)
Expand All @@ -78,7 +78,7 @@ class NodeWebSocket(
//region IRpcWebSocket
override var listener: IRpcWebSocketListener? = null

override val source: String = url.host
override val source: String = uri.host

override fun start() {
state = WebSocketState.Connecting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Eip1155Provider(
companion object {

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

return Eip1155Provider(apiProvider)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,15 @@ class EthereumKit(

val syncer: IRpcSyncer = when (rpcSource) {
is RpcSource.WebSocket -> {
val rpcWebSocket = NodeWebSocket(rpcSource.url, gson, rpcSource.auth)
val rpcWebSocket = NodeWebSocket(rpcSource.uri, gson, rpcSource.auth)
val webSocketRpcSyncer = WebSocketRpcSyncer(rpcWebSocket, gson)

rpcWebSocket.listener = webSocketRpcSyncer

webSocketRpcSyncer
}
is RpcSource.Http -> {
val apiProvider = NodeApiProvider(rpcSource.urls, gson, rpcSource.auth)
val apiProvider = NodeApiProvider(rpcSource.uris, gson, rpcSource.auth)
ApiRpcSyncer(apiProvider, connectionManager, chain.syncInterval)
}
}
Expand Down Expand Up @@ -441,7 +441,7 @@ class EthereumKit(
}

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

private fun transactionProvider(transactionSource: TransactionSource, address: Address): ITransactionProvider {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package io.horizontalsystems.ethereumkit.models

import java.net.URL
import java.net.URI

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

companion object {
private fun infuraHttp(subdomain: String, projectId: String, projectSecret: String? = null): Http {
return Http(listOf(URL("https://$subdomain.infura.io/v3/$projectId")), projectSecret)
return Http(listOf(URI("https://$subdomain.infura.io/v3/$projectId")), projectSecret)
}

private fun infuraWebSocket(subdomain: String, projectId: String, projectSecret: String? = null): WebSocket {
return WebSocket(URL("https://$subdomain.infura.io/ws/v3/$projectId"), projectSecret)
return WebSocket(URI("https://$subdomain.infura.io/ws/v3/$projectId"), projectSecret)
}

fun ethereumInfuraHttp(projectId: String, projectSecret: String? = null): Http {
Expand All @@ -32,52 +32,52 @@ sealed class RpcSource() {
}

fun bscRpcHttp(): Http {
return Http(listOf(URL("https://bscrpc.com")), null)
return Http(listOf(URI("https://bscrpc.com")), null)
}

fun binanceSmartChainHttp(): Http {
return Http(
listOf(
URL("https://bsc-dataseed.binance.org/"),
URL("https://bsc-dataseed1.defibit.io/"),
URL("https://bsc-dataseed1.ninicoin.io/"),
URL("https://bsc-dataseed2.defibit.io/"),
URL("https://bsc-dataseed3.defibit.io/"),
URL("https://bsc-dataseed4.defibit.io/"),
URL("https://bsc-dataseed2.ninicoin.io/"),
URL("https://bsc-dataseed3.ninicoin.io/"),
URL("https://bsc-dataseed4.ninicoin.io/"),
URL("https://bsc-dataseed1.binance.org/"),
URL("https://bsc-dataseed2.binance.org/"),
URL("https://bsc-dataseed3.binance.org/"),
URL("https://bsc-dataseed4.binance.org/")
URI("https://bsc-dataseed.binance.org/"),
URI("https://bsc-dataseed1.defibit.io/"),
URI("https://bsc-dataseed1.ninicoin.io/"),
URI("https://bsc-dataseed2.defibit.io/"),
URI("https://bsc-dataseed3.defibit.io/"),
URI("https://bsc-dataseed4.defibit.io/"),
URI("https://bsc-dataseed2.ninicoin.io/"),
URI("https://bsc-dataseed3.ninicoin.io/"),
URI("https://bsc-dataseed4.ninicoin.io/"),
URI("https://bsc-dataseed1.binance.org/"),
URI("https://bsc-dataseed2.binance.org/"),
URI("https://bsc-dataseed3.binance.org/"),
URI("https://bsc-dataseed4.binance.org/")
),
null
)
}

fun polygonRpcHttp(): Http {
return Http(listOf(URL("https://polygon-rpc.com")), null)
return Http(listOf(URI("https://polygon-rpc.com")), null)
}

fun optimismRpcHttp(): Http {
return Http(listOf(URL("https://mainnet.optimism.io")), null)
return Http(listOf(URI("https://mainnet.optimism.io")), null)
}

fun arbitrumOneRpcHttp(): Http {
return Http(listOf(URL("https://arb1.arbitrum.io/rpc")), null)
return Http(listOf(URI("https://arb1.arbitrum.io/rpc")), null)
}

fun avaxNetworkHttp(): Http {
return Http(listOf(URL("https://api.avax.network/ext/bc/C/rpc")), null)
return Http(listOf(URI("https://api.avax.network/ext/bc/C/rpc")), null)
}

fun gnosisRpcHttp(): Http {
return Http(listOf(URL("https://rpc.gnosischain.com")), null)
return Http(listOf(URI("https://rpc.gnosischain.com")), null)
}

fun fantomRpcHttp(): Http {
return Http(listOf(URL("https://rpc.fantom.network")), null)
return Http(listOf(URI("https://rpc.fantom.network")), null)
}

}
Expand Down
Loading