Skip to content

Commit

Permalink
swap fixeds
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Oct 14, 2024
1 parent ec35e30 commit 65e44d7
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,7 +66,7 @@ class BillingManager(
suspend fun getProducts(
productIds: List<String>,
productType: String = ProductType.INAPP
) = billingClient.ready {
) = billingClient.ready { client ->
if (productIds.isEmpty()) {
return@ready
}
Expand All @@ -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 {
Expand All @@ -90,6 +91,10 @@ class BillingManager(
}
}

fun setEmptyProducts() {
_productsFlow.value = emptyList()
}

private suspend fun getPendingPurchases(client: BillingClient): List<Purchase> {
val params = QueryPurchasesParams.newBuilder()
.setProductType(ProductType.INAPP)
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -64,7 +66,8 @@ class BatteryRefillViewModel(
.map { it.purchases }
.filter { it.isNotEmpty() }

private val promoStateFlow = MutableStateFlow<PromoState>(PromoState.Default)
private val _promoStateFlow = MutableStateFlow<PromoState>(PromoState.Default)
private val promoStateFlow = _promoStateFlow.asStateFlow()

private val purchaseInProgress = MutableStateFlow(false)

Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 65e44d7

Please sign in to comment.