-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IOS-7797 [Staking] StakeKitTransactionSender
use async/await
#822
Merged
Balashov152
merged 9 commits into
develop
from
feature/IOS-7797-StakeKitTransactionSender-async
Sep 4, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1c24f10
IOS-7797 Update `StakeKitTransactionSender`
Balashov152 c26ba37
IOS-7797 Update StakeKitTransactionSender
Balashov152 ba8393e
IOS-7797 Default implemenation for `StakeKitTransactionSender, StakeK…
Balashov152 7dc68a4
IOS-7797 Remove old code
Balashov152 732df54
IOS-7797 Fix review
Balashov152 4156b93
Merge branch 'develop' into feature/IOS-7797-StakeKitTransactionSende…
Balashov152 50b523d
IOS-7797 Update project.pbxproj
Balashov152 11f6635
IOS-7797 Update CosmosWalletManager.swift
Balashov152 ee5b9ac
IOS-7797 Update StakeKitTransactionSender.swift
Balashov152 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
BlockchainSdk/Common/Interfaces/StakeKitTransactionSender.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// StakeKitTransactionSender.swift | ||
// BlockchainSdk | ||
// | ||
// Created by Sergey Balashov on 02.09.2024. | ||
// | ||
|
||
import Foundation | ||
import TangemSdk | ||
|
||
public protocol StakeKitTransactionSender { | ||
/// Return stream with tx which was sent one by one | ||
/// If catch error stream will be stopped | ||
/// In case when manager already implemented the `StakeKitTransactionSenderProvider` method will be not required | ||
func sendStakeKit(transactions: [StakeKitTransaction], signer: TransactionSigner, delay second: UInt64?) -> AsyncThrowingStream<StakeKitTransactionSendResult, Error> | ||
} | ||
|
||
protocol StakeKitTransactionSenderProvider { | ||
associatedtype RawTransaction | ||
|
||
func prepareDataForSign(transaction: StakeKitTransaction) throws -> Data | ||
func prepareDataForSend(transaction: StakeKitTransaction, signature: SignatureInfo) throws -> RawTransaction | ||
func broadcast(transaction: StakeKitTransaction, rawTransaction: RawTransaction) async throws -> String | ||
} | ||
|
||
// MARK: - Common implementation for StakeKitTransactionSenderProvider | ||
|
||
extension StakeKitTransactionSender where Self: StakeKitTransactionSenderProvider, Self: WalletProvider, RawTransaction: CustomStringConvertible { | ||
func sendStakeKit(transactions: [StakeKitTransaction], signer: TransactionSigner, delay second: UInt64?) -> AsyncThrowingStream<StakeKitTransactionSendResult, Error> { | ||
.init { [weak self] continuation in | ||
let task = Task { [weak self] in | ||
guard let self else { | ||
continuation.finish() | ||
return | ||
} | ||
|
||
do { | ||
tureck1y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let preparedHashes = try transactions.map { try self.prepareDataForSign(transaction: $0) } | ||
let signatures: [SignatureInfo] = try await signer.sign(hashes: preparedHashes, walletPublicKey: wallet.publicKey).async() | ||
|
||
for (transaction, signature) in zip(transactions, signatures) { | ||
try Task.checkCancellation() | ||
let rawTransaction = try prepareDataForSend(transaction: transaction, signature: signature) | ||
|
||
do { | ||
let result: TransactionSendResult = try await broadcast(transaction: transaction, rawTransaction: rawTransaction) | ||
try Task.checkCancellation() | ||
continuation.yield(.init(transaction: transaction, result: result)) | ||
m3g0byt3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch { | ||
throw StakeKitTransactionSendError(transaction: transaction, error: error) | ||
} | ||
|
||
if transactions.count > 1, let second { | ||
Log.log("\(self) start \(second) second delay between the transactions sending") | ||
try await Task.sleep(nanoseconds: second * NSEC_PER_SEC) | ||
} | ||
} | ||
|
||
continuation.finish() | ||
|
||
} catch { | ||
Log.log("\(self) catch \(error) when sent staking transaction") | ||
continuation.finish(throwing: error) | ||
} | ||
} | ||
|
||
continuation.onTermination = { termination in | ||
task.cancel() | ||
} | ||
} | ||
} | ||
|
||
/// Convenience method with adding the `PendingTransaction` to the wallet and `SendTxError` mapping | ||
private func broadcast(transaction: StakeKitTransaction, rawTransaction: RawTransaction) async throws -> TransactionSendResult { | ||
do { | ||
let hash: String = try await broadcast(transaction: transaction, rawTransaction: rawTransaction) | ||
let mapper = PendingTransactionRecordMapper() | ||
let record = mapper.mapToPendingTransactionRecord( | ||
stakeKitTransaction: transaction, | ||
source: wallet.defaultAddress.value, | ||
hash: hash | ||
) | ||
|
||
await addPendingTransaction(record) | ||
|
||
return TransactionSendResult(hash: hash) | ||
} catch { | ||
throw SendTxErrorFactory().make(error: error, with: rawTransaction.description) | ||
} | ||
} | ||
|
||
@MainActor | ||
private func addPendingTransaction(_ record: PendingTransactionRecord) { | ||
wallet.addPendingTransaction(record) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а тут это точно надо? В оригинальном коде не было
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ага, я же писал тебе даже там ошибка была от ноды )