Skip to content

Commit

Permalink
IOS-8253 Show alert with available amout to send for Kaspa
Browse files Browse the repository at this point in the history
  • Loading branch information
amuraveinik committed Oct 15, 2024
1 parent bb119d7 commit 64f9d57
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
21 changes: 3 additions & 18 deletions BlockchainSdk/Blockchains/Kaspa/KaspaTransactionBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,16 @@ class KaspaTransactionBuilder {
}

func availableAmount() -> Amount {
let inputs = unspentOutputs
let inputs = Array(unspentOutputs.prefix(maxInputCount))
let availableAmountInSatoshi = inputs.reduce(0) { $0 + $1.amount }
return Amount(with: blockchain, value: Decimal(availableAmountInSatoshi) / blockchain.decimalValue)
}

func unspentOutputsCount(for amount: Amount) -> Int {
return unspentOutputs.count
}

func setUnspentOutputs(_ unspentOutputs: [BitcoinUnspentOutput]) {
let sortedOutputs = unspentOutputs.sorted {
$0.amount > $1.amount
}

self.unspentOutputs = Array(sortedOutputs.prefix(maxInputCount))
func setUnspentOutputs(_ unspentOutputs: [BitcoinUnspentOutput]) {
self.unspentOutputs = unspentOutputs.sorted { $0.amount > $1.amount }
}

func buildForSign(_ transaction: Transaction) throws -> (KaspaTransaction, [Data]) {
let availableInputValue = self.availableAmount()

guard transaction.amount.type == availableInputValue.type,
transaction.amount <= availableInputValue else {
throw WalletError.failedToBuildTx
}

let destinationAddressScript = try scriptPublicKey(address: transaction.destinationAddress).hexString.lowercased()

var outputs: [KaspaOutput] = [
Expand Down
8 changes: 7 additions & 1 deletion BlockchainSdk/Blockchains/Kaspa/KaspaWalletManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,13 @@ extension KaspaWalletManager: WithdrawalNotificationProvider {

extension KaspaWalletManager: MaximumAmountRestrictable {
func validateMaximumAmount(amount: Amount, fee: Amount) throws {
let amountAvailableToSend = txBuilder.availableAmount() - fee
var amountAvailableToSend = txBuilder.availableAmount() - fee

let change = amount - amountAvailableToSend
if change > .zeroCoin(for: wallet.blockchain), change < dustValue {
amountAvailableToSend = amountAvailableToSend - (dustValue - change)
}

if amount <= amountAvailableToSend {
return
}
Expand Down

0 comments on commit 64f9d57

Please sign in to comment.