@@ -16,63 +16,63 @@ import Alamofire
1616import Combine
1717import CommonKit
1818
19- final class ERC20WalletService : WalletCoreProtocol , @unchecked Sendable {
19+ final class ERC20WalletService : WalletCoreProtocol , ERC20GasAlgorithmComputable , @unchecked Sendable {
2020 // MARK: - Constants
2121 let addressRegex = try ! NSRegularExpression ( pattern: " ^0x[a-fA-F0-9]{40}$ " )
2222
2323 var minBalance : Decimal = 0
2424 var minAmount : Decimal = 0
2525
2626 var tokenSymbol : String {
27- return token. symbol
27+ token. symbol
2828 }
2929
3030 var tokenName : String {
31- return token. name
31+ token. name
3232 }
3333
3434 var tokenLogo : UIImage {
35- return token. logo
35+ token. logo
3636 }
3737
3838 static var tokenNetworkSymbol : String {
39- return " ERC20 "
39+ " ERC20 "
4040 }
4141
4242 var consistencyMaxTime : Double {
43- return 1200
43+ 1200
4444 }
4545
4646 var tokenContract : String {
47- return token. contractAddress
47+ token. contractAddress
4848 }
4949
5050 var tokenUniqueID : String {
5151 Self . tokenNetworkSymbol + tokenSymbol + tokenContract
5252 }
5353
5454 var defaultVisibility : Bool {
55- return token. defaultVisibility
55+ token. defaultVisibility
5656 }
5757
5858 var defaultOrdinalLevel : Int ? {
59- return token. defaultOrdinalLevel
59+ token. defaultOrdinalLevel
6060 }
6161
6262 var richMessageType : String {
63- return Self . richMessageType
63+ Self . richMessageType
6464 }
6565
6666 var qqPrefix : String {
67- return EthWalletService . qqPrefix
67+ EthWalletService . qqPrefix
6868 }
6969
7070 var isSupportIncreaseFee : Bool {
71- return true
71+ true
7272 }
7373
7474 var isIncreaseFeeEnabled : Bool {
75- return increaseFeeService. isIncreaseFeeEnabled ( for: tokenUniqueID)
75+ increaseFeeService. isIncreaseFeeEnabled ( for: tokenUniqueID)
7676 }
7777
7878 var nodeGroups : [ NodeGroup ] {
@@ -95,7 +95,19 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {
9595 private( set) var isWarningGasPrice = false
9696
9797 var isTransactionFeeValid : Bool {
98- return ethWallet? . balance ?? 0 > transactionFee
98+ ethWallet? . balance ?? 0 > transactionFee
99+ }
100+
101+ var reliabilityGasPricePercent : BigUInt {
102+ BigUInt ( token. reliabilityGasPricePercent)
103+ }
104+
105+ var reliabilityGasLimitPercent : BigUInt {
106+ BigUInt ( token. reliabilityGasLimitPercent)
107+ }
108+
109+ var increasedGasPricePercent : Decimal {
110+ token. increasedGasPricePercent
99111 }
100112
101113 static let transferGas : Decimal = 21000
@@ -283,42 +295,41 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {
283295 }
284296
285297 func calculateFee( for address: EthereumAddress ? = nil ) async {
286- let priceRaw = try ? await getGasPrices ( )
287- let gasLimitRaw = try ? await getGasLimit ( to: address)
288-
289- var price = priceRaw ?? BigUInt ( token. defaultGasPriceGwei) . toWei ( )
290- var gasLimit = gasLimitRaw ?? BigUInt ( token. defaultGasLimit)
291-
292- let pricePercent = price * BigUInt( token. reliabilityGasPricePercent) / 100
293- let gasLimitPercent = gasLimit * BigUInt( token. reliabilityGasLimitPercent) / 100
294-
295- price = priceRaw == nil
296- ? price
297- : price + pricePercent
298-
299- gasLimit = gasLimitRaw == nil
300- ? gasLimit
301- : gasLimit + gasLimitPercent
298+ // Setting initial
299+ async let pricePriceAsync = getGasPrices ( )
300+ async let gasLimitAsync = getGasLimit ( to: address)
301+ var gasPriceCoeficient : Decimal = 1
302+ if isIncreaseFeeEnabled {
303+ gasPriceCoeficient += token. increasedGasPricePercent / 100
304+ }
302305
303- var newFee = ( price * gasLimit) . asDecimal ( exponent : EthWalletService . currencyExponent )
306+ let gasPrice , gasLimit : BigUInt
304307
305- newFee = isIncreaseFeeEnabled
306- ? newFee * defaultIncreaseFee
307- : newFee
308-
309- guard transactionFee != newFee else { return }
310-
311- transactionFee = newFee
312- let incGasPrice = UInt64 ( price. asDouble ( ) * defaultIncreaseFee. doubleValue)
313-
314- gasPrice = isIncreaseFeeEnabled
315- ? BigUInt ( integerLiteral: incGasPrice)
316- : price
317-
318- isWarningGasPrice = gasPrice >= BigUInt ( token. warningGasPriceGwei) . toWei ( )
319- self . gasLimit = gasLimit
308+ // Getting gas data
309+ do {
310+ let ( gasPriceFromChain, gasLimitFromChain) = try await ( pricePriceAsync, gasLimitAsync)
311+ try Task . checkCancellation ( )
312+ gasPrice = gasPriceFromChain
313+ gasLimit = gasLimitFromChain
314+ } catch {
315+ gasPrice = BigUInt ( token. defaultGasPriceGwei) . toWei ( )
316+ gasLimit = BigUInt ( token. defaultGasLimit)
317+ }
320318
321- NotificationCenter . default. post ( name: transactionFeeUpdated, object: self , userInfo: nil )
319+ // Updating localy
320+ updateGasAndFee (
321+ gasPrice: gasPrice,
322+ gasLimit: gasLimit,
323+ gasPriceCoeficient: gasPriceCoeficient
324+ ) { [ weak self] gasPrice, gasLimit, newFee in
325+ guard let self else { return }
326+ self . gasPrice = gasPrice
327+ self . gasLimit = gasLimit
328+ guard transactionFee != newFee else { return }
329+ transactionFee = newFee
330+ isWarningGasPrice = gasPrice >= BigUInt ( token. warningGasPriceGwei) . toWei ( )
331+ NotificationCenter . default. post ( name: transactionFeeUpdated, object: self , userInfo: nil )
332+ }
322333 }
323334
324335 func validate( address: String ) -> AddressValidationResult {
0 commit comments