From 8ec63717c8a463f1de50da75547eccedf246c4d1 Mon Sep 17 00:00:00 2001 From: hydraCy <461189296@qq.com> Date: Fri, 22 Apr 2022 16:18:16 +0800 Subject: [PATCH 1/2] Update TransactionCell.swift --- .../TokenDetail/View/TransactionCell.swift | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/Maskbook/Scene/Wallet/TokenDetail/View/TransactionCell.swift b/Maskbook/Scene/Wallet/TokenDetail/View/TransactionCell.swift index 003305fe..55b3dc52 100644 --- a/Maskbook/Scene/Wallet/TokenDetail/View/TransactionCell.swift +++ b/Maskbook/Scene/Wallet/TokenDetail/View/TransactionCell.swift @@ -314,8 +314,22 @@ extension TransactionCell { guard let gasPrice = txToSpeedUp.transactionInfo?.gasPrice else { return } guard let gasLimit = txToSpeedUp.transactionInfo?.gaslimit else { return } - let confirmModel = SendConfirmViewModel(selectedToken: txToSpeedUp.transactionInfo?.token, gasPrice: gasPrice * BigUInt(1.2), gasLimit: gasLimit, gasFeeNetModel: txToSpeedUp.transactionInfo?.gasNetModel) + var gasNetModel = txToSpeedUp.transactionInfo?.gasNetModel + + if let maxFeePerGas = txToSpeedUp.transactionInfo?.gasNetModel?.suggestedMaxFeePerGas{ + let gwei = NSDecimalNumber(string: maxFeePerGas).multiplying(by: NSDecimalNumber(string: "1.2")) + let roundBehavior = NSDecimalNumberHandler(roundingMode: .plain, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) + gasNetModel?.suggestedMaxFeePerGas = gwei.rounding(accordingToBehavior: roundBehavior).stringValue + } + if let maxPriorityFee = txToSpeedUp.transactionInfo?.gasNetModel?.suggestedMaxPriorityFeePerGas{ + let gwei = NSDecimalNumber(string: maxPriorityFee).multiplying(by: NSDecimalNumber(string: "1.2")) + let roundBehavior = NSDecimalNumberHandler(roundingMode: .plain, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) + gasNetModel?.suggestedMaxPriorityFeePerGas = gwei.rounding(accordingToBehavior: roundBehavior).stringValue + } + + let confirmModel = SendConfirmViewModel(selectedToken: txToSpeedUp.transactionInfo?.token, gasPrice: gasPrice * BigUInt(12) / BigUInt(10), gasLimit: gasLimit, gasFeeNetModel: gasNetModel) + Coordinator.main.present( scene: .sendTransactionPopConfirm(sendConfirmViewModel: confirmModel, toAddress: toAddress, amount: amount, nonce: txToSpeedUp.nonce), transition: .panModel(animated: true)) @@ -337,8 +351,22 @@ extension TransactionCell { guard let gasLimit = txToSpeedUp.transactionInfo?.gaslimit else { return } let token = WalletAssetManager.shared.getDefaultMainToken() + + var gasNetModel = txToSpeedUp.transactionInfo?.gasNetModel + + if let maxFeePerGas = txToSpeedUp.transactionInfo?.gasNetModel?.suggestedMaxFeePerGas{ + let gwei = NSDecimalNumber(string: maxFeePerGas).multiplying(by: NSDecimalNumber(string: "1.5")) + let roundBehavior = NSDecimalNumberHandler(roundingMode: .plain, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) + gasNetModel?.suggestedMaxFeePerGas = gwei.rounding(accordingToBehavior: roundBehavior).stringValue + } + + if let maxPriorityFee = txToSpeedUp.transactionInfo?.gasNetModel?.suggestedMaxPriorityFeePerGas{ + let gwei = NSDecimalNumber(string: maxPriorityFee).multiplying(by: NSDecimalNumber(string: "1.5")) + let roundBehavior = NSDecimalNumberHandler(roundingMode: .plain, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) + gasNetModel?.suggestedMaxPriorityFeePerGas = gwei.rounding(accordingToBehavior: roundBehavior).stringValue + } - let confirmModel = SendConfirmViewModel(selectedToken: token, gasPrice: gasPrice * BigUInt(1.5), gasLimit: gasLimit, gasFeeNetModel: txToSpeedUp.transactionInfo?.gasNetModel) + let confirmModel = SendConfirmViewModel(selectedToken: token, gasPrice: gasPrice * BigUInt(15) / BigUInt(10), gasLimit: gasLimit, gasFeeNetModel: gasNetModel) Coordinator.main.present( scene: .sendTransactionPopConfirm(sendConfirmViewModel: confirmModel, toAddress: toAddress, amount: "0", nonce: txToSpeedUp.nonce), From e29904c67d0fb712733fa6e17b1b652e98624576 Mon Sep 17 00:00:00 2001 From: hydraCy <461189296@qq.com> Date: Wed, 27 Apr 2022 16:06:35 +0800 Subject: [PATCH 2/2] Update TransactionCell.swift --- .../TokenDetail/View/TransactionCell.swift | 95 ++++++++++--------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/Maskbook/Scene/Wallet/TokenDetail/View/TransactionCell.swift b/Maskbook/Scene/Wallet/TokenDetail/View/TransactionCell.swift index 55b3dc52..845a05c5 100644 --- a/Maskbook/Scene/Wallet/TokenDetail/View/TransactionCell.swift +++ b/Maskbook/Scene/Wallet/TokenDetail/View/TransactionCell.swift @@ -66,6 +66,31 @@ extension TransactionHistory.TransactionType { } class TransactionCell: UITableViewCell { + + enum actionType { + case speedUp + case cancel + + var gasRatio: NSDecimalNumber { + switch self { + case .speedUp: + return NSDecimalNumber(value: 1.2) + + case .cancel: + return NSDecimalNumber(value: 1.5) + } + } + + var gasRatioBigUInt: Int { + switch self { + case .speedUp: + return 12 + + case .cancel: + return 15 + } + } + } private var txHash: String? private var typeIconView: UIImageView = { @@ -299,8 +324,8 @@ class TransactionCell: UITableViewCell { } extension TransactionCell { - @objc - func speedUpClicked(_ sender: UIButton) { + + func gasAction(type: actionType) { let result = PendTransactionManager.shared.pendTransactions.value.filter { $0.txHash == self.txHash && $0.address == maskUserDefaults.defaultAccountAddress && @@ -310,67 +335,49 @@ extension TransactionCell { if let txToSpeedUp = result.first { guard let toAddress = txToSpeedUp.transactionInfo?.toAddress else { return } + guard let toAddressCancel = maskUserDefaults.defaultAccountAddress else { return } guard let amount = txToSpeedUp.transactionInfo?.amount else { return } guard let gasPrice = txToSpeedUp.transactionInfo?.gasPrice else { return } guard let gasLimit = txToSpeedUp.transactionInfo?.gaslimit else { return } var gasNetModel = txToSpeedUp.transactionInfo?.gasNetModel - if let maxFeePerGas = txToSpeedUp.transactionInfo?.gasNetModel?.suggestedMaxFeePerGas{ - let gwei = NSDecimalNumber(string: maxFeePerGas).multiplying(by: NSDecimalNumber(string: "1.2")) + if let maxFeePerGas = txToSpeedUp.transactionInfo?.gasNetModel?.suggestedMaxFeePerGas { + let gwei = NSDecimalNumber(string: maxFeePerGas).multiplying(by: type.gasRatio) let roundBehavior = NSDecimalNumberHandler(roundingMode: .plain, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) gasNetModel?.suggestedMaxFeePerGas = gwei.rounding(accordingToBehavior: roundBehavior).stringValue } - if let maxPriorityFee = txToSpeedUp.transactionInfo?.gasNetModel?.suggestedMaxPriorityFeePerGas{ - let gwei = NSDecimalNumber(string: maxPriorityFee).multiplying(by: NSDecimalNumber(string: "1.2")) + if let maxPriorityFee = txToSpeedUp.transactionInfo?.gasNetModel?.suggestedMaxPriorityFeePerGas { + let gwei = NSDecimalNumber(string: maxPriorityFee).multiplying(by: type.gasRatio) let roundBehavior = NSDecimalNumberHandler(roundingMode: .plain, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) gasNetModel?.suggestedMaxPriorityFeePerGas = gwei.rounding(accordingToBehavior: roundBehavior).stringValue } + let confirmModel = SendConfirmViewModel(selectedToken: txToSpeedUp.transactionInfo?.token, gasPrice: gasPrice * BigUInt(type.gasRatioBigUInt) / BigUInt(10), gasLimit: gasLimit, gasFeeNetModel: gasNetModel) - let confirmModel = SendConfirmViewModel(selectedToken: txToSpeedUp.transactionInfo?.token, gasPrice: gasPrice * BigUInt(12) / BigUInt(10), gasLimit: gasLimit, gasFeeNetModel: gasNetModel) + switch type { + case .speedUp: + Coordinator.main.present( + scene: .sendTransactionPopConfirm(sendConfirmViewModel: confirmModel, toAddress: toAddress, amount: amount, nonce: txToSpeedUp.nonce), + transition: .panModel(animated: true)) + case .cancel: + Coordinator.main.present( + scene: .sendTransactionPopConfirm(sendConfirmViewModel: confirmModel, toAddress: toAddressCancel, amount: "0", nonce: txToSpeedUp.nonce), + transition: .panModel(animated: true)) + } - Coordinator.main.present( - scene: .sendTransactionPopConfirm(sendConfirmViewModel: confirmModel, toAddress: toAddress, amount: amount, nonce: txToSpeedUp.nonce), - transition: .panModel(animated: true)) + } + } @objc - func cancelClicked(_ sender: UIButton) { - let result = PendTransactionManager.shared.pendTransactions.value.filter { - $0.txHash == self.txHash && - $0.address == maskUserDefaults.defaultAccountAddress && - $0.networkId == maskUserDefaults.network.networkId - } - - if let txToSpeedUp = result.first { + func speedUpClicked(_ sender: UIButton) { + gasAction(type: .speedUp) + } - guard let toAddress = maskUserDefaults.defaultAccountAddress else { return } - guard let gasPrice = txToSpeedUp.transactionInfo?.gasPrice else { return } - guard let gasLimit = txToSpeedUp.transactionInfo?.gaslimit else { return } - - let token = WalletAssetManager.shared.getDefaultMainToken() - - var gasNetModel = txToSpeedUp.transactionInfo?.gasNetModel - - if let maxFeePerGas = txToSpeedUp.transactionInfo?.gasNetModel?.suggestedMaxFeePerGas{ - let gwei = NSDecimalNumber(string: maxFeePerGas).multiplying(by: NSDecimalNumber(string: "1.5")) - let roundBehavior = NSDecimalNumberHandler(roundingMode: .plain, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) - gasNetModel?.suggestedMaxFeePerGas = gwei.rounding(accordingToBehavior: roundBehavior).stringValue - } - - if let maxPriorityFee = txToSpeedUp.transactionInfo?.gasNetModel?.suggestedMaxPriorityFeePerGas{ - let gwei = NSDecimalNumber(string: maxPriorityFee).multiplying(by: NSDecimalNumber(string: "1.5")) - let roundBehavior = NSDecimalNumberHandler(roundingMode: .plain, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) - gasNetModel?.suggestedMaxPriorityFeePerGas = gwei.rounding(accordingToBehavior: roundBehavior).stringValue - } - - let confirmModel = SendConfirmViewModel(selectedToken: token, gasPrice: gasPrice * BigUInt(15) / BigUInt(10), gasLimit: gasLimit, gasFeeNetModel: gasNetModel) - - Coordinator.main.present( - scene: .sendTransactionPopConfirm(sendConfirmViewModel: confirmModel, toAddress: toAddress, amount: "0", nonce: txToSpeedUp.nonce), - transition: .panModel(animated: true)) - } + @objc + func cancelClicked(_ sender: UIButton) { + gasAction(type: .cancel) } }