Skip to content

Commit

Permalink
Improve fee estimation logic for EVM when balance is insufficient
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Nov 18, 2024
1 parent 724e9ee commit ef34f51
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12967,7 +12967,7 @@
repositoryURL = "https://github.com/horizontalsystems/EvmKit.Swift";
requirement = {
kind = exactVersion;
version = 2.1.1;
version = 2.1.2;
};
};
D3604E4828F02A8B0066C366 /* XCRemoteSwiftPackageReference "Eip20Kit" */ = {
Expand Down
9 changes: 5 additions & 4 deletions UnstoppableWallet/UnstoppableWallet/Core/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,10 @@ class App {
}

func newSendEnabled(wallet: Wallet) -> Bool {
switch wallet.token.blockchainType {
case .ton: return true
default: return localStorage.newSendEnabled
}
true
// switch wallet.token.blockchainType {
// case .ton: return true
// default: return localStorage.newSendEnabled
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import MarketKit
struct EvmFeeEstimator {
private static let surchargePercent: Double = 10

private func _estimateFee(evmKitWrapper: EvmKitWrapper, transactionData: TransactionData, gasPrice: GasPrice, predefinedGasLimit: Int? = nil) async throws -> EvmFeeData {
func estimateFee(evmKitWrapper: EvmKitWrapper, transactionData: TransactionData, gasPriceData: GasPriceData, predefinedGasLimit: Int? = nil) async throws -> EvmFeeData {
let evmKit = evmKitWrapper.evmKit
let gasLimit: Int
let gasPrice = gasPriceData.userDefined

if let predefinedGasLimit {
gasLimit = predefinedGasLimit
} else {
gasLimit = try await evmKit.fetchEstimateGas(transactionData: transactionData, gasPrice: gasPrice)
do {
gasLimit = try await evmKit.fetchEstimateGas(transactionData: transactionData, gasPrice: gasPrice)
} catch {
gasLimit = try await evmKit.fetchEstimateGas(transactionData: transactionData)
}
}

let txAmount = transactionData.value
Expand Down Expand Up @@ -48,16 +53,4 @@ struct EvmFeeEstimator {

return .init(gasLimit: gasLimit, surchargedGasLimit: surchargedGasLimit, l1Fee: l1Fee)
}

func estimateFee(evmKitWrapper: EvmKitWrapper, transactionData: TransactionData, gasPriceData: GasPriceData, predefinedGasLimit _: Int? = nil) async throws -> EvmFeeData {
do {
return try await _estimateFee(evmKitWrapper: evmKitWrapper, transactionData: transactionData, gasPrice: gasPriceData.userDefined)
} catch {
if case let AppError.ethereum(reason: ethereumError) = error.convertedError, case .lowerThanBaseGasLimit = ethereumError {
return try await _estimateFee(evmKitWrapper: evmKitWrapper, transactionData: transactionData, gasPrice: gasPriceData.recommended)
} else {
throw error
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,19 @@ extension EvmSendHandler: ISendHandler {

evmFeeData = stubFeeData
transactionData = TransactionData(to: transactionData.to, value: max(0, transactionData.value - totalFee), input: transactionData.input)

if transactionData.value == 0 {
throw AppError.ethereum(reason: .insufficientBalanceWithFee)
}
} else {
evmFeeData = try await evmFeeEstimator.estimateFee(evmKitWrapper: evmKitWrapper, transactionData: transactionData, gasPriceData: gasPriceData)
let _evmFeeData = try await evmFeeEstimator.estimateFee(evmKitWrapper: evmKitWrapper, transactionData: transactionData, gasPriceData: gasPriceData)
let totalFee = _evmFeeData.totalFee(gasPrice: gasPriceData.userDefined)

evmFeeData = _evmFeeData

if evmBalance < totalFee {
throw AppError.ethereum(reason: .insufficientBalanceWithFee)
}
}
} catch {
transactionError = error
Expand Down

0 comments on commit ef34f51

Please sign in to comment.