Skip to content

Commit

Permalink
Ensure positive xc transfer amount when calculating fees
Browse files Browse the repository at this point in the history
  • Loading branch information
valentunn committed Nov 29, 2024
1 parent 3c7b3b9 commit 3e1a383
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ interface AssetTransferBase : AssetTransferDirection {
val amountPlanks: Balance
}

fun AssetTransferBase.replaceAmount(newAmount: Balance): AssetTransferBase {
return AssetTransferBase(recipient, originChain, originChainAsset, destinationChain, destinationChainAsset, feePaymentCurrency, newAmount)
}

// TODO this is too specialized for this module
interface AssetTransfer : AssetTransferBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.novafoundation.nova.common.data.network.runtime.binding.Weight
import io.novafoundation.nova.common.utils.Modules
import io.novafoundation.nova.common.utils.flatMap
import io.novafoundation.nova.common.utils.instanceOf
import io.novafoundation.nova.common.utils.isZero
import io.novafoundation.nova.common.utils.orZero
import io.novafoundation.nova.common.utils.transformResult
import io.novafoundation.nova.common.utils.wrapInResult
Expand All @@ -23,6 +24,7 @@ import io.novafoundation.nova.feature_wallet_api.data.network.blockhain.assets.b
import io.novafoundation.nova.feature_wallet_api.data.network.blockhain.assets.events.tryDetectDeposit
import io.novafoundation.nova.feature_wallet_api.data.network.blockhain.assets.tranfers.AssetTransferBase
import io.novafoundation.nova.feature_wallet_api.data.network.blockhain.assets.tranfers.AssetTransfersValidationSystem
import io.novafoundation.nova.feature_wallet_api.data.network.blockhain.assets.tranfers.replaceAmount
import io.novafoundation.nova.feature_wallet_api.data.network.blockhain.types.Balance
import io.novafoundation.nova.feature_wallet_api.data.network.crosschain.CrossChainTransactor
import io.novafoundation.nova.feature_wallet_api.data.network.crosschain.CrossChainWeigher
Expand Down Expand Up @@ -115,7 +117,12 @@ class RealCrossChainTransactor(
feePaymentCurrency = transfer.feePaymentCurrency
)
) {
crossChainTransfer(configuration, transfer, crossChainFee = Balance.ZERO)
// Starting from XCM V3, Fungible assets cannot have zero amount, this restriction is checked on Decoding level
// So even fee calculation will fail with zero amount on V3+
// We only modify it for fees - the positive amount check for the transfer itself happens as a part of validation process
val alwaysPositiveTransfer = transfer.ensurePositiveAmount()

crossChainTransfer(configuration, alwaysPositiveTransfer, crossChainFee = Balance.ZERO)
}
}

Expand Down Expand Up @@ -391,4 +398,12 @@ class RealCrossChainTransactor(

return accountId.accountIdToMultiLocation()
}

private fun AssetTransferBase.ensurePositiveAmount(): AssetTransferBase {
return if (amountPlanks.isZero) {
replaceAmount(newAmount = BigInteger.ONE)
} else {
this
}
}
}

0 comments on commit 3e1a383

Please sign in to comment.