Skip to content

Update TransactionCell.swift #127

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
87 changes: 61 additions & 26 deletions Maskbook/Scene/Wallet/TokenDetail/View/TransactionCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 &&
Expand All @@ -310,39 +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 }

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: type.gasRatio)
let roundBehavior = NSDecimalNumberHandler(roundingMode: .plain, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
gasNetModel?.suggestedMaxFeePerGas = gwei.rounding(accordingToBehavior: roundBehavior).stringValue
}

Coordinator.main.present(
scene: .sendTransactionPopConfirm(sendConfirmViewModel: confirmModel, toAddress: toAddress, amount: amount, nonce: txToSpeedUp.nonce),
transition: .panModel(animated: true))
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)

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))
}


}

}

@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()

let confirmModel = SendConfirmViewModel(selectedToken: token, gasPrice: gasPrice * BigUInt(1.5), gasLimit: gasLimit, gasFeeNetModel: txToSpeedUp.transactionInfo?.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)
}
}