Skip to content

Commit

Permalink
BigInteger for coins, token manager, wallets sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed May 31, 2024
1 parent f56a60d commit a47e9eb
Show file tree
Hide file tree
Showing 108 changed files with 1,860 additions and 816 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions apps/signer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId = Build.namespacePrefix("signer")
minSdk = Build.minSdkVersion
targetSdk = 34
versionCode = 13
versionName = "0.1.3"
versionCode = 14
versionName = "0.1.4"
}

lint {
Expand Down
4 changes: 2 additions & 2 deletions apps/signer/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@
<service
android:name="com.google.android.datatransport.runtime.backends.TransportBackendDiscovery"
tools:node="remove" />
<receiver
<!--receiver
android:name="androidx.profileinstaller.ProfileInstallReceiver"
tools:node="remove" />
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="com.tonapps.signer.androidx-startup"
tools:node="remove"/>
tools:node="remove"/-->
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.tonapps.security.vault.safeArea
import com.tonapps.uikit.list.ListCell
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.receiveAsFlow
import java.math.BigDecimal

class SignViewModel(
private val id: Long,
Expand Down Expand Up @@ -201,8 +202,8 @@ class SignViewModel(
}

private fun formatCoins(coins: Coins): String {
val value = coins.amount.toLong() / 1000000000L.toFloat()
return CurrencyFormatter.format("TON", value).toString()
val value = coins.amount.toLong() / 1000000000L.toDouble()

This comment has been minimized.

Copy link
@vkryl

vkryl Jun 4, 2024

I was just browsing around dev branch, and noticed this error: coins.amount.toLong() is just a shortcut for BigInteger's longValue method:

Converts this BigInteger to a long. This conversion is analogous to a narrowing primitive conversion from long to int as defined in The Java Language Specification: if this BigInteger is too big to fit in a long, only the low-order 64 bits are returned.

Code below should work properly for inputs of any size (unless I made a mistake counting zeroes). Inlined Coin.toCoins from PR #17:

val value = coins.amount.value.toBigDecimal().movePointLeft(9).stripTrailingZeros()
return CurrencyFormatter.format("TON", value).toString()
return CurrencyFormatter.format("TON", BigDecimal(value)).toString()
}

private fun parseAddress(address: MsgAddressInt, bounceable: Boolean = true): String {
Expand Down
10 changes: 3 additions & 7 deletions apps/wallet/api/src/main/java/com/tonapps/wallet/api/API.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.tonapps.wallet.api

import android.content.Context
import android.os.SystemClock
import android.util.ArrayMap
import android.util.Log
import com.tonapps.blockchain.Coin
import com.tonapps.blockchain.Coins
import com.tonapps.blockchain.ton.extensions.base64
import com.tonapps.blockchain.ton.extensions.isValid
import com.tonapps.extensions.ifPunycodeToUnicode
import com.tonapps.extensions.locale
import com.tonapps.extensions.unicodeToPunycode
import com.tonapps.network.SSEvent
Expand Down Expand Up @@ -45,7 +42,6 @@ import org.json.JSONArray
import org.json.JSONObject
import org.ton.api.pub.PublicKeyEd25519
import org.ton.cell.Cell
import org.ton.crypto.base64
import java.util.Locale
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -118,7 +114,7 @@ class API(
testnet: Boolean
): BalanceEntity {
val account = accounts(testnet).getAccount(accountId)
return BalanceEntity(TokenEntity.TON, Coin.toCoinsDouble(account.balance), accountId)
return BalanceEntity(TokenEntity.TON, Coins.of(account.balance), accountId)
}

fun getJettonsBalances(
Expand All @@ -131,7 +127,7 @@ class API(
accountId = accountId,
currencies = currency
).balances
return jettonsBalances.map { BalanceEntity(it) }.filter { it.value > 0 }
return jettonsBalances.map { BalanceEntity(it) }.filter { !it.value.isZero }
} catch (e: Throwable) {
return emptyList()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.tonapps.wallet.api.entity

import android.os.Parcelable
import android.util.Log
import com.tonapps.blockchain.Coin
import com.tonapps.blockchain.Coins
import io.tonapi.models.JettonBalance
import io.tonapi.models.TokenRates
import kotlinx.parcelize.IgnoredOnParcel
Expand All @@ -11,7 +10,7 @@ import kotlinx.parcelize.Parcelize
@Parcelize
data class BalanceEntity(
val token: TokenEntity,
val value: Double,
val value: Coins,
val walletAddress: String
): Parcelable {

Expand All @@ -20,7 +19,7 @@ data class BalanceEntity(

constructor(jettonBalance: JettonBalance) : this(
token = TokenEntity(jettonBalance.jetton),
value = Coin.parseJettonBalance(jettonBalance.balance, jettonBalance.jetton.decimals),
value = Coins.of(jettonBalance.balance, jettonBalance.jetton.decimals),
walletAddress = jettonBalance.walletAddress.address,
) {
rates = jettonBalance.price
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ data class TokenEntity(
companion object {

val TON_ICON_URI = Uri.Builder().scheme("res").path(R.drawable.ic_ton_with_bg.toString()).build()
val USDT_ICON_URI = Uri.Builder().scheme("res").path(R.drawable.ic_usdt.toString()).build()

val TON = TokenEntity(
address = "TON",
Expand All @@ -34,6 +35,15 @@ data class TokenEntity(
verification = Verification.whitelist
)

val USDT = TokenEntity(
address = "0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe",
name = "Tether USD",
symbol = "USD₮",
imageUri = USDT_ICON_URI,
decimals = 6,
verification = Verification.whitelist
)

private fun convertVerification(verification: JettonVerificationType): Verification {
return when (verification) {
JettonVerificationType.whitelist -> Verification.whitelist
Expand Down
12 changes: 12 additions & 0 deletions apps/wallet/api/src/main/res/drawable/ic_usdt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="2500dp"
android:height="2500dp"
android:viewportWidth="2000"
android:viewportHeight="2000">
<path
android:pathData="M1000,0c552.3,0 1000,447.7 1000,1000s-447.8,1000 -1000,1000S0,1552.4 0,1000 447.7,0 1000,0"
android:fillColor="#53ae94"/>
<path
android:pathData="M1123.4,866.8V718h340.2V491.3H537.3V718H877.5v148.6C601,879.3 393.1,934.1 393.1,999.7s208,120.4 484.4,133.1v476.5h246V1132.8c276,-12.7 483.5,-67.5 483.5,-133s-207.5,-120.3 -483.5,-133m0,225.6v-0.1c-6.9,0.4 -42.6,2.6 -122,2.6 -63.5,0 -108.1,-1.8 -123.9,-2.6v0.2C633.3,1081.7 451,1039.1 451,988.2S633.4,894.8 877.6,884v166.1c16,1.1 61.8,3.8 124.9,3.8 75.9,0 114,-3.2 121,-3.8V884c243.8,10.9 425.7,53.4 425.7,104.2s-182,93.3 -425.7,104.2"
android:fillColor="#fff"/>
</vector>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tonapps.wallet.data.events.entities

import android.os.Parcelable
import com.tonapps.blockchain.Coin
import com.tonapps.blockchain.Coins
import com.tonapps.wallet.api.entity.AccountEntity
import com.tonapps.wallet.api.entity.TokenEntity
import com.tonapps.wallet.data.collectibles.entities.NftEntity
Expand All @@ -21,7 +21,7 @@ data class ActionEntity(
val recipient: AccountEntity? = null,
val comment: String? = null,
val token: TokenEntity? = null,
val amount: Double? = null,
val amount: Coins? = null,
val nftAddress: String? = null,
var nftEntity: NftEntity? = null
): Parcelable {
Expand Down Expand Up @@ -62,7 +62,7 @@ data class ActionEntity(
recipient = AccountEntity(tonTransfer.recipient, testnet),
comment = tonTransfer.comment,
token = TokenEntity.TON,
amount = Coin.toCoinsDouble(tonTransfer.amount)
amount = Coins.of(tonTransfer.amount)
)
}

Expand All @@ -76,7 +76,7 @@ data class ActionEntity(
recipient = jettonTransfer.recipient?.let { AccountEntity(it, testnet) },
comment = jettonTransfer.comment,
token = TokenEntity(jettonTransfer.jetton),
amount = Coin.parseJettonBalance(jettonTransfer.amount, jettonTransfer.jetton.decimals)
amount = Coins.of(jettonTransfer.amount, jettonTransfer.jetton.decimals)
)
}

Expand Down
1 change: 1 addition & 0 deletions apps/wallet/data/rates/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
implementation(project(Dependence.Wallet.api))
implementation(project(Dependence.Wallet.Data.core))
implementation(project(Dependence.Module.tonApi))
implementation(project(Dependence.Lib.blockchain))
implementation(project(Dependence.Lib.extensions))
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.tonapps.wallet.data.rates

import android.content.Context
import com.tonapps.blockchain.Coins
import com.tonapps.wallet.api.API
import com.tonapps.wallet.data.core.WalletCurrency
import com.tonapps.wallet.data.rates.entity.RateDiffEntity
import com.tonapps.wallet.data.rates.entity.RateEntity
import com.tonapps.wallet.data.rates.entity.RatesEntity
import com.tonapps.wallet.data.rates.source.BlobDataSource
import io.tonapi.models.TokenRates
import java.math.BigDecimal

class RatesRepository(
context: Context,
Expand Down Expand Up @@ -37,7 +40,14 @@ class RatesRepository(
}
val entities = mutableListOf<RateEntity>()
for (rate in rates) {
entities.add(RateEntity(currency, rate.key, rate.value))
val value = rate.value
val bigDecimal = value.prices?.get(currency.code) ?: BigDecimal.ZERO
entities.add(RateEntity(
tokenCode = rate.key,
currency = currency,
value = Coins(bigDecimal),
diff = RateDiffEntity(currency, value),
))
}
localDataSource.add(currency, entities)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tonapps.wallet.data.rates.entity

import android.os.Parcelable
import com.tonapps.blockchain.Coins
import com.tonapps.wallet.data.core.WalletCurrency
import io.tonapi.models.TokenRates
import kotlinx.parcelize.Parcelize
Expand All @@ -9,18 +10,6 @@ import kotlinx.parcelize.Parcelize
data class RateEntity(
val tokenCode: String,
val currency: WalletCurrency,
val value: Float,
val value: Coins,
val diff: RateDiffEntity
): Parcelable {

constructor(
currency: WalletCurrency,
token: String,
rates: TokenRates
) : this(
tokenCode = token,
currency = currency,
value = rates.prices?.get(currency.code)?.toFloat() ?: 0f,
diff = RateDiffEntity(currency, rates)
)
}
): Parcelable
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tonapps.wallet.data.rates.entity

import android.os.Parcelable
import com.tonapps.blockchain.Coins
import com.tonapps.wallet.data.core.WalletCurrency
import kotlinx.parcelize.Parcelize

Expand Down Expand Up @@ -45,27 +46,23 @@ data class RatesEntity(
return map[token]
}

fun rateValue(token: String): Float {
return rate(token)?.value ?: 0f
fun rateValue(token: String): Coins {
return rate(token)?.value ?: Coins.ZERO
}

fun rateDiff(token: String): RateDiffEntity? {
return rate(token)?.diff
}

fun convert(token: String, value: Float): Float {
fun convert(token: String, value: Coins): Coins {
return value * rateValue(token)
}

fun convert(token: String, value: Double): Double {
return value * rateValue(token)
}

fun convertFromFiat(token: String, value: Double): Double {
fun convertFromFiat(token: String, value: Coins): Coins {
return value / rateValue(token)
}

fun getRate(token: String): Float {
fun getRate(token: String): Coins {
return rateValue(token)
}

Expand Down
1 change: 0 additions & 1 deletion apps/wallet/data/settings/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ android {
}
}


dependencies {
implementation(Dependence.KotlinX.coroutines)
implementation(Dependence.Koin.core)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.tonapps.wallet.data.settings

import android.content.Context
import androidx.collection.ArrayMap
import com.tonapps.extensions.MutableEffectFlow
import com.tonapps.extensions.locale
import com.tonapps.wallet.data.core.SearchEngine
import com.tonapps.wallet.data.core.Theme
import com.tonapps.wallet.data.core.WalletCurrency
import com.tonapps.wallet.data.settings.entities.TokenPrefsEntity
import com.tonapps.wallet.data.settings.folder.TokenPrefsFolder
import com.tonapps.wallet.data.settings.folder.WalletPrefsFolder
import com.tonapps.wallet.localization.Language
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -65,6 +71,11 @@ class SettingsRepository(
val walletPush = _walletPush.stateIn(scope, SharingStarted.Eagerly, null).filterNotNull()

private val prefs = context.getSharedPreferences(NAME, Context.MODE_PRIVATE)
private val tokenPrefsFolder = TokenPrefsFolder(context)
private val walletPrefsFolder = WalletPrefsFolder(context)

val tokenPrefsChangedFlow: Flow<Unit>
get() = tokenPrefsFolder.changedFlow

val installId: String
get() = prefs.getString(INSTALL_ID_KEY, null) ?: run {
Expand Down Expand Up @@ -172,6 +183,28 @@ class SettingsRepository(
_walletPush.tryEmit(map)
}

fun setTokenHidden(walletId: Long, tokenAddress: String, hidden: Boolean) {
tokenPrefsFolder.setHidden(walletId, tokenAddress, hidden)
}

fun setTokenPinned(walletId: Long, tokenAddress: String, pinned: Boolean) {
tokenPrefsFolder.setPinned(walletId, tokenAddress, pinned)
}

fun setTokensSort(walletId: Long, tokensAddress: List<String>) {
tokenPrefsFolder.setSort(walletId, tokensAddress)
}

fun setWalletsSort(walletIds: List<Long>) {
walletPrefsFolder.setSort(walletIds)
}

fun getWalletPrefs(walletId: Long) = walletPrefsFolder.get(walletId)

fun getTokenPrefs(walletId: Long, tokenAddress: String): TokenPrefsEntity {
return tokenPrefsFolder.get(walletId, tokenAddress)
}

init {
scope.launch(Dispatchers.IO) {
_currencyFlow.tryEmit(currency)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tonapps.wallet.data.settings.entities

data class TokenPrefsEntity(
val pinned: Boolean = false,
val hidden: Boolean = false,
val index: Int = -1,
)
Loading

0 comments on commit a47e9eb

Please sign in to comment.