Skip to content

Commit

Permalink
Covers the not enough funds error case. Upgrade libwallet version to …
Browse files Browse the repository at this point in the history
…0.16.14. Makes default output count = 2.
  • Loading branch information
kukabi committed Jan 19, 2021
1 parent bdfdd7d commit 03b7bdd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
34 changes: 23 additions & 11 deletions MobileWallet/Screens/Send/AddAmount/AddAmountViewController.swift
Expand Up @@ -141,14 +141,34 @@ class AddAmountViewController: UIViewController {
return
}

if totalMicroTari!.rawValue < microTariAmount.rawValue {
var fee: MicroTari
do {
fee = try wallet.estimateTxFee(
amount: microTariAmount,
gramFee: Wallet.defaultGramFee,
kernelCount: Wallet.defaultKernelCount,
outputCount: Wallet.defaultOutputCount
)
} catch {
switch error {
case WalletErrors.notEnoughFunds:
showBalanceExceeded(balance: totalMicroTari!.formatted)
continueButton.variation = .disabled
hideTxFee()
default:
break
}
return
}

if totalMicroTari!.rawValue < (microTariAmount.rawValue + fee.rawValue) {
showBalanceExceeded(balance: totalMicroTari!.formatted)
continueButton.variation = .disabled
} else {
continueButton.variation = .normal
}

showTxFee(microTariAmount)
showTxFee(fee)
}

@objc private func keypadButtonTapped(_ sender: UIButton) {
Expand Down Expand Up @@ -335,15 +355,7 @@ class AddAmountViewController: UIViewController {
warningView.isHidden = true
}

private func showTxFee(_ amount: MicroTari) {
guard let wallet = TariLib.shared.tariWallet else { return }
var fee = MicroTari(0)
do {
fee = try wallet.estimateTxFee(amount: amount, gramFee: MicroTari(100), kernelCount: 1, outputCount: 1)
} catch {
return
}

private func showTxFee(_ fee: MicroTari) {
txFeeLabel.text = fee.formattedPreciseWithOperator
if txFeeIsVisible { return }
txViewContainer.alpha = 0.0
Expand Down
Expand Up @@ -625,7 +625,12 @@ class SendingTariViewController: UIViewController {
self.txId = try wallet.sendTx(
destination: self.recipientPubKey,
amount: self.amount,
fee: wallet.estimateTxFee(amount: self.amount, gramFee: MicroTari(100), kernelCount: 1, outputCount: 1),
fee: wallet.estimateTxFee(
amount: self.amount,
gramFee: Wallet.defaultGramFee,
kernelCount: Wallet.defaultKernelCount,
outputCount: Wallet.defaultOutputCount
),
message: self.note
)
self.startListeningForWalletEvents()
Expand Down
2 changes: 2 additions & 0 deletions MobileWallet/TariLib/Wrappers/Utils/ErrorDescriptions.swift
Expand Up @@ -80,6 +80,8 @@ extension WalletErrors: LocalizedError {
return NSLocalizedString("wallet.error.cancel_non_pending_tx", comment: "Wallet error")
case .txToCancel:
return NSLocalizedString("wallet.error.fetch_txs_to_cancel", comment: "Wallet error")
case .notEnoughFunds:
return nil
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions MobileWallet/TariLib/Wrappers/Wallet.swift
Expand Up @@ -59,6 +59,7 @@ enum WalletErrors: Error {
case invalidSignatureAndNonceString
case cancelNonPendingTx
case txToCancel
case notEnoughFunds
}

struct CallbackTxResult {
Expand All @@ -73,6 +74,10 @@ class Wallet {
var dbName: String
var logPath: String

static let defaultGramFee = MicroTari(100)
static let defaultKernelCount = UInt64(1)
static let defaultOutputCount = UInt64(2)

var pointer: OpaquePointer {
return ptr
}
Expand Down Expand Up @@ -340,6 +345,9 @@ class Wallet {
let fee = withUnsafeMutablePointer(to: &errorCode, { error in
wallet_get_fee_estimate(ptr, amount.rawValue, gramFee.rawValue, kernelCount, outputCount, error)})
guard errorCode == 0 else {
if errorCode == 101 {
throw WalletErrors.notEnoughFunds
}
throw WalletErrors.generic(errorCode)
}

Expand Down
2 changes: 1 addition & 1 deletion dependencies.env
@@ -1 +1 @@
FFI_VERSION="0.16.13"
FFI_VERSION="0.16.14"

0 comments on commit 03b7bdd

Please sign in to comment.