Skip to content

Commit

Permalink
staking
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Sep 25, 2024
1 parent f56481a commit a9388c9
Show file tree
Hide file tree
Showing 61 changed files with 853 additions and 496 deletions.
55 changes: 55 additions & 0 deletions apps/wallet/api/src/main/java/com/tonapps/wallet/api/API.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.tonapps.wallet.api

import android.content.Context
import android.net.Uri
import android.util.ArrayMap
import android.util.Log
import androidx.core.graphics.drawable.toIcon
import com.squareup.moshi.JsonAdapter
import com.tonapps.blockchain.ton.extensions.EmptyPrivateKeyEd25519
import com.tonapps.blockchain.ton.extensions.base64
import com.tonapps.blockchain.ton.extensions.equalsAddress
import com.tonapps.blockchain.ton.extensions.isValidTonAddress
import com.tonapps.blockchain.ton.extensions.toAccountId
import com.tonapps.extensions.locale
Expand All @@ -18,6 +21,7 @@ import com.tonapps.network.interceptor.AcceptLanguageInterceptor
import com.tonapps.network.interceptor.AuthorizationInterceptor
import com.tonapps.network.post
import com.tonapps.network.postJSON
import com.tonapps.network.simple
import com.tonapps.network.sse
import com.tonapps.wallet.api.core.SourceAPI
import com.tonapps.wallet.api.entity.AccountDetailsEntity
Expand Down Expand Up @@ -59,6 +63,7 @@ import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import org.json.JSONArray
import org.json.JSONObject
import org.ton.api.pub.PublicKeyEd25519
Expand Down Expand Up @@ -104,6 +109,56 @@ class API(
val configFlow: Flow<ConfigEntity>
get() = configRepository.stream


private fun tonapiRawUrl(testnet: Boolean, method: String): String {
val host = if (!testnet) config.tonapiMainnetHost else config.tonapiTestnetHost
return "$host/v2/$method"
}

private fun tonapiRawResponse(response: Response): String {
return if (response.isSuccessful) {
response.body?.string() ?: ""
} else if (response.code > 0) {
"{\"error\": \"server response ${response.code}\"}"
} else {
"{\"error\": \"client error\"}"
}
}

suspend fun tonapiPostRaw(
testnet: Boolean,
method: String,
json: String
): String = withContext(Dispatchers.IO) {
try {
val url = tonapiRawUrl(testnet, method)
val response = tonAPIHttpClient.postJSON(url, json)
tonapiRawResponse(response)
} catch (e: Throwable) {
"{\"error\": \"unknown client error\"}"
}
}

suspend fun tonapiGetRaw(
testnet: Boolean,
method: String,
params: String
): String = withContext(Dispatchers.IO) {
try {
val builder = Uri.parse(tonapiRawUrl(testnet, method)).buildUpon()
if (params.isBlank() && params.equals("null", ignoreCase = true)) {
val json = JSONObject(params)
for (key in json.keys()) {
builder.appendQueryParameter(key, json.getString(key))
}
}
val response = tonAPIHttpClient.simple(builder.toString())
tonapiRawResponse(response)
} catch (e: Throwable) {
"{\"error\": \"unknown client error\"}"
}
}

private val provider: Provider by lazy {
Provider(config.tonapiMainnetHost, config.tonapiTestnetHost, tonAPIHttpClient)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ data class AccountDetailsEntity(
private companion object {
private fun resolveVersion(interfaces: List<String>?): WalletVersion {
interfaces ?: return WalletVersion.UNKNOWN
return if(interfaces.contains("wallet_v5_beta")) {
return if (interfaces.contains("wallet_v5_beta")) {
WalletVersion.V5BETA
} else if (interfaces.contains("wallet_v5") || interfaces.contains("wallet_v5r1")) {
WalletVersion.V5R1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ class AccountRepository(
seconds + (5 * 30L) // 5 minutes
}

private fun messageBody(
fun messageBody(
wallet: WalletEntity,
seqNo: Int,
validUntil: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ data class WalletEntity(
data class Keystone(
val xfp: String,
val path: String
) : Parcelable
) : Parcelable {

val isEmpty: Boolean
get() = xfp.isBlank() || path.isBlank()
}

val contract: BaseWalletContract by lazy {
val network = if (testnet) TonNetwork.TESTNET.value else TonNetwork.MAINNET.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class DAppsRepository(
url = legacyApp.url.removeSuffix("/"),
name = legacyApp.name,
iconUrl = legacyApp.icon,
empty = false,
)
database.insertApp(newApp)
for (legacyConnections in legacyApp.connections) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ data class AppEntity(
val url: String,
val name: String,
val iconUrl: String,
val empty: Boolean
): Parcelable {

val host: String
Expand All @@ -19,6 +20,7 @@ data class AppEntity(
url = json.getString("url"),
name = json.getString("name"),
iconUrl = json.getString("iconUrl"),
empty = false,
)

constructor(value: String) : this(JSONObject(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,23 @@ internal class DatabaseSource(
apps.add(AppEntity(
url = cursor.getString(urlIndex),
name = cursor.getString(nameIndex),
iconUrl = cursor.getString(iconUrlIndex)
iconUrl = cursor.getString(iconUrlIndex),
empty = false
))
}
cursor.close()
apps
val notFoundApps = hosts.filter { host -> apps.none { it.host == host } }
if (notFoundApps.isNotEmpty()) {
for (host in notFoundApps) {
apps.add(AppEntity(
url = "https://$host",
name = host.split(".").firstOrNull() ?: host,
iconUrl = "https://$host/favicon.ico",
empty = true
))
}
}
apps.toList()
}

suspend fun getApp(host: String): AppEntity? = withContext(coroutineContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class RemoteDataSource(
accountId: String,
testnet: Boolean,
beforeLt: Long? = null,
limit: Int = 20
limit: Int = 12
): AccountEvents? = api.getEvents(accountId, testnet, beforeLt, limit)

fun getSingle(eventId: String, testnet: Boolean) = api.getSingleEvent(eventId, testnet)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.tonapps.wallet.data.settings

import android.content.Context
import android.util.Log
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import com.tonapps.extensions.MutableEffectFlow
import com.tonapps.extensions.clear
Expand All @@ -23,9 +21,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -61,9 +57,6 @@ class SettingsRepository(
private val _languageFlow = MutableEffectFlow<Language>()
val languageFlow = _languageFlow.stateIn(scope, SharingStarted.Eagerly, null).filterNotNull()

private val _themeFlow = MutableEffectFlow<Theme>()
val themeFlow = _themeFlow.stateIn(scope, SharingStarted.Eagerly, null).filterNotNull()

private val _hiddenBalancesFlow = MutableEffectFlow<Boolean>()
val hiddenBalancesFlow = _hiddenBalancesFlow.stateIn(scope, SharingStarted.Eagerly, null).filterNotNull()

Expand Down Expand Up @@ -118,7 +111,6 @@ class SettingsRepository(
if (value != field) {
prefs.edit().putString(THEME_KEY, value.key).apply()
field = value
_themeFlow.tryEmit(value)
migrationHelper.setLegacyTheme(value)
}
}
Expand Down Expand Up @@ -150,7 +142,7 @@ class SettingsRepository(
}
}

var language: Language = Language(prefs.getString(LANGUAGE_CODE_KEY, "en") ?: Language.DEFAULT)
var language: Language = Language(prefs.getString(LANGUAGE_CODE_KEY, Language.DEFAULT) ?: Language.DEFAULT)
set(value) {
if (value != field) {
field = value
Expand All @@ -160,6 +152,16 @@ class SettingsRepository(
}
}

val localeList: LocaleListCompat
get() {
if (language.code.isBlank() || language.code == Language.DEFAULT) {
return LocaleListCompat.getEmptyLocaleList()
} else {
val newLocale = Locale.forLanguageTag(language.code)
return LocaleListCompat.create(newLocale)
}
}

var lockScreen: Boolean = prefs.getBoolean(LOCK_SCREEN_KEY, false)
set(value) {
if (value != field) {
Expand Down Expand Up @@ -327,12 +329,6 @@ class SettingsRepository(
}

init {
languageFlow.onEach {
try {
AppCompatDelegate.setApplicationLocales(getLocales())
} catch (ignored: Throwable) { }
}.launchIn(scope)

scope.launch(Dispatchers.IO) {
if (rnLegacy.isRequestMigration()) {
prefs.clear()
Expand All @@ -355,7 +351,6 @@ class SettingsRepository(
}

_currencyFlow.tryEmit(currency)
_themeFlow.tryEmit(theme)
_languageFlow.tryEmit(language)
_hiddenBalancesFlow.tryEmit(hiddenBalances)
_firebaseTokenFlow.tryEmit(firebaseToken)
Expand Down Expand Up @@ -426,15 +421,6 @@ class SettingsRepository(
}
}

private fun getLocales(): LocaleListCompat {
val code = language.code
return if (code == Language.DEFAULT) {
LocaleListCompat.getEmptyLocaleList()
} else {
LocaleListCompat.forLanguageTags(code)
}
}

private fun fixCountryCode(code: String?): String {
return if (code.isNullOrBlank()) {
getLocale().country
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ data class StakingEntity(
val info = info.find { it.pool == pool.address } ?: return Coins.ZERO
return info.readyWithdraw
}

fun getPendingDeposit(pool: PoolEntity): Coins {
val info = info.find { it.pool == pool.address } ?: return Coins.ZERO
return info.pendingDeposit
}

fun getPendingWithdraw(pool: PoolEntity): Coins {
val info = info.find { it.pool == pool.address } ?: return Coins.ZERO
return info.pendingWithdraw
}
}
Loading

0 comments on commit a9388c9

Please sign in to comment.