2021-12-24 12:52:08 +00:00
import ' package:cw_core/transaction_priority.dart ' ;
2024-10-28 15:16:23 +00:00
// Unimportant: the lowest possible, confirms when it confirms no matter how long it takes
// Normal: low fee, confirms in a reasonable time, normal because in most cases more than this is not needed, gets you in the next 2-3 blocks (about 1 hour)
// Elevated: medium fee, confirms soon, elevated because it's higher than normal, gets you in the next 1-2 blocks (about 30 mins)
// Priority: high fee, expected in the next block (about 10 mins).
class BitcoinMempoolAPITransactionPriority extends TransactionPriority {
const BitcoinMempoolAPITransactionPriority ( { required super . title , required super . raw } ) ;
2021-12-24 12:52:08 +00:00
2024-10-28 15:16:23 +00:00
static const BitcoinMempoolAPITransactionPriority unimportant =
BitcoinMempoolAPITransactionPriority ( title: ' Unimportant ' , raw: 0 ) ;
static const BitcoinMempoolAPITransactionPriority normal =
BitcoinMempoolAPITransactionPriority ( title: ' Normal ' , raw: 1 ) ;
static const BitcoinMempoolAPITransactionPriority elevated =
BitcoinMempoolAPITransactionPriority ( title: ' Elevated ' , raw: 2 ) ;
static const BitcoinMempoolAPITransactionPriority priority =
BitcoinMempoolAPITransactionPriority ( title: ' Priority ' , raw: 3 ) ;
static const BitcoinMempoolAPITransactionPriority custom =
BitcoinMempoolAPITransactionPriority ( title: ' Custom ' , raw: 4 ) ;
static BitcoinMempoolAPITransactionPriority deserialize ( { required int raw } ) {
2021-12-24 12:52:08 +00:00
switch ( raw ) {
case 0 :
2024-10-28 15:16:23 +00:00
return unimportant ;
2021-12-24 12:52:08 +00:00
case 1 :
2024-10-28 15:16:23 +00:00
return normal ;
2021-12-24 12:52:08 +00:00
case 2 :
2024-10-28 15:16:23 +00:00
return elevated ;
2024-04-08 14:54:58 +00:00
case 3 :
2024-10-28 15:16:23 +00:00
return priority ;
case 4 :
2024-04-08 14:54:58 +00:00
return custom ;
2021-12-24 12:52:08 +00:00
default :
2024-10-28 15:16:23 +00:00
throw Exception ( ' Unexpected token: $ raw for TransactionPriority deserialize ' ) ;
2021-12-24 12:52:08 +00:00
}
}
@ override
String toString ( ) {
var label = ' ' ;
switch ( this ) {
2024-10-28 15:16:23 +00:00
case BitcoinMempoolAPITransactionPriority . unimportant:
label = ' Unimportant ~24hrs+ ' ; // '${S.current.transaction_priority_slow} ~24hrs';
2021-12-24 12:52:08 +00:00
break ;
2024-10-28 15:16:23 +00:00
case BitcoinMempoolAPITransactionPriority . normal:
label = ' Normal ~1hr+ ' ; // S.current.transaction_priority_medium;
2021-12-24 12:52:08 +00:00
break ;
2024-10-28 15:16:23 +00:00
case BitcoinMempoolAPITransactionPriority . elevated:
label = ' Elevated ' ;
break ; // S.current.transaction_priority_fast;
case BitcoinMempoolAPITransactionPriority . priority:
label = ' Priority ' ;
2024-04-08 14:54:58 +00:00
break ; // S.current.transaction_priority_fast;
2024-10-28 15:16:23 +00:00
case BitcoinMempoolAPITransactionPriority . custom:
2024-04-08 14:54:58 +00:00
label = ' Custom ' ;
2021-12-24 12:52:08 +00:00
break ;
default :
break ;
}
return label ;
}
2024-04-08 14:54:58 +00:00
String labelWithRate ( int rate , int ? customRate ) {
final rateValue = this = = custom ? customRate ? ? = 0 : rate ;
return ' ${ toString ( ) } ( $ rateValue ${ units } /byte) ' ;
}
2021-12-24 12:52:08 +00:00
}
2024-10-28 15:16:23 +00:00
class BitcoinElectrumTransactionPriority extends TransactionPriority {
const BitcoinElectrumTransactionPriority ( { required String title , required int raw } )
2021-12-24 12:52:08 +00:00
: super ( title: title , raw: raw ) ;
2024-10-28 15:16:23 +00:00
static const List < BitcoinElectrumTransactionPriority > all = [
unimportant ,
normal ,
elevated ,
priority ,
custom ,
] ;
2021-12-24 12:52:08 +00:00
2024-10-28 15:16:23 +00:00
static const BitcoinElectrumTransactionPriority unimportant =
BitcoinElectrumTransactionPriority ( title: ' Unimportant ' , raw: 0 ) ;
static const BitcoinElectrumTransactionPriority normal =
BitcoinElectrumTransactionPriority ( title: ' Normal ' , raw: 1 ) ;
static const BitcoinElectrumTransactionPriority elevated =
BitcoinElectrumTransactionPriority ( title: ' Elevated ' , raw: 2 ) ;
static const BitcoinElectrumTransactionPriority priority =
BitcoinElectrumTransactionPriority ( title: ' Priority ' , raw: 3 ) ;
static const BitcoinElectrumTransactionPriority custom =
BitcoinElectrumTransactionPriority ( title: ' Custom ' , raw: 4 ) ;
static BitcoinElectrumTransactionPriority deserialize ( { required int raw } ) {
2021-12-24 12:52:08 +00:00
switch ( raw ) {
case 0 :
2024-10-28 15:16:23 +00:00
return unimportant ;
2021-12-24 12:52:08 +00:00
case 1 :
2024-10-28 15:16:23 +00:00
return normal ;
2021-12-24 12:52:08 +00:00
case 2 :
2024-10-28 15:16:23 +00:00
return elevated ;
case 3 :
return priority ;
case 4 :
return custom ;
2021-12-24 12:52:08 +00:00
default :
2024-10-28 15:16:23 +00:00
throw Exception ( ' Unexpected token: $ raw for TransactionPriority deserialize ' ) ;
2021-12-24 12:52:08 +00:00
}
}
@ override
String toString ( ) {
var label = ' ' ;
switch ( this ) {
2024-10-28 15:16:23 +00:00
case BitcoinElectrumTransactionPriority . unimportant:
label = ' Unimportant ' ; // '${S.current.transaction_priority_slow} ~24hrs';
2021-12-24 12:52:08 +00:00
break ;
2024-10-28 15:16:23 +00:00
case BitcoinElectrumTransactionPriority . normal:
label = ' Slow ~24hrs+ ' ; // '${S.current.transaction_priority_slow} ~24hrs';
2021-12-24 12:52:08 +00:00
break ;
2024-10-28 15:16:23 +00:00
case BitcoinElectrumTransactionPriority . elevated:
label = ' Medium ' ; // S.current.transaction_priority_medium;
break ; // S.current.transaction_priority_fast;
case BitcoinElectrumTransactionPriority . priority:
label = ' Fast ' ;
break ; // S.current.transaction_priority_fast;
case BitcoinElectrumTransactionPriority . custom:
label = ' Custom ' ;
2021-12-24 12:52:08 +00:00
break ;
default :
break ;
}
2023-10-12 22:50:16 +00:00
return label ;
}
2024-10-28 15:16:23 +00:00
String labelWithRate ( int rate , int ? customRate ) {
final rateValue = this = = custom ? customRate ? ? = 0 : rate ;
return ' ${ toString ( ) } ( $ rateValue ${ units } /byte) ' ;
}
2023-10-12 22:50:16 +00:00
}
2024-10-28 15:16:23 +00:00
class LitecoinTransactionPriority extends BitcoinElectrumTransactionPriority {
const LitecoinTransactionPriority ( { required super . title , required super . raw } ) ;
static const all = [ slow , medium , fast ] ;
static const LitecoinTransactionPriority slow =
LitecoinTransactionPriority ( title: ' Slow ' , raw: 0 ) ;
static const LitecoinTransactionPriority medium =
LitecoinTransactionPriority ( title: ' Medium ' , raw: 1 ) ;
static const LitecoinTransactionPriority fast =
LitecoinTransactionPriority ( title: ' Fast ' , raw: 2 ) ;
static LitecoinTransactionPriority deserialize ( { required int raw } ) {
switch ( raw ) {
case 0 :
return slow ;
case 1 :
return medium ;
case 2 :
return fast ;
default :
throw Exception ( ' Unexpected token: $ raw for LitecoinTransactionPriority deserialize ' ) ;
}
}
@ override
String get units = > ' lit ' ;
}
class BitcoinCashTransactionPriority extends BitcoinElectrumTransactionPriority {
const BitcoinCashTransactionPriority ( { required super . title , required super . raw } ) ;
static const all = [ slow , medium , fast ] ;
2023-10-12 22:50:16 +00:00
static const BitcoinCashTransactionPriority slow =
2024-10-28 15:16:23 +00:00
BitcoinCashTransactionPriority ( title: ' Slow ' , raw: 0 ) ;
2023-10-12 22:50:16 +00:00
static const BitcoinCashTransactionPriority medium =
2024-10-28 15:16:23 +00:00
BitcoinCashTransactionPriority ( title: ' Medium ' , raw: 1 ) ;
2023-10-12 22:50:16 +00:00
static const BitcoinCashTransactionPriority fast =
2024-10-28 15:16:23 +00:00
BitcoinCashTransactionPriority ( title: ' Fast ' , raw: 2 ) ;
2023-10-12 22:50:16 +00:00
static BitcoinCashTransactionPriority deserialize ( { required int raw } ) {
switch ( raw ) {
case 0 :
return slow ;
case 1 :
return medium ;
case 2 :
return fast ;
default :
2024-10-28 15:16:23 +00:00
throw Exception ( ' Unexpected token: $ raw for LitecoinTransactionPriority deserialize ' ) ;
2023-10-12 22:50:16 +00:00
}
}
@ override
2024-10-28 15:16:23 +00:00
String get units = > ' satoshi ' ;
}
2023-10-12 22:50:16 +00:00
2024-10-28 15:16:23 +00:00
class BitcoinMempoolAPITransactionPriorities implements TransactionPriorities {
const BitcoinMempoolAPITransactionPriorities ( {
required this . unimportant ,
required this . normal ,
required this . elevated ,
required this . priority ,
} ) ;
2023-10-12 22:50:16 +00:00
2024-10-28 15:16:23 +00:00
final int unimportant ;
final int normal ;
final int elevated ;
final int priority ;
@ override
int operator [ ] ( TransactionPriority type ) {
switch ( type ) {
case BitcoinMempoolAPITransactionPriority . unimportant:
return unimportant ;
case BitcoinMempoolAPITransactionPriority . normal:
return normal ;
case BitcoinMempoolAPITransactionPriority . elevated:
return elevated ;
case BitcoinMempoolAPITransactionPriority . priority:
return priority ;
2023-10-12 22:50:16 +00:00
default :
2024-10-28 15:16:23 +00:00
throw Exception ( ' Unexpected token: $ type for TransactionPriorities operator[] ' ) ;
2023-10-12 22:50:16 +00:00
}
2024-10-28 15:16:23 +00:00
}
2023-10-12 22:50:16 +00:00
2024-10-28 15:16:23 +00:00
@ override
String labelWithRate ( TransactionPriority priorityType , [ int ? rate ] ) {
late int rateValue ;
if ( priorityType = = BitcoinMempoolAPITransactionPriority . custom ) {
if ( rate = = null ) {
throw Exception ( ' Rate must be provided for custom transaction priority ' ) ;
}
rateValue = rate ;
} else {
rateValue = this [ priorityType ] ;
}
return ' ${ priorityType . toString ( ) } ( ${ rateValue } ${ priorityType . units } /byte) ' ;
2021-12-24 12:52:08 +00:00
}
}
2023-10-12 22:50:16 +00:00
2024-10-28 15:16:23 +00:00
class BitcoinElectrumTransactionPriorities implements TransactionPriorities {
const BitcoinElectrumTransactionPriorities ( {
required this . unimportant ,
required this . slow ,
required this . medium ,
required this . fast ,
} ) ;
final int unimportant ;
final int slow ;
final int medium ;
final int fast ;
@ override
int operator [ ] ( TransactionPriority type ) {
switch ( type ) {
case BitcoinElectrumTransactionPriority . unimportant:
return unimportant ;
case BitcoinElectrumTransactionPriority . normal:
return slow ;
case BitcoinElectrumTransactionPriority . elevated:
return medium ;
case BitcoinElectrumTransactionPriority . priority:
return fast ;
default :
throw Exception ( ' Unexpected token: $ type for TransactionPriorities operator[] ' ) ;
}
}
@ override
String labelWithRate ( TransactionPriority priorityType , [ int ? rate ] ) {
return ' ${ priorityType . toString ( ) } ( ${ this [ priorityType ] } ${ priorityType . units } /byte) ' ;
}
factory BitcoinElectrumTransactionPriorities . fromList ( List < int > list ) {
if ( list . length ! = 3 ) {
throw Exception (
' Unexpected list length: ${ list . length } for BitcoinElectrumTransactionPriorities.fromList ' ) ;
}
int unimportantFee = list [ 0 ] ;
// Electrum servers only provides 3 levels: slow, medium, fast
// so make "unimportant" always lower than slow (but not 0)
if ( unimportantFee > 1 ) {
unimportantFee - - ;
}
return BitcoinElectrumTransactionPriorities (
unimportant: unimportantFee ,
slow: list [ 0 ] ,
medium: list [ 1 ] ,
fast: list [ 2 ] ,
) ;
}
}