-
Notifications
You must be signed in to change notification settings - Fork 2
/
NetworkModule.kt
188 lines (181 loc) · 6.25 KB
/
NetworkModule.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.easy.wallet.network.di
import com.easy.wallet.network.HostConstant
import com.easy.wallet.network.httpClient
import com.easy.wallet.network.key.BuildKonfig
import com.easy.wallet.network.source.blockchair.BlockchairApi
import com.easy.wallet.network.source.blockchair.BlockchairApiController
import com.easy.wallet.network.source.coingecko.CoinGeckoApi
import com.easy.wallet.network.source.coingecko.CoinGeckoApiController
import com.easy.wallet.network.source.etherscan.EtherscanApi
import com.easy.wallet.network.source.etherscan.EtherscanApiController
import com.easy.wallet.network.source.evmrpc.EvmJsonRpcApiImpl
import com.easy.wallet.network.source.evmrpc.JsonRpcApi
import com.easy.wallet.network.source.evmrpc.parameter.Parameter
import com.easy.wallet.network.source.okx.OKXWebSocketManager
import com.easy.wallet.network.source.opensea.OpenseaApi
import com.easy.wallet.network.source.opensea.OpenseaApiController
import io.ktor.client.HttpClient
import io.ktor.client.HttpClientConfig
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.defaultRequest
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.Logging
import io.ktor.client.plugins.websocket.WebSockets
import io.ktor.client.request.header
import io.ktor.http.URLProtocol
import io.ktor.http.path
import io.ktor.serialization.kotlinx.KotlinxWebsocketSerializationConverter
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import org.koin.core.qualifier.named
import org.koin.dsl.module
import co.touchlab.kermit.Logger as KLogger
enum class SourceQualifier {
OPENSEA,
BLOCK_CHAIR,
ETHER_SCAN,
OKX_WEBSOCKETS,
COINGECKO,
EVM_RPC
}
private fun httpClientWithDefault(serializersModule: SerializersModule? = null, special: HttpClientConfig<*>.() -> Unit = {}): HttpClient =
httpClient {
install(ContentNegotiation) {
json(
Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
allowStructuredMapKeys = true
serializersModule?.let {
this.serializersModule = it
}
}
)
}
install(Logging) {
logger = object : Logger {
override fun log(message: String) {
KLogger.i("HttpClient") { message }
}
}
level = LogLevel.BODY
}
special()
}
private fun webSocketClient(config: HttpClientConfig<*>.() -> Unit = {}): HttpClient = httpClient {
install(WebSockets) {
contentConverter = KotlinxWebsocketSerializationConverter(
Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
}
)
}
install(Logging) {
logger = object : Logger {
override fun log(message: String) {
KLogger.i("HttpClient") { message }
}
}
level = LogLevel.BODY
}
config()
}
val networkModule = module {
single(qualifier = named(name = SourceQualifier.BLOCK_CHAIR.name)) {
httpClientWithDefault {
defaultRequest {
url {
protocol = URLProtocol.HTTPS
host = HostConstant.BLOCK_CHAIR
path("/")
}
header("Content-Type", "application/json")
}
}
}
single(qualifier = named(name = SourceQualifier.OPENSEA.name)) {
httpClientWithDefault {
defaultRequest {
url {
protocol = URLProtocol.HTTPS
host = "api.opensea.io"
path("v2/")
}
header("Content-Type", "application/json")
// TODO move to backend, delegate forward
header("X-API-KEY", BuildKonfig.OPENSEA_KEY)
}
}
}
single(qualifier = named(name = SourceQualifier.ETHER_SCAN.name)) {
httpClientWithDefault {
defaultRequest {
url {
protocol = URLProtocol.HTTPS
host = "api.etherscan.io"
// host = "api-sepolia.etherscan.io"
path("api/")
parameters.append("apikey", BuildKonfig.ETHERSCAN_KEY)
}
header("Content-Type", "application/json")
}
}
}
single(qualifier = named(name = SourceQualifier.EVM_RPC.name)) {
httpClientWithDefault(
serializersModule = SerializersModule {
polymorphic(List::class) {
ListSerializer(Parameter.serializer())
}
polymorphic(Parameter::class) {
subclass(Parameter.CallParameter::class, Parameter.CallParameter.serializer())
subclass(
Parameter.StringParameter::class,
Parameter.StringParameter.serializer()
)
subclass(
Parameter.IntListParameter::class,
Parameter.IntListParameter.serializer()
)
}
}
) {
defaultRequest {
/* url {
protocol = URLProtocol.HTTPS
host = "mainnet.infura.io"
// host = "sepolia.infura.io"
path("v3/${BuildKonfig.INFURA_KEY}")
}*/
header("Content-Type", "application/json")
}
}
}
single(qualifier = named(name = SourceQualifier.COINGECKO.name)) {
httpClientWithDefault {
defaultRequest {
url {
protocol = URLProtocol.HTTPS
host = "api.coingecko.com"
path("api/v3/")
parameters.append("x_cg_demo_api_key", BuildKonfig.COINGECKO_KEY)
}
header("Content-Type", "application/json")
}
}
}
single(qualifier = named(name = SourceQualifier.OKX_WEBSOCKETS.name)) { webSocketClient() }
single<BlockchairApi> { BlockchairApiController(get(qualifier = named(SourceQualifier.BLOCK_CHAIR.name))) }
single<OpenseaApi> { OpenseaApiController(get(qualifier = named(SourceQualifier.OPENSEA.name))) }
single<EtherscanApi> { EtherscanApiController(get(qualifier = named(SourceQualifier.ETHER_SCAN.name))) }
single<CoinGeckoApi> { CoinGeckoApiController(get(qualifier = named(SourceQualifier.COINGECKO.name))) }
single<JsonRpcApi> { EvmJsonRpcApiImpl(get(qualifier = named(SourceQualifier.EVM_RPC.name))) }
single { OKXWebSocketManager(get(qualifier = named(SourceQualifier.OKX_WEBSOCKETS.name))) }
}