Skip to content

Commit

Permalink
Add queryReceiptAsync, queryRecord, and queryRecordAsync to Transaction
Browse files Browse the repository at this point in the history
Remove executeForReceipt from Transaction and TransactionBuilder
  • Loading branch information
qtbeee committed Nov 14, 2019
1 parent 0b8bffe commit e1d8d9d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 54 deletions.
9 changes: 6 additions & 3 deletions Examples/CreateFile/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ let client = clientFromEnvironment(eventLoopGroup: eventLoopGroup)

let publicKey = Ed25519PrivateKey(ProcessInfo.processInfo.environment["OPERATOR_KEY"]!)!.publicKey

let receipt = try! FileCreateTransaction(client: client)
let tx = FileCreateTransaction(client: client)
.addKey(publicKey)
.setContents("This is a test")
.setMemo("File Create Example - Swift SDK")
.setMaxTransactionFee(1_000_000_000)
.executeForReceipt()
.get()
.build()

try! tx.execute().get()

let receipt = try! tx.queryReceipt().get()

print("File created: \(receipt.fileId!)")
104 changes: 62 additions & 42 deletions Sources/Hedera/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,44 +68,44 @@ public class Transaction {
inner
}

func executeAndWaitFor<T>(mapResponse: (TransactionReceipt) -> T) -> Result<T, HederaError> {
let startTime = Date()
var attempt: UInt8 = 0

// There's no point asking for the receipt of a transaction that failed to go through
switch execute() {
case .failure(let error):
return .failure(error)
default:
break
}

sleep(receiptInitialDelay)

while true {
attempt += 1
// func executeAndWaitFor<T>(_ client: Client, mapResponse: (TransactionReceipt) -> T) -> Result<T, HederaError> {
// let startTime = Date()
// var attempt: UInt8 = 0

// // There's no point asking for the receipt of a transaction that failed to go through
// switch execute() {
// case .failure(let error):
// return .failure(error)
// default:
// break
// }

// sleep(receiptInitialDelay)

// while true {
// attempt += 1

let receipt = queryReceipt()
switch receipt {
case .success(let receipt):
let receiptStatus = receipt.status

if Int(receiptStatus) == Proto_ResponseCodeEnum.unknown.rawValue ||
receiptStatus == Proto_ResponseCodeEnum.ok.rawValue {
// stop trying if the delay will put us over `validDuration`
guard let delayUs = getReceiptDelayUs(startTime: startTime, attempt: attempt) else {
return .failure(HederaError(message: "executeForReceipt timed out"))
}

usleep(delayUs)
} else {
return .success(mapResponse(receipt))
}
case .failure(let error):
return .failure(error)
}
}
}
// let receipt = queryReceipt(client)
// switch receipt {
// case .success(let receipt):
// let receiptStatus = receipt.status

// if Int(receiptStatus) == Proto_ResponseCodeEnum.unknown.rawValue ||
// receiptStatus == Proto_ResponseCodeEnum.ok.rawValue {
// // stop trying if the delay will put us over `validDuration`
// guard let delayUs = getReceiptDelayUs(startTime: startTime, attempt: attempt) else {
// return .failure(HederaError(message: "executeForReceipt timed out"))
// }

// usleep(delayUs)
// } else {
// return .success(mapResponse(receipt))
// }
// case .failure(let error):
// return .failure(error)
// }
// }
// }

func getReceiptDelayUs(startTime: Date, attempt: UInt8) -> UInt32? {
// exponential backoff algorithm:
Expand Down Expand Up @@ -191,15 +191,35 @@ public class Transaction {
}
}

public func executeForReceipt() -> Result<TransactionReceipt, HederaError> {
executeAndWaitFor { $0 }
}

public func queryReceipt() -> Result<TransactionReceipt, HederaError> {
guard let client = client else { return .failure(HederaError(message: "client must not be nil")) }

return TransactionReceiptQuery(client: client)
.setTransactionId(txId)
.setTransaction(txId)
.execute()
}

public func queryReceiptAsync() -> EventLoopFuture<Result<TransactionReceipt, HederaError>> {
guard let client = client else { fatalError("client must not be nil") }

return TransactionReceiptQuery(client: client)
.setTransaction(txId)
.executeAsync()
}

public func queryRecord() -> Result<TransactionRecord, HederaError> {
guard let client = client else { return .failure(HederaError(message: "client must not be nil")) }

return TransactionRecordQuery(client: client)
.setTransaction(txId)
.execute()
}

public func queryRecordAsync() -> EventLoopFuture<Result<TransactionRecord, HederaError>> {
guard let client = client else { fatalError("client must not be nil") }

return TransactionRecordQuery(client: client)
.setTransaction(txId)
.executeAsync()
}
}
8 changes: 0 additions & 8 deletions Sources/Hedera/TransactionBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,4 @@ public class TransactionBuilder {

return transaction
}

public func execute() -> Result<TransactionId, HederaError> {
build().execute()
}

public func executeForReceipt() -> Result<TransactionReceipt, HederaError> {
build().executeForReceipt()
}
}
2 changes: 1 addition & 1 deletion Sources/Hedera/TransactionReceiptQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ public class TransactionReceiptQuery: QueryBuilder<TransactionReceipt> {
override var needsPayment: Bool { false }

@discardableResult
public func setTransactionId(_ id: TransactionId) -> Self {
public func setTransaction(_ id: TransactionId) -> Self {
body.transactionGetReceipt.transactionID = id.toProto()
return self
}
Expand Down

0 comments on commit e1d8d9d

Please sign in to comment.