Skip to content

Commit

Permalink
5.0.18
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Dec 9, 2024
1 parent dadf203 commit 3cba653
Show file tree
Hide file tree
Showing 40 changed files with 329 additions and 185 deletions.
24 changes: 16 additions & 8 deletions apps/wallet/api/src/main/java/com/tonapps/wallet/api/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -511,17 +511,19 @@ class API(
fun emulateWithBattery(
tonProofToken: String,
cell: Cell,
testnet: Boolean
) = emulateWithBattery(tonProofToken, cell.base64(), testnet)
testnet: Boolean,
safeModeEnabled: Boolean,
) = emulateWithBattery(tonProofToken, cell.base64(), testnet, safeModeEnabled)

fun emulateWithBattery(
tonProofToken: String,
boc: String,
testnet: Boolean
testnet: Boolean,
safeModeEnabled: Boolean,
): Pair<MessageConsequences, Boolean>? {
val host = if (testnet) config.batteryTestnetHost else config.batteryHost
val url = "$host/wallet/emulate"
val data = "{\"boc\":\"$boc\"}"
val data = "{\"boc\":\"$boc\",\"safe_mode\":$safeModeEnabled}"

val response = withRetry {
tonAPIHttpClient.postJSON(url, data, ArrayMap<String, String>().apply {
Expand All @@ -542,13 +544,18 @@ class API(
boc: String,
testnet: Boolean,
address: String? = null,
balance: Long? = null
balance: Long? = null,
safeModeEnabled: Boolean,
): MessageConsequences? = withContext(Dispatchers.IO) {
val params = mutableListOf<EmulateMessageToWalletRequestParamsInner>()
if (address != null) {
params.add(EmulateMessageToWalletRequestParamsInner(address, balance))
}
val request = EmulateMessageToWalletRequest(boc, params)
val request = EmulateMessageToWalletRequest(
boc = boc,
params = params,
safeMode = safeModeEnabled
)
withRetry {
emulation(testnet).emulateMessageToWallet(request)
}
Expand All @@ -558,9 +565,10 @@ class API(
cell: Cell,
testnet: Boolean,
address: String? = null,
balance: Long? = null
balance: Long? = null,
safeModeEnabled: Boolean,
): MessageConsequences? {
return emulate(cell.hex(), testnet, address, balance)
return emulate(cell.hex(), testnet, address, balance, safeModeEnabled)
}

suspend fun sendToBlockchainWithBattery(
Expand Down
66 changes: 43 additions & 23 deletions apps/wallet/api/src/main/java/com/tonapps/wallet/api/CoreAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.tonapps.network.interceptor.AcceptLanguageInterceptor
import com.tonapps.network.interceptor.AuthorizationInterceptor
import com.tonapps.wallet.api.cronet.CronetInterceptor
import com.tonapps.wallet.api.entity.ConfigEntity
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import org.chromium.net.CronetEngine
import java.util.concurrent.TimeUnit
Expand All @@ -24,15 +25,19 @@ abstract class CoreAPI(private val context: Context) {
private var cronetEngine: CronetEngine? = null

val defaultHttpClient = baseOkHttpClientBuilder(
userAgent = userAgent,
cronetEngine = { cronetEngine },
timeoutSeconds = 15
timeoutSeconds = 15,
interceptors = listOf(
UserAgentInterceptor(userAgent),
)
).build()

val seeHttpClient = baseOkHttpClientBuilder(
userAgent = userAgent,
cronetEngine = { null },
timeoutSeconds = 120
timeoutSeconds = 120,
interceptors = listOf(
UserAgentInterceptor(userAgent),
)
).build()

init {
Expand All @@ -54,10 +59,20 @@ abstract class CoreAPI(private val context: Context) {
}

private companion object {

class UserAgentInterceptor(private val userAgent: String) : Interceptor {
override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
val request = chain.request().newBuilder()
.addHeader("User-Agent", userAgent)
.build()
return chain.proceed(request)
}
}

private fun baseOkHttpClientBuilder(
userAgent: String,
cronetEngine: () -> CronetEngine?,
timeoutSeconds: Long = 5
timeoutSeconds: Long = 5,
interceptors: List<Interceptor> = emptyList()
): OkHttpClient.Builder {
val builder = OkHttpClient().newBuilder()
.retryOnConnectionFailure(true)
Expand All @@ -67,36 +82,41 @@ abstract class CoreAPI(private val context: Context) {
.callTimeout(timeoutSeconds, TimeUnit.SECONDS)
.pingInterval(timeoutSeconds, TimeUnit.SECONDS)
.followSslRedirects(true)
.addInterceptor { chain ->
val request = chain.request().newBuilder()
.addHeader("User-Agent", userAgent)
.build()
chain.proceed(request)
}
.followRedirects(true)
.addInterceptor { chain ->
cronetEngine()?.let { engine ->
CronetInterceptor.newBuilder(engine).build()
}?.intercept(chain) ?: chain.proceed(chain.request())
}

for (interceptor in interceptors) {
builder.addInterceptor(interceptor)
}

builder.addInterceptor { chain ->
cronetEngine()?.let { engine ->
CronetInterceptor.newBuilder(engine).build()
}?.intercept(chain) ?: chain.proceed(chain.request())
}

return builder
}

private fun createTonAPIHttpClient(
context: Context,
userAgent: String,
context: Context,
cronetEngine: () -> CronetEngine?,
tonApiV2Key: () -> String,
allowDomains: () -> List<String>
): OkHttpClient {
return baseOkHttpClientBuilder(userAgent, cronetEngine)
.addInterceptor(AcceptLanguageInterceptor(context.locale))
.addInterceptor(
AuthorizationInterceptor.bearer(
val interceptors = listOf(
UserAgentInterceptor(userAgent),
AcceptLanguageInterceptor(context.locale),
AuthorizationInterceptor.bearer(
token = tonApiV2Key,
allowDomains = allowDomains
)).build()
)
)

return baseOkHttpClientBuilder(
cronetEngine = cronetEngine,
interceptors = interceptors
).build()
}

private fun requestCronet(context: Context, userAgent: String, callback: (CronetEngine) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ inline fun <reified T> fromJSON(json: String): T {
}

fun <R> withRetry(
times: Int = 3,
delay: Long = 100,
times: Int = 5,
delay: Long = 500,
retryBlock: () -> R
): R? {
var index = -1
Expand All @@ -50,7 +50,7 @@ fun <R> withRetry(
} catch (e: Throwable) {
val statusCode = e.getHttpStatusCode()
if (statusCode == 429 || statusCode == 401 || statusCode == 502 || statusCode == 520) {
SystemClock.sleep(delay)
SystemClock.sleep(delay + 100)
continue
}
if (statusCode >= 500 || statusCode == 404 || statusCode == 400) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ class BatteryRepository(
publicKey: PublicKeyEd25519,
testnet: Boolean,
boc: Cell,
forceRelayer: Boolean = false
forceRelayer: Boolean = false,
safeModeEnabled: Boolean,
): Pair<MessageConsequences, Boolean>? = withContext(Dispatchers.IO) {

val balance = getBalance(
Expand All @@ -106,7 +107,7 @@ class BatteryRepository(
throw IllegalStateException("Zero balance")
}

api.emulateWithBattery(tonProofToken, boc, testnet)
api.emulateWithBattery(tonProofToken, boc, testnet, safeModeEnabled)
}

suspend fun getAppliedPromo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ object AnalyticsHelper {
)
}

@UiThread
fun firstLaunch(installId: String) {
Aptabase.instance.trackEvent("first_launch", hashMapOf(
"firebase_user_id" to installId
))
}

@UiThread
fun onRampOpen(installId: String, source: String) {
Aptabase.instance.trackEvent("onramp_open", hashMapOf(
Expand Down Expand Up @@ -47,7 +54,6 @@ object AnalyticsHelper {
))
}


@UiThread
fun trackBrowserOpen(installId: String, from: String) {
Aptabase.instance.trackEvent("browser_open", hashMapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ object DevSettings {
}
}

var firstLaunchDate: Long = prefs.getLong("first_launch_date", 0)
set(value) {
if (field != value) {
field = value
prefs.edit().putLong("first_launch_date", value).apply()
}
}

var tonConnectLogs: Boolean = prefs.getBoolean("ton_connect_logs", false)
set(value) {
if (field != value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tonapps.tonkeeper.core.entities

import android.net.Uri
import android.os.Parcelable
import android.util.Log
import com.tonapps.wallet.api.entity.ConfigEntity
import com.tonapps.wallet.data.account.entities.WalletEntity
import com.tonapps.wallet.data.purchase.entity.PurchaseMethodEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ class AssetsManager(
sorted: Boolean = false,
) = cache.get(wallet, currency, sorted)

suspend fun getRemoteTotalBalance(
suspend fun requestTotalBalance(
wallet: WalletEntity,
currency: WalletCurrency,
refresh: Boolean = false,
sorted: Boolean = false,
): Coins? {
val totalBalance = calculateTotalBalance(wallet, currency, true, sorted) ?: return null
val totalBalance = calculateTotalBalance(wallet, currency, refresh, sorted) ?: return null
cache.set(wallet, currency, sorted, totalBalance)
return totalBalance
}
Expand All @@ -131,7 +132,7 @@ class AssetsManager(
wallet: WalletEntity,
currency: WalletCurrency,
sorted: Boolean = false
) = getCachedTotalBalance(wallet, currency, sorted) ?: getRemoteTotalBalance(wallet, currency, sorted)
) = getCachedTotalBalance(wallet, currency, sorted) ?: requestTotalBalance(wallet, currency, sorted)

private suspend fun calculateTotalBalance(
wallet: WalletEntity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@ data class DAppArgs(
val title: String? = null,
val url: Uri,
val source: String,
val sendAnalytics: Boolean,
): BaseArgs() {

private companion object {
private const val ARG_TITLE = "title"
private const val ARG_URL = "url"
private const val ARG_SOURCE = "source"
private const val ARG_SEND_ANALYTICS = "send_analytics"

}

constructor(bundle: Bundle) : this(
title = bundle.getString(ARG_TITLE),
url = bundle.getParcelableCompat(ARG_URL)!!,
source = bundle.getString(ARG_SOURCE) ?: ""
source = bundle.getString(ARG_SOURCE) ?: "",
sendAnalytics = bundle.getBoolean(ARG_SEND_ANALYTICS, true)
)

override fun toBundle(): Bundle = Bundle().apply {
putString(ARG_TITLE, title)
putParcelable(ARG_URL, url)
putString(ARG_SOURCE, source)
putBoolean(ARG_SEND_ANALYTICS, sendAnalytics)
}
}
Loading

0 comments on commit 3cba653

Please sign in to comment.