-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Transaction broadcasting (#72)
* Add classes for transaction broadcasting * fix broadcasting compilation problems * Reformat transaction_hash.dart * Review PR suggestions Co-authored-by: Andrzej Chmielewski <[email protected]>
- Loading branch information
1 parent
2dc9754
commit 5f4b350
Showing
6 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/transaction_signing_gateway/lib/alan/alan_transaction_broadcaster.dart
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,48 @@ | ||
import 'package:alan/transactions/sender/tx_sender.dart'; | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:transaction_signing_gateway/alan/alan_private_wallet_credentials.dart'; | ||
import 'package:transaction_signing_gateway/alan/alan_transaction.dart'; | ||
import 'package:transaction_signing_gateway/model/private_wallet_credentials.dart'; | ||
import 'package:transaction_signing_gateway/model/signed_transaction.dart'; | ||
import 'package:transaction_signing_gateway/model/transaction_broadcasting_failure.dart'; | ||
import 'package:transaction_signing_gateway/model/transaction_hash.dart'; | ||
import 'package:transaction_signing_gateway/transaction_broadcaster.dart'; | ||
|
||
class AlanTransactionBroadcaster implements TransactionBroadcaster { | ||
@override | ||
Future<Either<TransactionBroadcastingFailure, TransactionHash>> broadcast( | ||
{required SignedTransaction transaction, required PrivateWalletCredentials privateWalletCredentials}) async { | ||
if (transaction is! SignedAlanTransaction) { | ||
return left(AlanTransactionBroadcastingFailure('passed transaction is not $SignedAlanTransaction')); | ||
} | ||
if (privateWalletCredentials is! AlanPrivateWalletCredentials) { | ||
return left(AlanTransactionBroadcastingFailure("passed privateCredentials is not $AlanPrivateWalletCredentials")); | ||
} | ||
final txSender = TxSender.fromNetworkInfo(privateWalletCredentials.networkInfo); | ||
final response = await txSender.broadcastTx(transaction.signedTransaction); | ||
|
||
if (response.hasTxhash()) { | ||
return right(TransactionHash(txHash: response.txhash)); | ||
} else { | ||
return left(AlanTransactionBroadcastingFailure('Tx error: $response')); | ||
} | ||
} | ||
|
||
@override | ||
bool canBroadcast(SignedTransaction signedTransaction) => signedTransaction is SignedAlanTransaction; | ||
} | ||
|
||
class AlanTransactionBroadcastingFailure extends TransactionBroadcastingFailure { | ||
final Object cause; | ||
|
||
AlanTransactionBroadcastingFailure(this.cause); | ||
|
||
@override | ||
String toString() { | ||
return 'AlanTransactionSigningFailure{cause: $cause}'; | ||
} | ||
|
||
@override | ||
// TODO: implement type | ||
TransactionBroadcastingFailType get type => TransactionBroadcastingFailType.unknown; | ||
} |
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
20 changes: 20 additions & 0 deletions
20
packages/transaction_signing_gateway/lib/model/transaction_broadcasting_failure.dart
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,20 @@ | ||
enum TransactionBroadcastingFailType { | ||
noTransactionBroadcasterFound, | ||
walletCredentialsStorageFailure, | ||
unknown, | ||
} | ||
|
||
abstract class TransactionBroadcastingFailure { | ||
TransactionBroadcastingFailType get type; | ||
} | ||
|
||
class TransactionBroadcasterNotFoundFailure extends TransactionBroadcastingFailure { | ||
@override | ||
TransactionBroadcastingFailType get type => TransactionBroadcastingFailType.noTransactionBroadcasterFound; | ||
} | ||
|
||
class StorageProblemBroadcastingFailure extends TransactionBroadcastingFailure { | ||
@override | ||
// TODO: implement type | ||
TransactionBroadcastingFailType get type => TransactionBroadcastingFailType.walletCredentialsStorageFailure; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/transaction_signing_gateway/lib/model/transaction_hash.dart
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,5 @@ | ||
class TransactionHash { | ||
String txHash; | ||
|
||
TransactionHash({required this.txHash}); | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/transaction_signing_gateway/lib/transaction_broadcaster.dart
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,24 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:transaction_signing_gateway/model/private_wallet_credentials.dart'; | ||
import 'package:transaction_signing_gateway/model/signed_transaction.dart'; | ||
import 'package:transaction_signing_gateway/model/transaction_broadcasting_failure.dart'; | ||
import 'package:transaction_signing_gateway/model/transaction_hash.dart'; | ||
|
||
abstract class TransactionBroadcaster { | ||
Future<Either<TransactionBroadcastingFailure, TransactionHash>> broadcast({ | ||
required SignedTransaction transaction, | ||
required PrivateWalletCredentials privateWalletCredentials, | ||
}); | ||
|
||
bool canBroadcast(SignedTransaction signedTransaction); | ||
} | ||
|
||
class NotFoundBroadcaster implements TransactionBroadcaster { | ||
@override | ||
Future<Either<TransactionBroadcastingFailure, TransactionHash>> broadcast( | ||
{required SignedTransaction transaction, required PrivateWalletCredentials privateWalletCredentials}) async => | ||
left(TransactionBroadcasterNotFoundFailure()); | ||
|
||
@override | ||
bool canBroadcast(SignedTransaction signedTransaction) => true; | ||
} |
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