Skip to content

Commit

Permalink
IOS-7805 Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
amuraveinik committed Sep 10, 2024
1 parent fa40b2e commit df32053
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
17 changes: 17 additions & 0 deletions BlockchainSdk/Blockchains/Kaspa/KaspaTransactionBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ class KaspaTransactionBuilder {
return KaspaTransactionData(inputs: inputs, outputs: builtTransaction.outputs)
}

func buildForMassCalculation(transaction: Transaction) throws -> KaspaTransactionData {
// let dummySignature = Data(repeating: 1, count: 65)
let builtTransaction = try buildForSign(transaction).0

let inputs = builtTransaction.inputs.enumerated().map { (index, input) in
KaspaInput(
previousOutpoint: KaspaPreviousOutpoint(
transactionId: input.transactionHash,
index: input.outputIndex
),
signatureScript: ""
)
}

return KaspaTransactionData(inputs: inputs, outputs: builtTransaction.outputs)
}

private func amount(from transaction: Transaction) -> UInt64 {
return ((transaction.amount.value * blockchain.decimalValue) as NSDecimalNumber).uint64Value
}
Expand Down
57 changes: 27 additions & 30 deletions BlockchainSdk/Blockchains/Kaspa/KaspaWalletManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class KaspaWalletManager: BaseManager, WalletManager {
.eraseToAnyPublisher()
}

let blockchain = wallet.blockchain
let decimalValue = wallet.blockchain.decimalValue
let source = wallet.address

let feePerUtxo: Decimal = 0.0001
let fee = feePerUtxo * Decimal(numberOfUtxos)

Expand All @@ -88,42 +92,35 @@ class KaspaWalletManager: BaseManager, WalletManager {
utxoCount: numberOfUtxos
)

let dummySignature = Data(repeating: 1, count: 65)
let transaction = Transaction(
amount: amount,
fee: Fee(Amount.zeroCoin(for: wallet.blockchain)),
sourceAddress: wallet.address,
fee: Fee(Amount.zeroCoin(for: blockchain)),
sourceAddress: source,
destinationAddress: destination,
changeAddress: wallet.address
changeAddress: source
)
guard let kaspaTransaction = try? txBuilder.buildForSign(transaction).0 else {
return .anyFail(error: WalletError.failedToGetFee)
}
let signatures = Array(repeating: dummySignature, count: kaspaTransaction.inputs.count)
let transactionData = txBuilder.buildForSend(transaction: kaspaTransaction, signatures: signatures)

let blockchain = wallet.blockchain
let decimalValue = wallet.blockchain.decimalValue

return networkService.mass(data: transactionData)
.zip(networkService.feeEstimate())
.map { mass, feeEstimate in
let priorityFee = Decimal(feeEstimate.priorityBucket.feerate * mass.mass) / decimalValue
let normalFees = feeEstimate.normalBuckets.map { bucket in
Decimal(bucket.feerate * mass.mass) / decimalValue
}
let lowFees = feeEstimate.lowBuckets.map { bucket in
Decimal(bucket.feerate * mass.mass) / decimalValue
}

return ([priorityFee] + normalFees + lowFees).map { fee in
Fee(
Amount(with: blockchain, value: fee),
parameters: params
)
}
return Result {
try txBuilder.buildForMassCalculation(transaction: transaction)
}
.publisher
.withWeakCaptureOf(networkService)
.flatMap { networkService, transactionData in
networkService.mass(data: transactionData)
.zip(networkService.feeEstimate())
}
.map { mass, feeEstimate in
let buckets = [feeEstimate.priorityBucket] + feeEstimate.normalBuckets + feeEstimate.lowBuckets

return buckets.prefix(3).map { bucket in
let value = Decimal(bucket.feerate * mass.mass) / decimalValue
return Fee(
Amount(with: blockchain, value: value),
parameters: params
)
}
.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}

private func updateWallet(_ info: KaspaAddressInfo) {
Expand Down

0 comments on commit df32053

Please sign in to comment.