Skip to content

Commit

Permalink
Added total network fee option when changing gas price/limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Scoff committed Dec 9, 2017
1 parent 7001cf6 commit 4b66fd9
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 19 deletions.
13 changes: 12 additions & 1 deletion Trust/Models/EthereumUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@

import Foundation

enum EthereumUnit: Int64 {
public enum EthereumUnit: Int64 {
case wei = 1
case kwei = 1_000
case gwei = 1_000_000_000
case ether = 1_000_000_000_000_000_000
}

extension EthereumUnit {
var name: String {
switch self {
case .wei: return "Wei"
case .kwei: return "Kwei"
case .gwei: return "Gwei"
case .ether: return "Ether"
}
}
}

//https://github.com/ethereumjs/ethereumjs-units/blob/master/units.json
5 changes: 5 additions & 0 deletions Trust/Settings/Types/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ public struct Constants {
// support
public static let supportEmail = "[email protected]"
}

public struct UnitConfiguration {
public static let gasPriceUnit: EthereumUnit = .gwei
public static let gasFeeUnit: EthereumUnit = .ether
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class ConfigureTransactionViewController: FormViewController {

let configuration: TransactionConfiguration
let config: Config
private let fullFormatter = EtherNumberFormatter.full

struct Values {
static let gasPrice = "gasPrice"
static let gasLimit = "gasLimit"
static let totalFee = "totalFee"
}

private struct Constant {
Expand All @@ -32,13 +34,25 @@ class ConfigureTransactionViewController: FormViewController {
return ConfigureTransactionViewModel(config: self.config)
}()

var gasPriceRow: SliderRow? {
private var gasPriceRow: SliderRow? {
return form.rowBy(tag: Values.gasPrice) as? SliderRow
}
var gasLimitRow: SliderRow? {
private var gasLimitRow: SliderRow? {
return form.rowBy(tag: Values.gasLimit) as? SliderRow
}
private let gasPriceUnit: EthereumUnit = .gwei
private var totalFeeRow: TextRow? {
return form.rowBy(tag: Values.totalFee) as? TextRow
}

private var gasLimit: BigInt {
return BigInt(String(Int(gasLimitRow?.value ?? 0)), radix: 10) ?? BigInt()
}
private var gasPrice: BigInt {
return fullFormatter.number(from: String(Int(gasPriceRow?.value ?? 1)), units: UnitConfiguration.gasPriceUnit) ?? BigInt()
}
private var totalFee: BigInt {
return gasPrice * gasLimit
}

weak var delegate: ConfigureTransactionViewControllerDelegate?

Expand All @@ -52,7 +66,7 @@ class ConfigureTransactionViewController: FormViewController {
super.init(nibName: nil, bundle: nil)

navigationItem.title = viewModel.title
navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("generic.save", value: "Save", comment: ""), style: .done, target: self, action: #selector(self.save))
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(save))
}

required init?(coder aDecoder: NSCoder) {
Expand All @@ -62,7 +76,7 @@ class ConfigureTransactionViewController: FormViewController {
override func viewDidLoad() {
super.viewDidLoad()

let gasPriceGwei = EtherNumberFormatter.full.string(from: configuration.speed.gasPrice, units: gasPriceUnit)
let gasPriceGwei = EtherNumberFormatter.full.string(from: configuration.speed.gasPrice, units: UnitConfiguration.gasPriceUnit)

form = Section()

Expand All @@ -79,6 +93,9 @@ class ConfigureTransactionViewController: FormViewController {
$0.displayValueFor = { (rowValue: Float?) in
return "\(Int(rowValue ?? 1)) (Gwei)"
}
$0.onChange { [unowned self] _ in
self.recalculateTotalFee()
}
}

+++ Section(
Expand All @@ -94,15 +111,27 @@ class ConfigureTransactionViewController: FormViewController {
$0.displayValueFor = { (rowValue: Float?) in
return "\(Int(rowValue ?? 1))"
}
$0.onChange { [unowned self] _ in
self.recalculateTotalFee()
}
}
}

@objc func save() {
let gasPrice = EtherNumberFormatter.full.number(from: String(Int(gasPriceRow?.value ?? 1)), units: gasPriceUnit) ?? BigInt()
+++ Section()

<<< TextRow(Values.totalFee) {
$0.title = NSLocalizedString("configureTransaction.totalNetworkFee", value: "Total network fee", comment: "")
$0.disabled = true
}

let gasLimit = BigInt(String(Int(gasLimitRow?.value ?? 0)), radix: 10) ?? BigInt()
let totalFee = gasPrice * gasLimit
recalculateTotalFee()
}

func recalculateTotalFee() {
totalFeeRow?.value = "\(fullFormatter.string(from: totalFee)) \(config.server.symbol)"
totalFeeRow?.updateCell()
}

@objc func save() {
guard gasLimit <= ConfigureTransaction.gasLimitMax else {
return displayError(error: ConfigureTransactionError.gasLimitTooHigh)
}
Expand Down
32 changes: 27 additions & 5 deletions Trust/Transfer/ViewControllers/ConfirmPaymentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class ConfirmPaymentViewController: UIViewController {
stackViewController.view.backgroundColor = viewModel.backgroundColor

navigationItem.title = viewModel.title
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(edit))
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(edit))

reloadView()

Expand All @@ -114,10 +114,32 @@ class ConfirmPaymentViewController: UIViewController {
viewModel: headerViewModel
),
TransactionAppearance.divider(color: Colors.lightGray, alpha: 0.3),
TransactionAppearance.item(title: NSLocalizedString("confirmPayment.from", value: "From", comment: ""), subTitle: session.account.address.address),
TransactionAppearance.item(title: NSLocalizedString("confirmPayment.to", value: "To", comment: ""), subTitle: viewModel.addressText),
TransactionAppearance.item(title: NSLocalizedString("confirmPayment.gasLimit", value: "Gas Limit", comment: ""), subTitle: viewModel.gasLimiText),
TransactionAppearance.item(title: NSLocalizedString("confirmPayment.gasFee", value: "Gas Fee", comment: ""), subTitle: viewModel.feeText),
TransactionAppearance.item(
title: viewModel.paymentFromTitle,
subTitle: session.account.address.address
),
TransactionAppearance.item(
title: viewModel.paymentToTitle,
subTitle: viewModel.paymentToText
),
TransactionAppearance.item(
title: viewModel.gasLimitTitle,
subTitle: viewModel.gasLimitText
) { [unowned self] _, _, _ in
self.edit()
},
TransactionAppearance.item(
title: viewModel.gasPriceTitle,
subTitle: viewModel.gasPriceText
) { [unowned self] _, _, _ in
self.edit()
},
TransactionAppearance.item(
title: viewModel.feeTitle,
subTitle: viewModel.feeText
) { [unowned self] _, _, _ in
self.edit()
},
]

for item in items {
Expand Down
40 changes: 37 additions & 3 deletions Trust/Transfer/ViewModels/ConfirmPaymentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,55 @@ struct ConfirmPaymentViewModel {
return fullFormatter.string(from: transaction.value)
}

var addressText: String {
var paymentFromTitle: String {
return NSLocalizedString("confirmPayment.from", value: "From", comment: "")
}

var paymentToTitle: String {
return NSLocalizedString("confirmPayment.to", value: "To", comment: "")
}
var paymentToText: String {
return transaction.address.address
}

var gasPriceTitle: String {
return NSLocalizedString("confirmPayment.gasPrice", value: "Gas Price", comment: "")
}

var gasPriceText: String {
let unit = UnitConfiguration.gasPriceUnit
let amount = fullFormatter.string(from: configuration.speed.gasPrice, units: UnitConfiguration.gasPriceUnit)
return String(
format: "%@ %@",
amount,
unit.name
)
}

var feeTitle: String {
return NSLocalizedString("confirmPayment.gasFee", value: "Network Fee", comment: "")
}

var feeText: String {
let fee = fullFormatter.string(from: totalFee)
let feeAndSymbol = fee.description + " \(config.server.symbol)"
let feeAndSymbol = String(
format: "%@ %@",
fee.description,
config.server.symbol
)

let warningFee = BigInt(EthereumUnit.ether.rawValue) / BigInt(20)
guard totalFee <= warningFee else {
return feeAndSymbol + " - WARNING. HIGH FEE."
}
return feeAndSymbol
}

var gasLimiText: String {
var gasLimitTitle: String {
return NSLocalizedString("confirmPayment.gasLimit", value: "Gas Limit", comment: "")
}

var gasLimitText: String {
return gasLimit.description
}
}

0 comments on commit 4b66fd9

Please sign in to comment.