Skip to content

Commit

Permalink
compress jetton
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Sep 13, 2024
1 parent d391a4b commit 4896152
Show file tree
Hide file tree
Showing 17 changed files with 441 additions and 46 deletions.
22 changes: 19 additions & 3 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 @@ -239,7 +239,9 @@ class API(
token = TokenEntity.TON,
value = Coins.of(account.balance),
walletAddress = accountId,
initializedAccount = initializedAccount
initializedAccount = initializedAccount,
isCompressed = false,
isTransferable = true
)
}

Expand All @@ -254,15 +256,29 @@ class API(
return TokenEntity(jetton)
}

fun getJettonCustomPayload(
accountId: String,
testnet: Boolean,
jettonId: String
): TokenEntity.TransferPayload? {
val jettonsAPI = jettons(testnet)
val payload = withRetry {
jettonsAPI.getJettonTransferPayload(accountId, jettonId)
} ?: return null
return TokenEntity.TransferPayload(tokenAddress = jettonId, payload)
}

fun getJettonsBalances(
accountId: String,
testnet: Boolean,
currency: String? = null
currency: String? = null,
extensions: List<String>? = null
): List<BalanceEntity>? {
val jettonsBalances = withRetry {
accounts(testnet).getAccountJettonsBalances(
accountId = accountId,
currencies = currency?.let { listOf(it) }
currencies = currency?.let { listOf(it) },
extensions = extensions,
).balances
} ?: return null
return jettonsBalances.map { BalanceEntity(it) }.filter { it.value.isPositive }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tonapps.wallet.api.entity

import android.os.Parcelable
import android.util.Log
import com.tonapps.icu.Coins
import io.tonapi.models.JettonBalance
import io.tonapi.models.TokenRates
Expand All @@ -13,18 +14,31 @@ data class BalanceEntity(
val token: TokenEntity,
val value: Coins,
val walletAddress: String,
val initializedAccount: Boolean
val initializedAccount: Boolean,
val isCompressed: Boolean,
val isTransferable: Boolean,
): Parcelable {

companion object {

fun empty(accountId: String) = create(accountId, Coins.ZERO)
fun empty(
accountId: String,
isCompressed: Boolean,
isTransferable: Boolean
) = create(accountId, Coins.ZERO, isCompressed, isTransferable)

fun create(accountId: String, value: Coins) = BalanceEntity(
fun create(
accountId: String,
value: Coins,
isCompressed: Boolean,
isTransferable: Boolean
) = BalanceEntity(
token = TokenEntity.TON,
value = value,
walletAddress = accountId,
initializedAccount = false,
isCompressed = isCompressed,
isTransferable = isTransferable
)
}

Expand All @@ -38,10 +52,12 @@ data class BalanceEntity(
get() = token.decimals

constructor(jettonBalance: JettonBalance) : this(
token = TokenEntity(jettonBalance.jetton),
token = TokenEntity(jettonBalance.jetton, jettonBalance.extensions, jettonBalance.lock),
value = Coins.of(BigDecimal(jettonBalance.balance).movePointLeft(jettonBalance.jetton.decimals), jettonBalance.jetton.decimals),
walletAddress = jettonBalance.walletAddress.address,
initializedAccount = true,
isCompressed = jettonBalance.extensions?.contains(TokenEntity.Extension.CustomPayload.value) == true,
isTransferable = jettonBalance.extensions?.contains(TokenEntity.Extension.NonTransferable.value) != true
) {
rates = jettonBalance.price
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package com.tonapps.wallet.api.entity
import android.net.Uri
import android.os.Parcelable
import android.util.Log
import com.tonapps.blockchain.ton.extensions.safeParseCell
import com.tonapps.blockchain.ton.extensions.toRawAddress
import com.tonapps.wallet.api.R
import io.tonapi.models.JettonBalanceLock
import io.tonapi.models.JettonInfo
import io.tonapi.models.JettonPreview
import io.tonapi.models.JettonTransferPayload
import io.tonapi.models.JettonVerificationType
import kotlinx.parcelize.Parcelize
import org.ton.block.StateInit
import org.ton.cell.Cell

@Parcelize
data class TokenEntity(
Expand All @@ -17,13 +22,58 @@ data class TokenEntity(
val symbol: String,
val imageUri: Uri,
val decimals: Int,
val verification: Verification
val verification: Verification,
val isCompressed: Boolean,
val isTransferable: Boolean,
val lock: Lock? = null
): Parcelable {

enum class Verification {
whitelist, blacklist, none
}

@Parcelize
data class Lock(
val amount: String,
val till: Long
): Parcelable {

constructor(lock: JettonBalanceLock) : this(
amount = lock.amount,
till = lock.till
)
}

data class TransferPayload(
val tokenAddress: String,
val customPayload: Cell? = null,
val stateInit: StateInit? = null
) {

companion object {

fun empty(tokenAddress: String): TransferPayload {
return TransferPayload(tokenAddress)
}
}

val isEmpty: Boolean
get() = customPayload == null && stateInit == null

constructor(tokenAddress: String, model: JettonTransferPayload) : this(
tokenAddress = tokenAddress,
customPayload = model.customPayload?.safeParseCell(),
stateInit = model.stateInit?.safeParseCell()?.let {
StateInit.Companion.loadTlb(it)
},
)
}

enum class Extension(val value: String) {
NonTransferable("non_transferable"),
CustomPayload("custom_payload")
}

companion object {

val TON_ICON_URI = Uri.Builder().scheme("res").path(R.drawable.ic_ton_with_bg.toString()).build()
Expand All @@ -35,7 +85,9 @@ data class TokenEntity(
symbol = "TON",
imageUri = TON_ICON_URI,
decimals = 9,
verification = Verification.whitelist
verification = Verification.whitelist,
isCompressed = false,
isTransferable = true
)

val USDT = TokenEntity(
Expand All @@ -44,7 +96,9 @@ data class TokenEntity(
symbol = "USD₮",
imageUri = USDT_ICON_URI,
decimals = 6,
verification = Verification.whitelist
verification = Verification.whitelist,
isCompressed = false,
isTransferable = true
)

private fun convertVerification(verification: JettonVerificationType): Verification {
Expand All @@ -68,21 +122,35 @@ data class TokenEntity(
val blacklist: Boolean
get() = verification == TokenEntity.Verification.blacklist

constructor(jetton: JettonPreview) : this(
constructor(
jetton: JettonPreview,
extensions: List<String>? = null,
lock: JettonBalanceLock? = null
) : this(
address = jetton.address.toRawAddress(),
name = jetton.name,
symbol = jetton.symbol,
imageUri = Uri.parse(jetton.image),
decimals = jetton.decimals,
verification = convertVerification(jetton.verification)
verification = convertVerification(jetton.verification),
isCompressed = extensions?.contains(Extension.CustomPayload.value) == true,
isTransferable = extensions?.contains(Extension.NonTransferable.value) != true,
lock = lock?.let { Lock(it) }
)

constructor(jetton: JettonInfo) : this(
constructor(
jetton: JettonInfo,
extensions: List<String>? = null,
lock: JettonBalanceLock? = null
) : this(
address = jetton.metadata.address.toRawAddress(),
name = jetton.metadata.name,
symbol = jetton.metadata.symbol,
imageUri = Uri.parse(jetton.metadata.image),
decimals = jetton.metadata.decimals.toInt(),
verification = convertVerification(jetton.verification)
verification = convertVerification(jetton.verification),
isCompressed = extensions?.contains(Extension.CustomPayload.value) == true,
isTransferable = extensions?.contains(Extension.NonTransferable.value) != true,
lock = lock?.let { Lock(it) }
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ data class RawMessageEntity(
val payload: Cell
get() = payloadValue.safeParseCell() ?: Cell()

fun getWalletTransfer(excessesAddress: AddrStd? = null): WalletTransfer {
fun getWalletTransfer(
excessesAddress: AddrStd? = null
): WalletTransfer {
val builder = WalletTransferBuilder()
builder.stateInit = stateInit
builder.destination = address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ data class AccountTokenEntity(
companion object {

val EMPTY = AccountTokenEntity(
BalanceEntity(
TokenEntity.TON,
Coins.ZERO,
"",
false
balance = BalanceEntity(
token = TokenEntity.TON,
value = Coins.ZERO,
walletAddress = "",
initializedAccount = false,
isCompressed = false,
isTransferable = true,
)
)
}
Expand Down Expand Up @@ -62,4 +64,7 @@ data class AccountTokenEntity(

val blacklist: Boolean
get() = balance.token.blacklist

val isCompressed: Boolean
get() = balance.isCompressed
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ internal class RemoteDataSource(
testnet: Boolean
): List<BalanceEntity>? = withContext(Dispatchers.IO) {
val tonBalanceDeferred = async { api.getTonBalance(accountId, testnet) }
val jettonBalancesDeferred = async { api.getJettonsBalances(accountId, testnet, currency.code) }
val jettonBalancesDeferred = async { api.getJettonsBalances(
accountId = accountId,
testnet = testnet,
currency = currency.code,
extensions = listOf(
TokenEntity.Extension.CustomPayload.value,
TokenEntity.Extension.NonTransferable.value
))
}
val tonBalance = tonBalanceDeferred.await() ?: return@withContext null
val jettons = jettonBalancesDeferred.await()?.toMutableList() ?: mutableListOf()

Expand All @@ -36,7 +44,9 @@ internal class RemoteDataSource(
token = TokenEntity.USDT,
value = Coins.ZERO,
walletAddress = accountId,
initializedAccount = tonBalance.initializedAccount
initializedAccount = tonBalance.initializedAccount,
isCompressed = false,
isTransferable = true
))
} else if (usdtIndex >= 0) {
jettons[usdtIndex] = jettons[usdtIndex].copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ data class AssetsExtendedEntity(
get() = when (raw) {
is AssetsEntity.Token -> raw.token
is AssetsEntity.Staked -> AccountTokenEntity(
balance = BalanceEntity.create(accountId, raw.staked.balance),
balance = BalanceEntity.create(
accountId = accountId,
value = raw.staked.balance,
isCompressed = false,
isTransferable = true
),
)
}

Expand Down
Loading

0 comments on commit 4896152

Please sign in to comment.