Skip to content

Commit

Permalink
bug fixeds
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Sep 16, 2024
1 parent 67c133f commit 9f40413
Show file tree
Hide file tree
Showing 19 changed files with 286 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sealed class Wallet {
get() = accountName.isBlank() && emoji.isBlank()

val name: String
get() = accountName.ifBlank { "Tonkeeper" }
get() = accountName

val title: CharSequence?
get() = if (isEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,15 @@ class TokenRepository(

fun getToken(accountId: String, testnet: Boolean) = remoteDataSource.getJetton(accountId, testnet)

suspend fun getTotalBalances(
currency: WalletCurrency,
accountId: String,
testnet: Boolean
): Coins? {
return totalBalanceCache[cacheKey(accountId, testnet)] ?: loadTotalBalances(currency, accountId, testnet)
}

private suspend fun loadTotalBalances(
currency: WalletCurrency,
accountId: String,
testnet: Boolean
): Coins? {
val tokens = get(currency, accountId, testnet) ?: return null
var fiatBalance = Coins.of(0)
if (testnet) {
fiatBalance = tokens.first().balance.value
} else {
for (token in tokens) {
fiatBalance += token.fiat
}
}
totalBalanceCache[cacheKey(accountId, testnet)] = fiatBalance
return fiatBalance
}

suspend fun get(
currency: WalletCurrency,
accountId: String,
testnet: Boolean
testnet: Boolean,
refresh: Boolean = false,
): List<AccountTokenEntity>? {
if (refresh) {
return getRemote(currency, accountId, testnet)
}
val tokens = getLocal(currency, accountId, testnet)
if (tokens.isNotEmpty()) {
return tokens
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.tonapps.blockchain.ton.TONOpCode
import com.tonapps.blockchain.ton.TonSendMode
import com.tonapps.blockchain.ton.TonTransferHelper
import com.tonapps.blockchain.ton.contract.BaseWalletContract
import com.tonapps.blockchain.ton.extensions.EmptyPrivateKeyEd25519
import com.tonapps.blockchain.ton.extensions.storeOpCode
import com.tonapps.blockchain.ton.extensions.storeStringTail
import com.tonapps.blockchain.ton.extensions.toAccountId
Expand Down Expand Up @@ -52,6 +53,10 @@ data class TransferEntity(
val contract: BaseWalletContract
get() = wallet.contract

val fakePrivateKey: PrivateKeyEd25519 by lazy {
if (commentEncrypted) PrivateKeyEd25519() else EmptyPrivateKeyEd25519
}

val isTon: Boolean
get() = token.isTon

Expand Down Expand Up @@ -261,7 +266,7 @@ data class TransferEntity(
}

fun toSignedMessage(
privateKey: PrivateKeyEd25519,
privateKey: PrivateKeyEd25519 = fakePrivateKey,
internalMessage: Boolean,
excessesAddress: AddrStd? = null,
additionalGifts: List<WalletTransfer> = emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.tonapps.wallet.data.core.entity.RawMessageEntity
import org.ton.block.AddrStd
import org.ton.block.Coins
import org.ton.block.MsgAddressInt
import org.ton.block.StateInit
import org.ton.cell.Cell
import org.ton.cell.CellBuilder
import org.ton.contract.wallet.WalletTransfer
Expand Down Expand Up @@ -48,18 +49,23 @@ private fun RawMessageEntity.rebuildBodyWithCustomExcessesAccount(
}

private fun RawMessageEntity.rebuildJettonTransferWithCustomPayload(
customPayload: Cell,
newCustomPayload: Cell,
): Cell {
val slice = payload.beginParse()
val opCode = slice.loadOpCode()
if (opCode != TONOpCode.JETTON_TRANSFER) {
return customPayload
return newCustomPayload
}

val queryId = slice.loadUInt(64)
val jettonAmount = slice.loadTlb(Coins.tlbCodec())
val receiverAddress = slice.loadTlb(AddrStd.tlbCodec())
val excessesAddress = slice.loadTlb(AddrStd.tlbCodec())
slice.loadMaybeRef()
val customPayload = slice.loadMaybeRef()
if (customPayload != null) {
return payload
}

val forwardAmount = slice.loadTlb(Coins.tlbCodec()).amount.toLong()
val forwardBody = slice.loadMaybeRef()

Expand All @@ -70,22 +76,39 @@ private fun RawMessageEntity.rebuildJettonTransferWithCustomPayload(
queryId = queryId,
forwardAmount = forwardAmount,
forwardPayload = forwardBody,
customPayload = customPayload
customPayload = newCustomPayload
)
}

fun RawMessageEntity.getJettonAddress(): AddrStd? {
val slice = payload.beginParse()
val opCode = slice.loadOpCode()
if (opCode != TONOpCode.JETTON_TRANSFER) {
return null
}
slice.loadUInt(64)
slice.loadTlb(Coins.tlbCodec())
slice.loadTlb(AddrStd.tlbCodec())
val excessesAddress = slice.loadTlb(AddrStd.tlbCodec())
val customPayload = slice.loadMaybeRef()
if (customPayload != null) {
return null
}
return excessesAddress
}

fun RawMessageEntity.getWalletTransfer(
excessesAddress: AddrStd? = null,
customPayload: Cell? = null
newStateInit: StateInit? = null,
newCustomPayload: Cell? = null,
): WalletTransfer {
val builder = WalletTransferBuilder()
builder.stateInit = stateInit
builder.stateInit = newStateInit ?: stateInit
builder.destination = address
builder.body = if (excessesAddress != null) {
rebuildBodyWithCustomExcessesAccount(excessesAddress)
} else if (customPayload != null) {
rebuildJettonTransferWithCustomPayload(customPayload)
} else if (newCustomPayload != null) {
rebuildJettonTransferWithCustomPayload(newCustomPayload)
} else {
payload
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
package com.tonapps.tonkeeper.extensions

import com.tonapps.blockchain.ton.extensions.toAccountId
import com.tonapps.wallet.api.API
import com.tonapps.wallet.data.account.entities.WalletEntity
import com.tonapps.wallet.data.core.entity.SignRequestEntity
import com.tonapps.wallet.data.token.entities.AccountTokenEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.ton.block.AddrStd
import org.ton.contract.wallet.WalletTransfer

suspend fun SignRequestEntity.getTransfers(): List<WalletTransfer> {
return messages.map { it.getWalletTransfer() }
suspend fun SignRequestEntity.getTransfers(
wallet: WalletEntity,
compressedTokens: List<AccountTokenEntity>,
excessesAddress: AddrStd? = null,
api: API,
): List<WalletTransfer> = withContext(Dispatchers.IO) {
val transfers = mutableListOf<WalletTransfer>()
for (message in messages) {
val jettonCustomPayload = message.getJettonAddress()?.toAccountId()?.let {
api.getJettonCustomPayload(wallet.accountId, wallet.testnet, it)
}

val transfer = message.getWalletTransfer(
excessesAddress = excessesAddress,
newStateInit = jettonCustomPayload?.stateInit,
newCustomPayload = jettonCustomPayload?.customPayload,
)
transfers.add(transfer)
}
transfers
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.tonapps.tonkeeper.manager.tx.TransactionManager
import com.tonapps.tonkeeper.core.history.HistoryHelper
import com.tonapps.tonkeeper.ui.screen.main.MainViewModel
import com.tonapps.tonkeeper.ui.screen.root.RootViewModel
import com.tonapps.tonkeeper.sign.SignManager
import com.tonapps.tonkeeper.manager.SignManager
import com.tonapps.tonkeeper.ui.base.BaseWalletVM
import com.tonapps.tonkeeper.ui.screen.add.imprt.ImportWalletViewModel
import com.tonapps.tonkeeper.ui.screen.battery.BatteryViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,54 @@ class AssetsManager(

suspend fun getAssets(
wallet: WalletEntity,
currency: WalletCurrency = settingsRepository.currency
currency: WalletCurrency = settingsRepository.currency,
refresh: Boolean,
): List<AssetsEntity>? {
val tokens = getTokens(wallet, currency) ?: return null
val staked = getStaked(wallet, tokens.map { it.token }, currency)
val tokens = getTokens(wallet, currency, refresh)
val staked = getStaked(wallet, tokens.map { it.token }, currency, refresh)

val liquid = staked.find { it.isTonstakers }?.liquidToken
val filteredTokens = if (liquid == null) {
tokens
} else {
tokens.filter { !liquid.token.address.contains(it.address) }
}
return (filteredTokens + staked).sortedBy { it.fiat }.reversed()
val list = (filteredTokens + staked).sortedBy { it.fiat }.reversed()
if (list.isEmpty()) {
return null
}
return list
}

private suspend fun getTokens(
wallet: WalletEntity,
currency: WalletCurrency = settingsRepository.currency
): List<AssetsEntity.Token>? {
val tokens = tokenRepository.get(currency, wallet.accountId, wallet.testnet) ?: return null
currency: WalletCurrency = settingsRepository.currency,
refresh: Boolean,
): List<AssetsEntity.Token> {
val tokens = tokenRepository.get(currency, wallet.accountId, wallet.testnet, refresh) ?: return emptyList()
return tokens.map { AssetsEntity.Token(it) }
}

private suspend fun getStaked(
wallet: WalletEntity,
tokens: List<AccountTokenEntity>,
currency: WalletCurrency = settingsRepository.currency
currency: WalletCurrency = settingsRepository.currency,
refresh: Boolean,
): List<AssetsEntity.Staked> {
val staking = getStaking(wallet)
val staking = getStaking(wallet, refresh)
val staked = StakedEntity.create(staking, tokens, currency, ratesRepository)
return staked.map { AssetsEntity.Staked(it) }
}

private suspend fun getStaking(wallet: WalletEntity): StakingEntity {
return stakingRepository.get(wallet.accountId, wallet.testnet)
private suspend fun getStaking(
wallet: WalletEntity,
refresh: Boolean
): StakingEntity {
return stakingRepository.get(
accountId = wallet.accountId,
testnet = wallet.testnet,
ignoreCache = refresh
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.tonapps.tonkeeper.sign
package com.tonapps.tonkeeper.manager

import android.os.CancellationSignal
import android.util.Log
import com.tonapps.blockchain.ton.extensions.EmptyPrivateKeyEd25519
import com.tonapps.tonkeeper.core.AnalyticsHelper
import com.tonapps.tonkeeper.core.SendBlockchainException
Expand All @@ -21,6 +20,8 @@ import com.tonapps.wallet.data.core.entity.SignRequestEntity
import com.tonapps.wallet.data.rates.RatesRepository
import com.tonapps.wallet.data.settings.BatteryTransaction
import com.tonapps.wallet.data.settings.SettingsRepository
import com.tonapps.wallet.data.token.TokenRepository
import com.tonapps.wallet.data.token.entities.AccountTokenEntity
import com.tonapps.wallet.localization.Localization
import kotlinx.coroutines.suspendCancellableCoroutine
import uikit.navigation.Navigation
Expand All @@ -35,6 +36,7 @@ class SignManager(
private val historyHelper: HistoryHelper,
private val batteryRepository: BatteryRepository,
private val transactionManager: TransactionManager,
private val tokenRepository: TokenRepository,
) {

suspend fun action(
Expand All @@ -45,18 +47,28 @@ class SignManager(
batteryTxType: BatteryTransaction? = null,
forceRelayer: Boolean = false,
): String {
Log.d("ActionLog", "SignManager.action #1")
navigation.toastLoading(true)

val compressedTokens = tokenRepository.get(settingsRepository.currency, wallet.accountId, wallet.testnet)?.filter { it.isCompressed } ?: emptyList()

var isBattery = batteryTxType != null && settingsRepository.batteryIsEnabledTx(wallet.accountId, batteryTxType)

Log.d("ActionLog", "SignManager.action #2")
val details: HistoryHelper.Details?
if (isBattery || forceRelayer) {
val result = emulateBattery(request, wallet, forceRelayer = forceRelayer)
val result = emulateBattery(
request = request,
wallet = wallet,
forceRelayer = forceRelayer,
compressedTokens = compressedTokens
)
details = result.first
isBattery = result.second
} else {
details = emulate(request, wallet)
details = emulate(
request = request,
wallet = wallet,
compressedTokens = compressedTokens
)
}

navigation.toastLoading(false)
Expand Down Expand Up @@ -101,6 +113,7 @@ class SignManager(
request: SignRequestEntity,
wallet: WalletEntity,
currency: WalletCurrency = settingsRepository.currency,
compressedTokens: List<AccountTokenEntity>,
): HistoryHelper.Details? {

val rates = ratesRepository.getRates(currency, "TON")
Expand All @@ -118,7 +131,7 @@ class SignManager(
seqno = seqno,
privateKeyEd25519 = EmptyPrivateKeyEd25519,
validUntil = validUntil,
transfers = request.getTransfers()
transfers = request.getTransfers(wallet, compressedTokens, api = api)
)
val emulated = api.emulate(cell, wallet.testnet) ?: return null
historyHelper.create(wallet, emulated, rates)
Expand All @@ -132,6 +145,7 @@ class SignManager(
wallet: WalletEntity,
currency: WalletCurrency = settingsRepository.currency,
forceRelayer: Boolean,
compressedTokens: List<AccountTokenEntity>,
): Pair<HistoryHelper.Details?, Boolean> {
return try {
if (api.config.isBatteryDisabled) {
Expand All @@ -142,7 +156,14 @@ class SignManager(

val rates = ratesRepository.getRates(currency, "TON")
val seqno = accountRepository.getSeqno(wallet)
val cell = accountRepository.createSignedMessage(wallet, seqno, EmptyPrivateKeyEd25519, request.validUntil, request.getTransfers(), internalMessage = true)
val cell = accountRepository.createSignedMessage(
wallet = wallet,
seqno = seqno,
privateKeyEd25519 = EmptyPrivateKeyEd25519,
validUntil = request.validUntil,
transfers = request.getTransfers(wallet, compressedTokens, api = api),
internalMessage = true
)

val (consequences, withBattery) = batteryRepository.emulate(
tonProofToken = tonProofToken,
Expand All @@ -155,7 +176,7 @@ class SignManager(
val details = historyHelper.create(wallet, consequences, rates, isBattery = true)
Pair(details, withBattery)
} catch (e: Throwable) {
Pair(emulate(request, wallet, currency), false)
Pair(emulate(request, wallet, currency, compressedTokens), false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class TransactionManager(
emit(initialTx)

if (initialTx.pending) {
delay(15.seconds)
delay(20.seconds)
while (currentCoroutineContext().isActive) {
val finalTx = getTransaction(wallet, hash)
if (finalTx == null || finalTx.pending) {
Expand Down
Loading

0 comments on commit 9f40413

Please sign in to comment.