Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Sui Presign to support sending non-native tokens #1518

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update the Sui presign process to support sending non-native tokens
aminsato committed Dec 22, 2024
commit d1fc273870c4d316564ba48fc31a494e6f8b1dec
Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@ internal class SuiApiImpl @Inject constructor(
?.jsonArray
?.mapNotNull {
val coinType = it.jsonObject["coinType"]?.jsonPrimitive?.content
if (coinType == "0x2::sui::SUI") {
if (coinType != null) {
SuiCoin(
coinObjectId = it.jsonObject["coinObjectId"]?.jsonPrimitive?.content ?: "",
version = it.jsonObject["version"]?.jsonPrimitive?.content ?: "",
60 changes: 43 additions & 17 deletions data/src/main/kotlin/com/vultisig/wallet/data/crypto/SuiHelper.kt
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import wallet.core.jni.proto.Sui
object SuiHelper {

private val coinType = CoinType.SUI
private val suiContractAddress = "0x2::sui::SUI"

private fun getPreSignedInputData(keysignPayload: KeysignPayload): ByteArray {
require(keysignPayload.coin.chain == Chain.Sui) { "Coin is not SUI" }
@@ -29,23 +30,48 @@ object SuiHelper {

val toAddress = AnyAddress(keysignPayload.toAddress, coinType)

val suiCoins = coins.map { coin ->
Sui.ObjectRef.newBuilder()
.setObjectId(coin.coinObjectId)
.setVersion(coin.version.toLong())
.setObjectDigest(coin.digest)
.build()
}

val input = Sui.SigningInput.newBuilder()
.setPaySui(
Sui.PaySui.newBuilder()
.addAllInputCoins(suiCoins)
.addAllRecipients(listOf(toAddress.description()))
.addAllAmounts(listOf(keysignPayload.toAmount.toLong()))
.build()
)
.setSigner(keysignPayload.coin.address)
val (nonSuiObjectRef, suiObjectRefs) = coins.partition { it.coinType.equals(keysignPayload.coin.contractAddress) && !it.coinType.equals(suiContractAddress) }
.let { (notSui, sui) ->
notSui.map {
Sui.ObjectRef.newBuilder()
.setObjectId(it.coinObjectId)
.setVersion(it.version.toLong())
.setObjectDigest(it.digest)
.build()
} to sui.filter { it.coinType.equals(suiContractAddress) }.map {
Sui.ObjectRef.newBuilder()
.setObjectId(it.coinObjectId)
.setVersion(it.version.toLong())
.setObjectDigest(it.digest)
.build()
}
}
val gascoin =
coins.filter { it.coinType == suiContractAddress && it.balance.toBigInteger() > referenceGasPrice }
.minByOrNull { it.balance.toBigInteger() }

val input = if (nonSuiObjectRef.isNotEmpty()) {
Sui.SigningInput.newBuilder()
.setPay(
Sui.Pay.newBuilder()
.setGas(suiObjectRefs.first { it.objectId == gascoin?.coinObjectId })
.addAllInputCoins(nonSuiObjectRef)
.addAllRecipients(listOf(toAddress.description()))
.addAllAmounts(listOf(keysignPayload.toAmount.toLong()))
.build()
)

} else {
Sui.SigningInput.newBuilder()
.setPaySui(
Sui.PaySui.newBuilder()
.addAllInputCoins(suiObjectRefs)
.addAllRecipients(listOf(toAddress.description()))
.addAllAmounts(listOf(keysignPayload.toAmount.toLong()))
.build()
)

}.setSigner(keysignPayload.coin.address)
.setGasBudget(3000000L)
.setReferenceGasPrice(referenceGasPrice.toLong())
.build()