2021-06-28 14:46:53 +03:00
|
|
|
// Copyright © 2021 Encrypted Ink. All rights reserved.
|
|
|
|
|
|
|
|
import Cocoa
|
2021-07-02 23:00:00 +03:00
|
|
|
import WalletConnect
|
2021-07-03 22:48:20 +03:00
|
|
|
import Kingfisher
|
2021-06-28 14:46:53 +03:00
|
|
|
|
|
|
|
class ApproveTransactionViewController: NSViewController {
|
|
|
|
|
2021-07-03 23:23:55 +03:00
|
|
|
@IBOutlet weak var gweiLabel: NSTextField!
|
2021-06-28 14:46:53 +03:00
|
|
|
@IBOutlet weak var titleLabel: NSTextField!
|
|
|
|
@IBOutlet var metaTextView: NSTextView!
|
|
|
|
@IBOutlet weak var okButton: NSButton!
|
|
|
|
@IBOutlet weak var speedSlider: NSSlider!
|
|
|
|
@IBOutlet weak var slowSpeedLabel: NSTextField!
|
|
|
|
@IBOutlet weak var fastSpeedLabel: NSTextField!
|
2021-07-03 22:48:20 +03:00
|
|
|
@IBOutlet weak var peerNameLabel: NSTextField!
|
|
|
|
@IBOutlet weak var peerLogoImageView: NSImageView! {
|
|
|
|
didSet {
|
|
|
|
peerLogoImageView.wantsLayer = true
|
|
|
|
peerLogoImageView.layer?.backgroundColor = NSColor.systemGray.withAlphaComponent(0.5).cgColor
|
|
|
|
}
|
|
|
|
}
|
2021-06-28 14:46:53 +03:00
|
|
|
|
2021-06-28 16:25:56 +03:00
|
|
|
private let gasService = GasService.shared
|
2021-07-04 18:05:50 +03:00
|
|
|
private let priceService = PriceService.shared
|
2021-06-28 18:12:39 +03:00
|
|
|
private var currentGasInfo: GasService.Info?
|
2021-06-28 16:15:53 +03:00
|
|
|
private var transaction: Transaction!
|
|
|
|
private var completion: ((Transaction?) -> Void)!
|
2021-06-28 18:12:39 +03:00
|
|
|
private var didEnableSpeedConfiguration = false
|
2021-07-03 22:48:20 +03:00
|
|
|
private var peerMeta: WCPeerMeta?
|
2021-06-28 14:46:53 +03:00
|
|
|
|
2021-07-02 23:00:00 +03:00
|
|
|
static func with(transaction: Transaction, peerMeta: WCPeerMeta?, completion: @escaping (Transaction?) -> Void) -> ApproveTransactionViewController {
|
2021-06-28 14:46:53 +03:00
|
|
|
let new = instantiate(ApproveTransactionViewController.self)
|
2021-06-28 16:15:53 +03:00
|
|
|
new.transaction = transaction
|
2021-06-28 14:46:53 +03:00
|
|
|
new.completion = completion
|
2021-07-03 22:48:20 +03:00
|
|
|
new.peerMeta = peerMeta
|
2021-06-28 14:46:53 +03:00
|
|
|
return new
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2021-07-04 21:53:14 +03:00
|
|
|
priceService.update()
|
2021-06-28 16:15:53 +03:00
|
|
|
titleLabel.stringValue = Strings.sendTransaction
|
2021-06-28 18:12:39 +03:00
|
|
|
setSpeedConfigurationViews(enabled: false)
|
2021-06-28 16:15:53 +03:00
|
|
|
updateInterface()
|
|
|
|
prepareTransaction()
|
2021-07-03 22:48:20 +03:00
|
|
|
if let peer = peerMeta {
|
|
|
|
peerNameLabel.stringValue = peer.name
|
|
|
|
if let urlString = peer.icons.first, let url = URL(string: urlString) {
|
|
|
|
peerLogoImageView.kf.setImage(with: url) { [weak peerLogoImageView] result in
|
|
|
|
if case .success = result {
|
|
|
|
peerLogoImageView?.layer?.backgroundColor = NSColor.clear.cgColor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-28 16:15:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private func prepareTransaction() {
|
|
|
|
Ethereum.prepareTransaction(transaction) { [weak self] updated in
|
|
|
|
self?.transaction = updated
|
|
|
|
self?.updateInterface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateInterface() {
|
2021-06-28 18:12:39 +03:00
|
|
|
enableSpeedConfigurationIfNeeded()
|
2021-07-04 20:35:06 +03:00
|
|
|
let meta = transaction.description(ethPrice: priceService.currentPrice)
|
2021-06-28 16:15:53 +03:00
|
|
|
if metaTextView.string != meta {
|
|
|
|
metaTextView.string = meta
|
|
|
|
}
|
2021-07-03 23:42:07 +03:00
|
|
|
if didEnableSpeedConfiguration, let gwei = transaction.gasPriceGwei {
|
|
|
|
gweiLabel.stringValue = "\(gwei) Gwei"
|
|
|
|
}
|
2021-06-28 14:46:53 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 18:12:39 +03:00
|
|
|
private func enableSpeedConfigurationIfNeeded() {
|
|
|
|
guard !didEnableSpeedConfiguration else { return }
|
|
|
|
let newGasInfo = gasService.currentInfo
|
|
|
|
guard transaction.hasFee, let gasInfo = newGasInfo else { return }
|
|
|
|
didEnableSpeedConfiguration = true
|
|
|
|
currentGasInfo = gasInfo
|
|
|
|
speedSlider.doubleValue = transaction.currentGasInRelationTo(info: gasInfo)
|
|
|
|
setSpeedConfigurationViews(enabled: true)
|
2021-06-28 14:46:53 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 18:12:39 +03:00
|
|
|
private func setSpeedConfigurationViews(enabled: Bool) {
|
|
|
|
slowSpeedLabel.alphaValue = enabled ? 1 : 0.5
|
|
|
|
fastSpeedLabel.alphaValue = enabled ? 1 : 0.5
|
|
|
|
speedSlider.isEnabled = enabled
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:46:53 +03:00
|
|
|
@IBAction func sliderValueChanged(_ sender: NSSlider) {
|
2021-06-28 18:12:39 +03:00
|
|
|
guard let gasInfo = currentGasInfo else { return }
|
|
|
|
transaction.setGasPrice(value: sender.doubleValue, inRelationTo: gasInfo)
|
|
|
|
updateInterface()
|
2021-06-28 14:46:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func actionButtonTapped(_ sender: Any) {
|
2021-06-28 16:15:53 +03:00
|
|
|
completion(transaction)
|
2021-06-28 14:46:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func cancelButtonTapped(_ sender: NSButton) {
|
2021-06-28 16:15:53 +03:00
|
|
|
completion(nil)
|
2021-06-28 14:46:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|