[WIP]Get UTXOs

This commit is contained in:
Francisco Gindre 2020-12-09 20:57:23 -03:00
parent bedeb89316
commit 6b48695744
4 changed files with 83 additions and 2 deletions

View File

@ -0,0 +1,22 @@
//
// UnspentTransactionOutputDAO.swift
// ZcashLightClientKit
//
// Created by Francisco Gindre on 12/9/20.
//
import Foundation
struct UTXO: UnspentTransactionOutputEntity {
var address: String
var txid: Data
var index: Int32
var script: Data
var valueZat: Int64
var height: UInt64
}

View File

@ -0,0 +1,24 @@
//
// UnspentTransactionOutputEntity.swift
// ZcashLightClientKit
//
// Created by Francisco Gindre on 12/9/20.
//
import Foundation
public protocol UnspentTransactionOutputEntity {
var address: String { get set }
var txid: Data {get set}
var index: Int32 {get set}
var script: Data {get set}
var valueZat: Int64 {get set}
var height: UInt64 {get set}
}

View File

@ -75,8 +75,7 @@ public class LightWalletGRPCService {
requestIDProvider: .autogenerated,
requestIDHeader: nil,
cacheable: false)
}
}
}
extension LightWalletGRPCService: LightWalletService {
@ -218,6 +217,39 @@ extension LightWalletGRPCService: LightWalletService {
}
return height
}
public func fetchUTXOs(for tAddress: String, result: @escaping (Result<[UnspentTransactionOutputEntity], LightWalletServiceError>) -> Void) {
queue.async { [weak self] in
guard let self = self else { return }
let arg = GetAddressUtxosArg.with { (utxoArgs) in
utxoArgs.address = tAddress
}
var utxos = [UnspentTransactionOutputEntity]()
let response = self.compactTxStreamer.getAddressUtxosStream(arg) { (reply) in
utxos.append(
UTXO(address: tAddress,
txid: reply.txid,
index: reply.index,
script: reply.script,
valueZat: reply.valueZat,
height: UInt64(reply.valueZat)
)
)
}
do {
let status = try response.status.wait()
switch status.code {
case .ok:
result(.success(utxos))
default:
result(.failure(.mapCode(status)))
}
} catch {
result(.failure(error.mapToServiceError()))
}
}
}
}
extension Error {

View File

@ -168,4 +168,7 @@ public protocol LightWalletService {
- Returns: LightWalletServiceResponse
*/
func fetchTransaction(txId: Data, result: @escaping (Result<TransactionEntity,LightWalletServiceError>) -> Void)
func fetchUTXOs(for tAddress: String, result: @escaping(Result<[UnspentTransactionOutputEntity], LightWalletServiceError>) -> Void)
}