2021-12-24 12:52:08 +00:00
import ' package:cw_core/transaction_priority.dart ' ;
2024-11-06 15:06:52 +00:00
class BitcoinTransactionPriority extends TransactionPriority {
const BitcoinTransactionPriority ( { required super . title , required super . raw } ) ;
2024-10-28 15:16:23 +00:00
// Unimportant: the lowest possible, confirms when it confirms no matter how long it takes
2024-11-06 15:06:52 +00:00
static const BitcoinTransactionPriority unimportant =
BitcoinTransactionPriority ( title: ' Unimportant ' , raw: 0 ) ;
2024-10-28 15:16:23 +00:00
// 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)
2024-11-06 15:06:52 +00:00
static const BitcoinTransactionPriority normal =
BitcoinTransactionPriority ( title: ' Normal ' , raw: 1 ) ;
2024-10-28 15:16:23 +00:00
// Elevated: medium fee, confirms soon, elevated because it's higher than normal, gets you in the next 1-2 blocks (about 30 mins)
2024-11-06 15:06:52 +00:00
static const BitcoinTransactionPriority elevated =
BitcoinTransactionPriority ( title: ' Elevated ' , raw: 2 ) ;
2024-10-28 15:16:23 +00:00
// Priority: high fee, expected in the next block (about 10 mins).
2024-11-06 15:06:52 +00:00
static const BitcoinTransactionPriority priority =
BitcoinTransactionPriority ( title: ' Priority ' , raw: 3 ) ;
// Custom: any fee, user defined
static const BitcoinTransactionPriority custom =
BitcoinTransactionPriority ( title: ' Custom ' , raw: 4 ) ;
2024-10-28 15:16:23 +00:00
2024-11-06 15:06:52 +00:00
static BitcoinTransactionPriority 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-11-06 15:06:52 +00:00
case BitcoinTransactionPriority . unimportant:
2024-10-28 15:16:23 +00:00
label = ' Unimportant ~24hrs+ ' ; // '${S.current.transaction_priority_slow} ~24hrs';
2021-12-24 12:52:08 +00:00
break ;
2024-11-06 15:06:52 +00:00
case BitcoinTransactionPriority . normal:
2024-10-28 15:16:23 +00:00
label = ' Normal ~1hr+ ' ; // S.current.transaction_priority_medium;
2021-12-24 12:52:08 +00:00
break ;
2024-11-06 15:06:52 +00:00
case BitcoinTransactionPriority . elevated:
2024-10-28 15:16:23 +00:00
label = ' Elevated ' ;
break ; // S.current.transaction_priority_fast;
2024-11-06 15:06:52 +00:00
case BitcoinTransactionPriority . priority:
2024-10-28 15:16:23 +00:00
label = ' Priority ' ;
2024-04-08 14:54:58 +00:00
break ; // S.current.transaction_priority_fast;
2024-11-06 15:06:52 +00:00
case BitcoinTransactionPriority . 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-11-06 15:06:52 +00:00
class ElectrumTransactionPriority extends TransactionPriority {
const ElectrumTransactionPriority ( { required String title , required int raw } )
2021-12-24 12:52:08 +00:00
: super ( title: title , raw: raw ) ;
2024-11-06 15:06:52 +00:00
static const List < ElectrumTransactionPriority > all = [ fast , medium , slow , custom ] ;
static const ElectrumTransactionPriority slow =
ElectrumTransactionPriority ( title: ' Slow ' , raw: 0 ) ;
static const ElectrumTransactionPriority medium =
ElectrumTransactionPriority ( title: ' Medium ' , raw: 1 ) ;
static const ElectrumTransactionPriority fast =
ElectrumTransactionPriority ( title: ' Fast ' , raw: 2 ) ;
static const ElectrumTransactionPriority custom =
ElectrumTransactionPriority ( title: ' Custom ' , raw: 3 ) ;
static ElectrumTransactionPriority deserialize ( { required int raw } ) {
2021-12-24 12:52:08 +00:00
switch ( raw ) {
case 0 :
2024-11-06 15:06:52 +00:00
return slow ;
2021-12-24 12:52:08 +00:00
case 1 :
2024-11-06 15:06:52 +00:00
return medium ;
2021-12-24 12:52:08 +00:00
case 2 :
2024-11-06 15:06:52 +00:00
return fast ;
2024-10-28 15:16:23 +00:00
case 3 :
return custom ;
2021-12-24 12:52:08 +00:00
default :
2024-11-06 15:06:52 +00:00
throw Exception ( ' Unexpected token: $ raw for ElectrumTransactionPriority deserialize ' ) ;
2021-12-24 12:52:08 +00:00
}
}
2024-11-06 15:06:52 +00:00
String get units = > ' sat ' ;
2021-12-24 12:52:08 +00:00
@ override
String toString ( ) {
var label = ' ' ;
switch ( this ) {
2024-11-06 15:06:52 +00:00
case ElectrumTransactionPriority . slow:
2024-10-28 15:16:23 +00:00
label = ' Slow ~24hrs+ ' ; // '${S.current.transaction_priority_slow} ~24hrs';
2021-12-24 12:52:08 +00:00
break ;
2024-11-06 15:06:52 +00:00
case ElectrumTransactionPriority . medium:
2024-10-28 15:16:23 +00:00
label = ' Medium ' ; // S.current.transaction_priority_medium;
2024-11-06 15:06:52 +00:00
break ;
case ElectrumTransactionPriority . fast:
2024-10-28 15:16:23 +00:00
label = ' Fast ' ;
break ; // S.current.transaction_priority_fast;
2024-11-06 15:06:52 +00:00
case ElectrumTransactionPriority . custom:
2024-10-28 15:16:23 +00:00
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-11-06 15:06:52 +00:00
class LitecoinTransactionPriority extends ElectrumTransactionPriority {
2024-10-28 15:16:23 +00:00
const LitecoinTransactionPriority ( { required super . title , required super . raw } ) ;
@ override
String get units = > ' lit ' ;
}
2024-11-06 15:06:52 +00:00
class BitcoinCashTransactionPriority extends ElectrumTransactionPriority {
2024-10-28 15:16:23 +00:00
const BitcoinCashTransactionPriority ( { required super . title , required super . raw } ) ;
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-11-06 15:06:52 +00:00
class BitcoinTransactionPriorities implements TransactionPriorities {
const BitcoinTransactionPriorities ( {
2024-10-28 15:16:23 +00:00
required this . unimportant ,
required this . normal ,
required this . elevated ,
required this . priority ,
2024-11-06 15:06:52 +00:00
required this . custom ,
2024-10-28 15:16:23 +00:00
} ) ;
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 ;
2024-11-06 15:06:52 +00:00
final int custom ;
2024-10-28 15:16:23 +00:00
@ override
int operator [ ] ( TransactionPriority type ) {
switch ( type ) {
2024-11-06 15:06:52 +00:00
case BitcoinTransactionPriority . unimportant:
2024-10-28 15:16:23 +00:00
return unimportant ;
2024-11-06 15:06:52 +00:00
case BitcoinTransactionPriority . normal:
2024-10-28 15:16:23 +00:00
return normal ;
2024-11-06 15:06:52 +00:00
case BitcoinTransactionPriority . elevated:
2024-10-28 15:16:23 +00:00
return elevated ;
2024-11-06 15:06:52 +00:00
case BitcoinTransactionPriority . priority:
2024-10-28 15:16:23 +00:00
return priority ;
2024-11-06 15:06:52 +00:00
case BitcoinTransactionPriority . custom:
return custom ;
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 ;
2024-11-06 15:06:52 +00:00
if ( priorityType = = BitcoinTransactionPriority . custom ) {
2024-10-28 15:16:23 +00:00
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
}
2024-11-06 15:06:52 +00:00
@ override
Map < String , int > toJson ( ) {
return {
' unimportant ' : unimportant ,
' normal ' : normal ,
' elevated ' : elevated ,
' priority ' : priority ,
' custom ' : custom ,
} ;
}
static BitcoinTransactionPriorities fromJson ( Map < String , dynamic > json ) {
return BitcoinTransactionPriorities (
unimportant: json [ ' unimportant ' ] as int ,
normal: json [ ' normal ' ] as int ,
elevated: json [ ' elevated ' ] as int ,
priority: json [ ' priority ' ] as int ,
custom: json [ ' custom ' ] as int ,
) ;
}
2021-12-24 12:52:08 +00:00
}
2023-10-12 22:50:16 +00:00
2024-11-06 15:06:52 +00:00
class ElectrumTransactionPriorities implements TransactionPriorities {
const ElectrumTransactionPriorities ( {
2024-10-28 15:16:23 +00:00
required this . slow ,
required this . medium ,
required this . fast ,
2024-11-06 15:06:52 +00:00
required this . custom ,
2024-10-28 15:16:23 +00:00
} ) ;
final int slow ;
final int medium ;
final int fast ;
2024-11-06 15:06:52 +00:00
final int custom ;
2024-10-28 15:16:23 +00:00
@ override
int operator [ ] ( TransactionPriority type ) {
switch ( type ) {
2024-11-06 15:06:52 +00:00
case ElectrumTransactionPriority . slow:
2024-10-28 15:16:23 +00:00
return slow ;
2024-11-06 15:06:52 +00:00
case ElectrumTransactionPriority . medium:
2024-10-28 15:16:23 +00:00
return medium ;
2024-11-06 15:06:52 +00:00
case ElectrumTransactionPriority . fast:
2024-10-28 15:16:23 +00:00
return fast ;
2024-11-06 15:06:52 +00:00
case ElectrumTransactionPriority . custom:
return custom ;
2024-10-28 15:16:23 +00:00
default :
throw Exception ( ' Unexpected token: $ type for TransactionPriorities operator[] ' ) ;
}
}
@ override
String labelWithRate ( TransactionPriority priorityType , [ int ? rate ] ) {
return ' ${ priorityType . toString ( ) } ( ${ this [ priorityType ] } ${ priorityType . units } /byte) ' ;
}
2024-11-06 15:06:52 +00:00
factory ElectrumTransactionPriorities . fromList ( List < int > list ) {
2024-10-28 15:16:23 +00:00
if ( list . length ! = 3 ) {
throw Exception (
' Unexpected list length: ${ list . length } for BitcoinElectrumTransactionPriorities.fromList ' ) ;
}
2024-11-06 15:06:52 +00:00
return ElectrumTransactionPriorities (
2024-10-28 15:16:23 +00:00
slow: list [ 0 ] ,
medium: list [ 1 ] ,
fast: list [ 2 ] ,
2024-11-06 15:06:52 +00:00
custom: 0 ,
) ;
}
@ override
Map < String , int > toJson ( ) {
return {
' slow ' : slow ,
' medium ' : medium ,
' fast ' : fast ,
' custom ' : custom ,
} ;
}
static ElectrumTransactionPriorities fromJson ( Map < String , dynamic > json ) {
return ElectrumTransactionPriorities (
slow: json [ ' slow ' ] as int ,
medium: json [ ' medium ' ] as int ,
fast: json [ ' fast ' ] as int ,
custom: json [ ' custom ' ] as int ,
2024-10-28 15:16:23 +00:00
) ;
}
}
2024-11-06 15:06:52 +00:00
TransactionPriorities deserializeTransactionPriorities ( Map < String , dynamic > json ) {
if ( json . containsKey ( ' unimportant ' ) ) {
return BitcoinTransactionPriorities . fromJson ( json ) ;
} else if ( json . containsKey ( ' slow ' ) ) {
return ElectrumTransactionPriorities . fromJson ( json ) ;
} else {
throw Exception ( ' Unexpected token: $ json for deserializeTransactionPriorities ' ) ;
}
}