From 65e44d7aeea662bbab36a8fd6c1f6da7224663bd Mon Sep 17 00:00:00 2001 From: polstianka Date: Mon, 14 Oct 2024 09:34:16 -0700 Subject: [PATCH] swap fixeds --- .../tonapps/wallet/api/entity/ConfigEntity.kt | 2 +- .../tonkeeper/billing/BillingManager.kt | 9 +++- .../tonkeeper/extensions/RawMessageEntity.kt | 43 +++++++++++++++---- .../recharge/BatteryRechargeViewModel.kt | 2 +- .../battery/refill/BatteryRefillViewModel.kt | 23 +++++----- .../blockchain/ton/extensions/CellSlice.kt | 12 ++++++ 6 files changed, 68 insertions(+), 23 deletions(-) diff --git a/apps/wallet/api/src/main/java/com/tonapps/wallet/api/entity/ConfigEntity.kt b/apps/wallet/api/src/main/java/com/tonapps/wallet/api/entity/ConfigEntity.kt index b01932c9..f6479668 100644 --- a/apps/wallet/api/src/main/java/com/tonapps/wallet/api/entity/ConfigEntity.kt +++ b/apps/wallet/api/src/main/java/com/tonapps/wallet/api/entity/ConfigEntity.kt @@ -100,7 +100,7 @@ data class ConfigEntity( iapPackages = json.optJSONArray("iap_packages")?.let { array -> (0 until array.length()).map { IAPPackageEntity(array.getJSONObject(it)) } } ?: emptyList(), - burnZeroDomain = json.optString("burnZeroDomain", "tonkeeper-zero.ton") + burnZeroDomain = json.optString("burnZeroDomain", "UQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJKZ") // tonkeeper-zero.ton ) constructor() : this( diff --git a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/billing/BillingManager.kt b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/billing/BillingManager.kt index b375d619..97f40798 100644 --- a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/billing/BillingManager.kt +++ b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/billing/BillingManager.kt @@ -2,6 +2,7 @@ package com.tonapps.tonkeeper.billing import android.app.Activity import android.content.Context +import android.util.Log import com.android.billingclient.api.BillingClient import com.android.billingclient.api.BillingClient.ProductType import com.android.billingclient.api.BillingFlowParams @@ -65,7 +66,7 @@ class BillingManager( suspend fun getProducts( productIds: List, productType: String = ProductType.INAPP - ) = billingClient.ready { + ) = billingClient.ready { client -> if (productIds.isEmpty()) { return@ready } @@ -81,7 +82,7 @@ class BillingManager( .setProductList(productList) .build() - billingClient.queryProductDetailsAsync(params) { billingResult, productDetailsList -> + client.queryProductDetailsAsync(params) { billingResult, productDetailsList -> if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) { _productsFlow.value = productDetailsList } else { @@ -90,6 +91,10 @@ class BillingManager( } } + fun setEmptyProducts() { + _productsFlow.value = emptyList() + } + private suspend fun getPendingPurchases(client: BillingClient): List { val params = QueryPurchasesParams.newBuilder() .setProductType(ProductType.INAPP) diff --git a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/extensions/RawMessageEntity.kt b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/extensions/RawMessageEntity.kt index 974726d7..ac2c93bb 100644 --- a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/extensions/RawMessageEntity.kt +++ b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/extensions/RawMessageEntity.kt @@ -1,28 +1,56 @@ package com.tonapps.tonkeeper.extensions -import android.util.Log import com.tonapps.blockchain.ton.TONOpCode import com.tonapps.blockchain.ton.TonTransferHelper import com.tonapps.blockchain.ton.extensions.loadAddress import com.tonapps.blockchain.ton.extensions.loadCoins +import com.tonapps.blockchain.ton.extensions.loadMaybeAddress import com.tonapps.blockchain.ton.extensions.loadMaybeRef import com.tonapps.blockchain.ton.extensions.loadOpCode import com.tonapps.blockchain.ton.extensions.storeAddress import com.tonapps.blockchain.ton.extensions.storeCoins import com.tonapps.blockchain.ton.extensions.storeOpCode +import com.tonapps.ledger.ton.remainingRefs 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.cell.CellSlice import org.ton.contract.wallet.WalletTransfer import org.ton.contract.wallet.WalletTransferBuilder -import org.ton.tlb.loadTlb -import org.ton.tlb.storeTlb -private fun RawMessageEntity.rebuildBodyWithCustomExcessesAccount( +private fun rebuildJettonWithCustomExcessesAccount( + payload: Cell, + slice: CellSlice, + builder: CellBuilder, + excessesAddress: AddrStd +): Cell { + try { + builder + .storeOpCode(TONOpCode.JETTON_TRANSFER) + .storeUInt(slice.loadUInt(64), 64) + .storeCoins(slice.loadCoins()) + .storeAddress(slice.loadAddress()) + + slice.loadMaybeAddress() + + while (slice.remainingRefs > 0) { + val forwardCell = slice.loadRef() + builder.storeRef(rebuildBodyWithCustomExcessesAccount(forwardCell, excessesAddress)) + } + return builder + .storeAddress(excessesAddress) + .storeBits(slice.loadBits(slice.remainingBits)) + .endCell() + } catch (e: Throwable) { + return payload + } +} + +private fun rebuildBodyWithCustomExcessesAccount( + payload: Cell, excessesAddress: AddrStd ): Cell { val slice = payload.beginParse() @@ -40,14 +68,13 @@ private fun RawMessageEntity.rebuildBodyWithCustomExcessesAccount( slice.loadAddress() } slice.endParse() - builder .storeBit(true) .storeAddress(excessesAddress) .endCell() } TONOpCode.NFT_TRANSFER -> payload - TONOpCode.JETTON_TRANSFER -> payload + TONOpCode.JETTON_TRANSFER -> rebuildJettonWithCustomExcessesAccount(payload, slice, builder, excessesAddress) else -> payload } } @@ -93,7 +120,7 @@ fun RawMessageEntity.getWalletTransfer( builder.stateInit = stateInit ?: newStateInit builder.destination = address builder.body = if (excessesAddress != null) { - rebuildBodyWithCustomExcessesAccount(excessesAddress) + rebuildBodyWithCustomExcessesAccount(payload, excessesAddress) } else if (newCustomPayload != null) { rebuildJettonTransferWithCustomPayload(newCustomPayload) } else { diff --git a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeViewModel.kt b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeViewModel.kt index 4f59698f..eb353102 100644 --- a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeViewModel.kt +++ b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeViewModel.kt @@ -176,7 +176,7 @@ class BatteryRechargeViewModel( uiItems.addAll(uiItemsPacks(packs, selectedPackType, customAmount)) - if (true) { // !api.config.batteryPromoDisable + if (!api.config.batteryPromoDisable) { uiItems.add(Item.Space) uiItems.add(Item.Promo(promoState)) } diff --git a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/refill/BatteryRefillViewModel.kt b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/refill/BatteryRefillViewModel.kt index 0152cc34..99a2cf03 100644 --- a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/refill/BatteryRefillViewModel.kt +++ b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/refill/BatteryRefillViewModel.kt @@ -2,6 +2,7 @@ package com.tonapps.tonkeeper.ui.screen.battery.refill import android.app.Activity import android.app.Application +import android.util.Log import androidx.lifecycle.viewModelScope import com.android.billingclient.api.ProductDetails import com.android.billingclient.api.Purchase @@ -38,6 +39,7 @@ import io.batteryapi.models.AndroidBatteryPurchaseRequestPurchasesInner import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.flowOn @@ -64,7 +66,8 @@ class BatteryRefillViewModel( .map { it.purchases } .filter { it.isNotEmpty() } - private val promoStateFlow = MutableStateFlow(PromoState.Default) + private val _promoStateFlow = MutableStateFlow(PromoState.Default) + private val promoStateFlow = _promoStateFlow.asStateFlow() private val purchaseInProgress = MutableStateFlow(false) @@ -81,7 +84,7 @@ class BatteryRefillViewModel( uiItems.add(uiItemBattery(batteryBalance, api.config)) uiItems.add(Item.Space) - if (true) { // !api.config.batteryPromoDisable + if (!api.config.batteryPromoDisable) { uiItems.add(Item.Promo(promoState)) uiItems.add(Item.Space) } @@ -134,16 +137,14 @@ class BatteryRefillViewModel( init { viewModelScope.launch(Dispatchers.IO) { - if (environment.isGooglePlayAvailable) { - billingManager.getProducts(api.config.iapPackages.map { it.productId }) - } + billingManager.getProducts(api.config.iapPackages.map { it.productId }) val appliedPromo = batteryRepository.getAppliedPromo(wallet.testnet) if (appliedPromo.isNullOrBlank()) { - promoStateFlow.tryEmit(PromoState.Default) + _promoStateFlow.value = PromoState.Default } else { - promoStateFlow.tryEmit(PromoState.Applied(appliedPromo)) + _promoStateFlow.value = PromoState.Applied(appliedPromo) } } @@ -286,11 +287,11 @@ class BatteryRefillViewModel( viewModelScope.launch(Dispatchers.IO) { if (promo.isEmpty()) { batteryRepository.setAppliedPromo(wallet.testnet, null) - promoStateFlow.tryEmit(PromoState.Default) + _promoStateFlow.value = PromoState.Default return@launch } val initialPromo = if (isInitial) promo else null - promoStateFlow.tryEmit(PromoState.Loading(initialPromo = initialPromo)) + _promoStateFlow.value = PromoState.Loading(initialPromo = initialPromo) try { if (isInitial) { delay(2000) @@ -299,10 +300,10 @@ class BatteryRefillViewModel( api.batteryVerifyPurchasePromo(wallet.testnet, promo) api.batteryApplyPromoCode(token, wallet.testnet, promo) batteryRepository.setAppliedPromo(wallet.testnet, promo) - promoStateFlow.tryEmit(PromoState.Applied(promo)) + _promoStateFlow.value = PromoState.Applied(promo) } catch (_: Exception) { batteryRepository.setAppliedPromo(wallet.testnet, null) - promoStateFlow.tryEmit(PromoState.Error) + _promoStateFlow.value = PromoState.Error } } } diff --git a/lib/blockchain/src/main/java/com/tonapps/blockchain/ton/extensions/CellSlice.kt b/lib/blockchain/src/main/java/com/tonapps/blockchain/ton/extensions/CellSlice.kt index 13f5b955..69edaed1 100644 --- a/lib/blockchain/src/main/java/com/tonapps/blockchain/ton/extensions/CellSlice.kt +++ b/lib/blockchain/src/main/java/com/tonapps/blockchain/ton/extensions/CellSlice.kt @@ -1,6 +1,7 @@ package com.tonapps.blockchain.ton.extensions import com.tonapps.blockchain.ton.TONOpCode +import org.ton.bigint.BigInt import org.ton.block.Coins import org.ton.block.MsgAddress import org.ton.block.MsgAddressInt @@ -21,6 +22,17 @@ fun CellSlice.loadMaybeRef(): Cell? { return loadRef() } +fun CellSlice.loadMaybeAddress(): MsgAddress? { + return when (val type = preloadUInt(2)) { + BigInt.valueOf(2) -> loadAddress() + BigInt.valueOf(0) -> { + bitsPosition += 2 + null + } + else -> throw RuntimeException("Invalid address type: $type") + } +} + fun CellSlice.loadAddress(): MsgAddressInt { // loadTlb(MsgAddressInt.tlbCodec()) return loadTlb(MsgAddressInt)