From f0dd1d7af1703c6917ca798f101749c79e2a2f68 Mon Sep 17 00:00:00 2001 From: kubel Date: Tue, 5 Mar 2024 10:48:59 +0100 Subject: [PATCH] Fetch enable analytics flag from Explorer when not set in the SDK --- .../internal/common/di/AndroidCommonDITags.kt | 3 +- .../internal/common/di/Web3ModalModule.kt | 3 ++ .../json_rpc/domain/JsonRpcInteractor.kt | 2 -- .../common/modal/Web3ModalApiRepository.kt | 6 ++++ .../modal/data/network/Web3ModalService.kt | 6 ++++ .../data/network/model/EnableAnalyticsDTO.kt | 8 ++++++ .../domain/usecase/EnableAnalyticsUseCase.kt | 25 +++++++++++++++++ .../android/pulse/domain/SendEventUseCase.kt | 28 +++++++++++-------- .../walletconnect/web3/modal/client/Modal.kt | 7 +++-- .../web3/modal/client/Web3Modal.kt | 6 ++++ .../web3/modal/di/EngineModule.kt | 1 + .../web3/modal/di/Web3ModalDITags.kt | 2 +- .../web3/modal/engine/Web3ModalEngine.kt | 5 +++- 13 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/data/network/model/EnableAnalyticsDTO.kt create mode 100644 core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/domain/usecase/EnableAnalyticsUseCase.kt diff --git a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/di/AndroidCommonDITags.kt b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/di/AndroidCommonDITags.kt index 0684e17e18..9f95cdffb1 100644 --- a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/di/AndroidCommonDITags.kt +++ b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/di/AndroidCommonDITags.kt @@ -39,5 +39,6 @@ enum class AndroidCommonDITags { DECRYPT_AUTH_MESSAGE, DECRYPT_NOTIFY_MESSAGE, DECRYPT_USE_CASES, - BUNDLE_ID + BUNDLE_ID, + ENABLE_ANALYTICS } \ No newline at end of file diff --git a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/di/Web3ModalModule.kt b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/di/Web3ModalModule.kt index 19a75d6e8c..e72bd5567a 100644 --- a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/di/Web3ModalModule.kt +++ b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/di/Web3ModalModule.kt @@ -3,6 +3,8 @@ package com.walletconnect.android.internal.common.di import com.walletconnect.android.BuildConfig import com.walletconnect.android.internal.common.modal.Web3ModalApiRepository import com.walletconnect.android.internal.common.modal.data.network.Web3ModalService +import com.walletconnect.android.internal.common.modal.domain.usecase.EnableAnalyticsUseCase +import com.walletconnect.android.internal.common.modal.domain.usecase.EnableAnalyticsUseCaseInterface import com.walletconnect.android.internal.common.modal.domain.usecase.GetInstalledWalletsIdsUseCase import com.walletconnect.android.internal.common.modal.domain.usecase.GetInstalledWalletsIdsUseCaseInterface import com.walletconnect.android.internal.common.modal.domain.usecase.GetSampleWalletsUseCase @@ -60,4 +62,5 @@ internal fun web3ModalModule() = module { single { GetInstalledWalletsIdsUseCase(web3ModalApiRepository = get()) } single { GetWalletsUseCase(web3ModalApiRepository = get()) } single { GetSampleWalletsUseCase(context = get()) } + single { EnableAnalyticsUseCase(repository = get()) } } diff --git a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/JsonRpcInteractor.kt b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/JsonRpcInteractor.kt index bc6c674bcd..d4c46bc643 100644 --- a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/JsonRpcInteractor.kt +++ b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/JsonRpcInteractor.kt @@ -94,8 +94,6 @@ internal class JsonRpcInteractor( return onFailure(e) } - println("kobe: Request: $requestJson") - try { if (jsonRpcHistory.setRequest(payload.id, topic, payload.method, requestJson)) { val encryptedRequest = chaChaPolyCodec.encrypt(topic, requestJson, envelopeType, participants) diff --git a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/Web3ModalApiRepository.kt b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/Web3ModalApiRepository.kt index 5d2ba76efe..cb22ee072d 100644 --- a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/Web3ModalApiRepository.kt +++ b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/Web3ModalApiRepository.kt @@ -21,6 +21,12 @@ internal class Web3ModalApiRepository( response.body()!!.data.toWalletsAppData().filter { it.isInstalled } } + suspend fun getAnalyticsConfig(sdkType: String = "w3m") = runCatching { + web3ModalService.getAnalyticsConfig(sdkType = sdkType) + }.mapCatching { response -> + response.body()!!.isAnalyticsEnabled + } + suspend fun getWallets( sdkType: String, page: Int, diff --git a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/data/network/Web3ModalService.kt b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/data/network/Web3ModalService.kt index b12711de5c..4a60589adf 100644 --- a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/data/network/Web3ModalService.kt +++ b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/data/network/Web3ModalService.kt @@ -1,5 +1,6 @@ package com.walletconnect.android.internal.common.modal.data.network +import com.walletconnect.android.internal.common.modal.data.network.model.EnableAnalyticsDTO import com.walletconnect.android.internal.common.modal.data.network.model.GetAndroidDataDTO import com.walletconnect.android.internal.common.modal.data.network.model.GetWalletsDTO import retrofit2.Response @@ -23,4 +24,9 @@ internal interface Web3ModalService { suspend fun getAndroidData( @Header("x-sdk-type") sdkType: String, ): Response + + @GET("getAnalyticsConfig") + suspend fun getAnalyticsConfig( + @Header("x-sdk-type") sdkType: String, + ): Response } diff --git a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/data/network/model/EnableAnalyticsDTO.kt b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/data/network/model/EnableAnalyticsDTO.kt new file mode 100644 index 0000000000..ccb7dfe0b8 --- /dev/null +++ b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/data/network/model/EnableAnalyticsDTO.kt @@ -0,0 +1,8 @@ +package com.walletconnect.android.internal.common.modal.data.network.model + +import com.squareup.moshi.Json + +data class EnableAnalyticsDTO( + @Json(name = "isAnalyticsEnabled") + val isAnalyticsEnabled: Boolean +) diff --git a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/domain/usecase/EnableAnalyticsUseCase.kt b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/domain/usecase/EnableAnalyticsUseCase.kt new file mode 100644 index 0000000000..716caa09b6 --- /dev/null +++ b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/modal/domain/usecase/EnableAnalyticsUseCase.kt @@ -0,0 +1,25 @@ +package com.walletconnect.android.internal.common.modal.domain.usecase + +import com.walletconnect.android.internal.common.modal.Web3ModalApiRepository +import kotlinx.coroutines.runBlocking + +interface EnableAnalyticsUseCaseInterface { + fun fetchAnalyticsConfig(): Boolean +} + +internal class EnableAnalyticsUseCase(private val repository: Web3ModalApiRepository) : EnableAnalyticsUseCaseInterface { + override fun fetchAnalyticsConfig(): Boolean { + return runBlocking { + try { + val response = repository.getAnalyticsConfig() + if (response.isSuccess) { + response.getOrDefault(false) + } else { + false + } + } catch (e: Exception) { + false + } + } + } +} \ No newline at end of file diff --git a/core/android/src/main/kotlin/com/walletconnect/android/pulse/domain/SendEventUseCase.kt b/core/android/src/main/kotlin/com/walletconnect/android/pulse/domain/SendEventUseCase.kt index 39e1b895cf..0931b23457 100644 --- a/core/android/src/main/kotlin/com/walletconnect/android/pulse/domain/SendEventUseCase.kt +++ b/core/android/src/main/kotlin/com/walletconnect/android/pulse/domain/SendEventUseCase.kt @@ -1,30 +1,36 @@ package com.walletconnect.android.pulse.domain +import com.walletconnect.android.internal.common.di.AndroidCommonDITags import com.walletconnect.android.internal.common.scope +import com.walletconnect.android.internal.common.wcKoinApp import com.walletconnect.android.pulse.data.PulseService import com.walletconnect.android.pulse.model.Event import com.walletconnect.foundation.util.Logger import kotlinx.coroutines.launch import kotlinx.coroutines.supervisorScope +import org.koin.core.qualifier.named abstract class SendEventUseCase( private val pulseService: PulseService, private val logger: Logger, internal val bundleId: String ) { + private val enableAnalytics: Boolean by lazy { wcKoinApp.koin.get(named(AndroidCommonDITags.ENABLE_ANALYTICS)) } + operator fun invoke(event: Event) { - scope.launch { - supervisorScope { - try { - logger.log("kobe: Sending event: ${event.props.type}") - val response = pulseService.sendEvent(body = event) - if (!response.isSuccessful) { - logger.error("kobe: Failed to send event: ${event.props.type}") - } else { - logger.log("kobe: Event sent successfully: ${event.props.type}") + if (enableAnalytics) { + scope.launch { + supervisorScope { + try { + val response = pulseService.sendEvent(body = event) + if (!response.isSuccessful) { + logger.error("Failed to send event: ${event.props.type}") + } else { + logger.log("Event sent successfully: ${event.props.type}") + } + } catch (e: Exception) { + logger.error("Failed to send event: ${event.props.type}, error: $e") } - } catch (e: Exception) { - logger.error("kobe: Failed to send event: ${event.props.type}, error: $e") } } } diff --git a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/client/Modal.kt b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/client/Modal.kt index 4cb42ca2eb..a51a8f2663 100644 --- a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/client/Modal.kt +++ b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/client/Modal.kt @@ -18,7 +18,8 @@ object Modal { val core: CoreInterface, val excludedWalletIds: List = listOf(), val recommendedWalletsIds: List = listOf(), - val coinbaseEnabled: Boolean = true + val coinbaseEnabled: Boolean = true, + val enableAnalytics: Boolean? = true ) : Params() data class Connect( @@ -78,13 +79,13 @@ object Modal { val metaData: Core.Model.AppMetaData?, val namespaces: Map, val accounts: List, - ): ApprovedSession() + ) : ApprovedSession() data class CoinbaseSession( val chain: String, val networkId: String, val address: String - ): ApprovedSession() + ) : ApprovedSession() } data class RejectedSession(val topic: String, val reason: String) : Model() diff --git a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/client/Web3Modal.kt b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/client/Web3Modal.kt index 09b352a148..fd9ba8674d 100644 --- a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/client/Web3Modal.kt +++ b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/client/Web3Modal.kt @@ -1,6 +1,7 @@ package com.walletconnect.web3.modal.client import androidx.activity.ComponentActivity +import com.walletconnect.android.internal.common.di.AndroidCommonDITags import com.walletconnect.android.internal.common.scope import com.walletconnect.android.internal.common.wcKoinApp import com.walletconnect.sign.client.Sign @@ -20,6 +21,8 @@ import com.walletconnect.web3.modal.engine.Web3ModalEngine import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import org.jetbrains.annotations.ApiStatus.Experimental +import org.koin.core.qualifier.named +import org.koin.dsl.module object Web3Modal { @@ -106,6 +109,9 @@ object Web3Modal { web3ModalEngine = wcKoinApp.koin.get() web3ModalEngine.setup(init, onError) web3ModalEngine.setInternalDelegate(Web3ModalDelegate) + wcKoinApp.modules( + module { single(named(AndroidCommonDITags.ENABLE_ANALYTICS)) { init.enableAnalytics ?: web3ModalEngine.fetchAnalyticsConfig() } } + ) } .onFailure { error -> return@onInitializedClient onError(Modal.Model.Error(error)) } .onSuccess { diff --git a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/di/EngineModule.kt b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/di/EngineModule.kt index c0f69d11c6..f45a0e6b29 100644 --- a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/di/EngineModule.kt +++ b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/di/EngineModule.kt @@ -27,6 +27,7 @@ internal fun engineModule() = module { sendConnectErrorUseCase = get(), sendConnectSuccessUseCase = get(), connectionEventRepository = get(), + enableAnalyticsUseCase = get(), logger = get(named(AndroidCommonDITags.LOGGER)), ) } diff --git a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/di/Web3ModalDITags.kt b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/di/Web3ModalDITags.kt index 4a56b7b935..24db155f53 100644 --- a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/di/Web3ModalDITags.kt +++ b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/di/Web3ModalDITags.kt @@ -4,5 +4,5 @@ internal enum class Web3ModalDITags { BALANCE_RPC_RETROFIT, BLOCKCHAIN_RETROFIT, MOSHI, - SESSION_DATA_STORE + SESSION_DATA_STORE, } \ No newline at end of file diff --git a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt index 8198ee5435..b770a831bd 100644 --- a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt +++ b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt @@ -5,6 +5,7 @@ import android.content.Intent import android.net.Uri import androidx.activity.ComponentActivity import androidx.activity.result.contract.ActivityResultContracts +import com.walletconnect.android.internal.common.modal.domain.usecase.EnableAnalyticsUseCaseInterface import com.walletconnect.android.internal.common.scope import com.walletconnect.android.internal.common.wcKoinApp import com.walletconnect.android.pulse.domain.SendConnectErrorUseCase @@ -55,8 +56,10 @@ internal class Web3ModalEngine( private val sendConnectErrorUseCase: SendConnectErrorUseCase, private val sendConnectSuccessUseCase: SendConnectSuccessUseCase, private val connectionEventRepository: ConnectionEventRepository, + private val enableAnalyticsUseCase: EnableAnalyticsUseCaseInterface, private val logger: Logger -) : SendModalLoadedUseCaseInterface by sendModalLoadedUseCase { +) : SendModalLoadedUseCaseInterface by sendModalLoadedUseCase, + EnableAnalyticsUseCaseInterface by enableAnalyticsUseCase { internal var excludedWalletsIds: MutableList = mutableListOf() internal var recommendedWalletsIds: MutableList = mutableListOf()