Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Rebasing PR
Browse files Browse the repository at this point in the history
  • Loading branch information
chitowncrispy committed Oct 24, 2019
1 parent 3dcf085 commit 54986bd
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Library/Generated/strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ internal enum L10n {
internal static let memoHeadline = L10n.tr("Localizable", "scene.send.memo_headline")
/// Send
internal static let sendButton = L10n.tr("Localizable", "scene.send.send_button")
/// Send amount too small
internal static let sendAmountTooSmall = L10n.tr("Localizable", "scene.send.send_amount_too_small")
/// Sending...
internal static let sending = L10n.tr("Localizable", "scene.send.sending")
/// Payment Successful
Expand Down
22 changes: 16 additions & 6 deletions Library/Scenes/ModalDetail/Send/LoadingAmountView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import Bond
import Foundation
import ReactiveKit
import SwiftBTC
import SwiftLnd

enum Loadable<E> {
enum Loadable<T, E> {
case loading
case element(E)
case element(T)
case error(E)
}

final class LoadingAmountView: UIView {
Expand All @@ -29,7 +31,7 @@ final class LoadingAmountView: UIView {
}
}

init(loadable: Observable<Loadable<Satoshi?>>) {
init(loadable: Observable<Loadable<Satoshi?, LndApiError>>) {
amountLabel = UILabel(frame: CGRect.zero)
activityIndicator = UIActivityIndicatorView(style: .white)

Expand Down Expand Up @@ -63,7 +65,7 @@ final class LoadingAmountView: UIView {
fatalError("init(coder:) has not been implemented")
}

private func updateLoadable(_ loadable: Loadable<Satoshi?>) {
private func updateLoadable(_ loadable: Loadable<Satoshi?, LndApiError>) {
switch loadable {
case .loading:
activityIndicator.isHidden = false
Expand All @@ -80,8 +82,16 @@ final class LoadingAmountView: UIView {
} else {
amountLabel.text = "-"
}

case .error(let lndApiError):
activityIndicator.isHidden = true
amountLabel.isHidden = false
let dustErrorString = "rpc error: code = Unknown desc = transaction output is dust"

if lndApiError.localizedDescription == dustErrorString {
amountLabel.text = L10n.Scene.Send.sendAmountTooSmall
} else {
amountLabel.text = "-"
}
}

}
}
28 changes: 19 additions & 9 deletions Library/Scenes/ModalDetail/Send/SendViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class SendViewModel: NSObject {
}
}

let fee = Observable<Loadable<Satoshi?>>(.loading)
let fee = Observable<Loadable<Satoshi?, LndApiError>>(.loading)

let method: SendMethod

Expand Down Expand Up @@ -162,14 +162,24 @@ final class SendViewModel: NSObject {
private func fetchFee() {
guard let amount = amount else { return }

let feeCompletion = { [weak self] (result: (amount: Satoshi, fee: Satoshi?)) -> Void in
guard
let self = self,
result.amount == self.amount
else { return }

self.fee.value = .element(result.fee)
self.updateIsUIEnabled()
let feeCompletion = { [weak self] (result: Result<(amount: Satoshi, fee: Satoshi?), LndApiError>) -> Void in
switch result {
case .success(let result):
guard
let self = self,
result.amount == self.amount
else { return }

self.fee.value = .element(result.fee)
self.updateIsUIEnabled()
case .failure(let lndApiError):
guard
let self = self
else { return }

self.fee.value = .error(lndApiError)
self.updateIsUIEnabled()
}
}

switch method {
Expand Down
5 changes: 3 additions & 2 deletions Library/Views/OnChainFeeView/OnChainFeeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Bond
import Foundation
import SwiftBTC
import SwiftLnd

protocol OnChainFeeViewDelegate: class {
func confirmationTargetChanged(to confirmationTarget: Int)
Expand Down Expand Up @@ -35,7 +36,7 @@ final class OnChainFeeView: UIView {

weak var delegate: OnChainFeeViewDelegate?

init(loadable: Observable<Loadable<Satoshi?>>) {
init(loadable: Observable<Loadable<Satoshi?, LndApiError>>) {
super.init(frame: .zero)
setup(loadable: loadable)
}
Expand All @@ -44,7 +45,7 @@ final class OnChainFeeView: UIView {
fatalError("init(coder:) has not been implemented")
}

private func setup(loadable: Observable<Loadable<Satoshi?>>) {
private func setup(loadable: Observable<Loadable<Satoshi?, LndApiError>>) {
Bundle.library.loadNibNamed("OnChainFeeView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
Expand Down
1 change: 1 addition & 0 deletions Library/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"scene.send.address_headline" = "To:";
"scene.send.memo_headline" = "Memo:";
"scene.send.sending" = "Sending...";
"scene.send.send_amount_too_small" = "Send amount too small for fee estimation";
"scene.send.lightning.title" = "Send Lightning Payment";
"scene.send.on_chain.title" = "Send On-Chain";
"scene.send.on_chain.fee" = "Fee:";
Expand Down
14 changes: 9 additions & 5 deletions Lightning/Services/TransactionService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,23 @@ public final class TransactionService {
/// Fee methods can be called multiple times at once. So the completion
/// includes amount to match on the caller side that the returned result is
/// for the correct amount.
public typealias FeeApiCompletion = ((amount: Satoshi, fee: Satoshi?)) -> Void
public typealias FeeApiCompletion = (Result<(amount: Satoshi, fee: Satoshi?), LndApiError>) -> Void

public func lightningFees(for paymentRequest: PaymentRequest, amount: Satoshi, completion: @escaping FeeApiCompletion) {
api.route(destination: paymentRequest.destination, amount: amount) { result in
let totalFees = (try? result.get())?.totalFees
completion((amount: amount, fee: totalFees))
completion(.success((amount: amount, fee: totalFees)))
}
}

public func onChainFees(address: BitcoinAddress, amount: Satoshi, confirmationTarget: Int, completion: @escaping FeeApiCompletion) {
api.estimateFees(address: address, amount: amount, confirmationTarget: confirmationTarget) {
let fees = (try? $0.get())?.total
completion((amount: amount, fee: fees))
api.estimateFees(address: address, amount: amount, confirmationTarget: confirmationTarget) { result in
switch result {
case .success(let feeEstimate):
completion(.success((amount: amount, fee: feeEstimate.total)))
case .failure(let lndApiError):
completion(.failure(lndApiError))
}
}
}

Expand Down

0 comments on commit 54986bd

Please sign in to comment.