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

Improve fee estimation logic for EVM when balance is insufficient #6023

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
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
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