diff --git a/BlockchainSdk/Blockchains/Hedera/HederaTransactionBuilder.swift b/BlockchainSdk/Blockchains/Hedera/HederaTransactionBuilder.swift index 1eb8b51c4..27fe2ccc7 100644 --- a/BlockchainSdk/Blockchains/Hedera/HederaTransactionBuilder.swift +++ b/BlockchainSdk/Blockchains/Hedera/HederaTransactionBuilder.swift @@ -58,6 +58,8 @@ final class HederaTransactionBuilder { ) throws -> CompiledTransaction { // At the moment, we intentionally don't support custom fees for HTS tokens (HIP-18 https://hips.hedera.com/HIP/hip-18.html) let feeValue = transaction.fee.amount.value * pow(Decimal(10), transaction.fee.amount.decimals) + // Hedera fee calculation involves conversion from USD to HBar units, which ultimately results in a loss of precision. + // Therefore, the fee value is always approximate and rounding of the fee value is mandatory. let feeRoundedValue = feeValue.rounded(roundingMode: .up) let feeAmount = try Hbar(feeRoundedValue, .tinybar) diff --git a/BlockchainSdk/Blockchains/Hedera/HederaWalletManager.swift b/BlockchainSdk/Blockchains/Hedera/HederaWalletManager.swift index 5f4519c9c..85c31ca5c 100644 --- a/BlockchainSdk/Blockchains/Hedera/HederaWalletManager.swift +++ b/BlockchainSdk/Blockchains/Hedera/HederaWalletManager.swift @@ -422,7 +422,10 @@ final class HederaWalletManager: BaseManager { let (exchangeRate, doesAccountExist) = input let feeBase = doesAccountExist ? transferFeeBase : Constants.cryptoCreateServiceCostInUSD let feeValue = exchangeRate.nextHBARPerUSD * feeBase * Constants.maxFeeMultiplier - let feeAmount = Amount(with: walletManager.wallet.blockchain, value: feeValue) + // Hedera fee calculation involves conversion from USD to HBar units, which ultimately results in a loss of precision. + // Therefore, the fee value is always approximate and rounding of the fee value is mandatory. + let feeRoundedValue = feeValue.rounded(blockchain: walletManager.wallet.blockchain, roundingMode: .up) + let feeAmount = Amount(with: walletManager.wallet.blockchain, value: feeRoundedValue) let fee = Fee(feeAmount) return [fee] @@ -553,7 +556,10 @@ extension HederaWalletManager: AssetRequirementsManager { } let feeValue = tokenAssociationFeeExchangeRate * Constants.tokenAssociateServiceCostInUSD - let feeAmount = Amount.init(with: wallet.blockchain, value: feeValue) + // Hedera fee calculation involves conversion from USD to HBar units, which ultimately results in a loss of precision. + // Therefore, the fee value is always approximate and rounding of the fee value is mandatory. + let feeRoundedValue = feeValue.rounded(blockchain: wallet.blockchain, roundingMode: .up) + let feeAmount = Amount(with: wallet.blockchain, value: feeRoundedValue) return .paidTransactionWithFee(feeAmount: feeAmount) }