Skip to content

Commit

Permalink
fix jetton & nft transfer amount (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokin0andrey authored Oct 21, 2024
1 parent 9b41b21 commit 21fc470
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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
import com.tonapps.blockchain.ton.tlb.MessageData
import com.tonapps.extensions.toByteArray
import com.tonapps.icu.Coins
import com.tonapps.ledger.ton.TonPayloadFormat
Expand All @@ -24,11 +23,9 @@ import com.tonapps.wallet.data.account.entities.WalletEntity
import com.tonapps.wallet.data.events.CommentEncryption
import org.ton.api.pk.PrivateKeyEd25519
import org.ton.api.pub.PublicKeyEd25519
import org.ton.bitstring.BitString
import org.ton.block.AddrStd
import org.ton.block.StateInit
import org.ton.cell.Cell
import org.ton.cell.CellBuilder
import org.ton.cell.CellBuilder.Companion.beginCell
import org.ton.contract.wallet.WalletTransfer
import org.ton.contract.wallet.WalletTransferBuilder
Expand Down Expand Up @@ -106,7 +103,8 @@ data class TransferEntity(
.storeStringTail(comment)
.endCell()
} else {
privateKey ?: throw IllegalArgumentException("Private key required for encrypted comment")
privateKey
?: throw IllegalArgumentException("Private key required for encrypted comment")
return CommentEncryption.encryptComment(
comment = comment,
myPublicKey = privateKey.publicKey(),
Expand All @@ -120,17 +118,18 @@ data class TransferEntity(
private fun getWalletTransfer(
privateKey: PrivateKeyEd25519?,
excessesAddress: AddrStd,
jettonAmount: Coins?
jettonAmount: Coins?,
jettonTransferAmount: Coins,
): WalletTransfer {
val builder = WalletTransferBuilder()
builder.bounceable = bounceable
builder.body = body(privateKey, excessesAddress, jettonAmount)
builder.sendMode = sendMode
if (isNft) {
builder.coins = coins
builder.coins = jettonTransferAmount.toGrams()
builder.destination = AddrStd.parse(nftAddress!!)
} else if (!isTon) {
builder.coins = TRANSFER_PRICE
builder.coins = jettonTransferAmount.toGrams()
builder.destination = AddrStd.parse(token.walletAddress)
} else {
builder.coins = coins
Expand All @@ -144,10 +143,18 @@ data class TransferEntity(
privateKey: PrivateKeyEd25519?,
excessesAddress: AddrStd,
additionalGifts: List<WalletTransfer>,
jettonAmount: Coins?
jettonAmount: Coins?,
jettonTransferAmount: Coins
): Array<WalletTransfer> {
val gifts = mutableListOf<WalletTransfer>()
gifts.add(getWalletTransfer(privateKey, excessesAddress, jettonAmount))
gifts.add(
getWalletTransfer(
privateKey,
excessesAddress,
jettonAmount,
jettonTransferAmount
)
)
gifts.addAll(additionalGifts)
return gifts.toTypedArray()
}
Expand All @@ -158,6 +165,7 @@ data class TransferEntity(
excessesAddress: AddrStd? = null,
additionalGifts: List<WalletTransfer> = emptyList(),
jettonAmount: Coins? = null,
jettonTransferAmount: Coins
): Cell {
return contract.createTransferUnsignedBody(
validUntil = validUntil,
Expand All @@ -167,12 +175,13 @@ data class TransferEntity(
excessesAddress = excessesAddress ?: contract.address,
additionalGifts = additionalGifts,
jettonAmount = jettonAmount,
jettonTransferAmount = jettonTransferAmount,
),
internalMessage = internalMessage,
)
}

fun getLedgerTransaction(): Transaction? {
fun getLedgerTransaction(jettonTransferAmount: Coins): Transaction? {
if (wallet.type != Wallet.Type.Ledger) {
return null
}
Expand All @@ -191,7 +200,7 @@ data class TransferEntity(
)
)
} else if (!isTon) {
builder.setCoins(TRANSFER_PRICE)
builder.setCoins(jettonTransferAmount.toGrams())
builder.setDestination(AddrStd.parse(token.walletAddress))
builder.setPayload(
TonPayloadFormat.JettonTransfer(
Expand Down Expand Up @@ -219,29 +228,11 @@ data class TransferEntity(
return builder.build()
}

fun signedHash(privateKey: PrivateKeyEd25519): BitString {
return BitString(privateKey.sign(getUnsignedBody(privateKey).hash()))
}

private fun messageBodyWithSign(
signature: BitString
): Cell {
val unsignedBody = getUnsignedBody()
return contract.signedBody(signature, unsignedBody)
}

fun transferMessage(
signature: BitString
): Cell {
val signedBody = messageBodyWithSign(signature)
return contract.createTransferMessageCell(contract.address, seqno, signedBody)
}

fun transferMessage(signedBody: Cell): Cell {
return contract.createTransferMessageCell(contract.address, seqno, signedBody)
}

private fun body(privateKey: PrivateKeyEd25519?, excessesAddress: AddrStd, jettonAmount: Coins?): Cell? {
private fun body(
privateKey: PrivateKeyEd25519?,
excessesAddress: AddrStd,
jettonAmount: Coins?
): Cell? {
if (isNft) {
return nftBody(privateKey, excessesAddress)
} else if (!isTon) {
Expand Down Expand Up @@ -274,23 +265,24 @@ data class TransferEntity(
)
}

fun toSignedMessage(
privateKey: PrivateKeyEd25519 = fakePrivateKey,
fun signForEstimation(
jettonAmount: Coins? = null,
internalMessage: Boolean,
excessesAddress: AddrStd? = null,
additionalGifts: List<WalletTransfer> = emptyList()
additionalGifts: List<WalletTransfer> = emptyList(),
jettonTransferAmount: Coins
): Cell {
return contract.createTransferMessageCell(
address = contract.address,
privateKey = privateKey,
privateKey = fakePrivateKey,
seqNo = seqno,
unsignedBody = getUnsignedBody(
privateKey = privateKey,
privateKey = fakePrivateKey,
internalMessage = internalMessage,
excessesAddress = excessesAddress,
additionalGifts = additionalGifts,
jettonAmount = jettonAmount,
jettonTransferAmount = jettonTransferAmount,
),
)
}
Expand All @@ -313,7 +305,7 @@ data class TransferEntity(
forwardPayload = beginCell().storeOpCode(TONOpCode.GASLESS).endCell(),
customPayload = tokenPayload?.customPayload,
)
builder.coins = TRANSFER_PRICE
builder.coins = POINT_ONE_TON.toGrams()
builder.destination = AddrStd.parse(token.walletAddress)

return builder.build()
Expand Down Expand Up @@ -396,8 +388,9 @@ data class TransferEntity(

companion object {

val BASE_FORWARD_AMOUNT = Coins.of(0.1, 9)
private val TRANSFER_PRICE = BASE_FORWARD_AMOUNT.toGrams()
val BASE_FORWARD_AMOUNT = Coins.of(0.05, 9)
val ONE_TON = Coins.of(1, 9)
val POINT_ONE_TON = Coins.of(0.1, 9)

fun newWalletQueryId(): BigInteger {
return try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,17 @@
package com.tonapps.tonkeeper.ui.screen.nft

import android.app.Application
import android.content.Context
import androidx.lifecycle.viewModelScope
import com.tonapps.blockchain.ton.TonNetwork
import com.tonapps.blockchain.ton.TonTransferHelper
import com.tonapps.blockchain.ton.extensions.EmptyPrivateKeyEd25519
import com.tonapps.blockchain.ton.extensions.base64
import com.tonapps.blockchain.ton.extensions.toAccountId
import com.tonapps.icu.Coins
import com.tonapps.tonkeeper.core.entities.SendMetadataEntity
import com.tonapps.tonkeeper.core.entities.TransferEntity
import com.tonapps.tonkeeper.extensions.toGrams
import com.tonapps.tonkeeper.ui.base.BaseWalletVM
import com.tonapps.tonkeeper.ui.screen.send.main.helper.SendNftHelper
import com.tonapps.tonkeeper.ui.screen.send.transaction.SendTransactionScreen
import com.tonapps.wallet.api.API
import com.tonapps.wallet.api.entity.TokenEntity
import com.tonapps.wallet.data.account.AccountRepository
import com.tonapps.wallet.data.account.entities.WalletEntity
import com.tonapps.wallet.data.collectibles.entities.NftEntity
import com.tonapps.wallet.data.core.entity.RawMessageEntity
import com.tonapps.wallet.data.core.entity.SignRequestEntity
import com.tonapps.wallet.data.settings.SettingsRepository
import com.tonapps.wallet.data.settings.entities.TokenPrefsEntity
import com.tonapps.wallet.data.token.TokenRepository
import com.tonapps.wallet.data.token.entities.AccountTokenEntity
import com.tonapps.wallet.localization.Localization
import io.tonapi.models.Account
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.ton.block.AddrStd
import java.math.BigInteger

class NftViewModel(
app: Application,
Expand Down
Loading

0 comments on commit 21fc470

Please sign in to comment.