Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ginkgo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
vjrj
ginkgo
Commits
2d870c1f
Commit
2d870c1f
authored
1 year ago
by
vjrj
Browse files
Options
Downloads
Patches
Plain Diff
Improve utxo cubit
parent
c574a7e4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/data/models/utxo_cubit.dart
+30
-39
30 additions, 39 deletions
lib/data/models/utxo_cubit.dart
with
30 additions
and
39 deletions
lib/data/models/utxo_cubit.dart
+
30
−
39
View file @
2d870c1f
...
...
@@ -57,66 +57,57 @@ class UtxoCubit extends HydratedCubit<UtxoState> {
}
}
List
<
Utxo
>
?
consume
(
double
amount
)
{
final
List
<
Utxo
>
selectedUtxos
=
<
Utxo
>[];
double
coveredAmount
=
0
;
final
Map
<
String
,
Utxo
>
updatedConsumedUtxos
=
<
String
,
Utxo
>{};
List
<
Utxo
>
consume
(
double
amount
)
{
if
(
state
is
UtxoLoaded
)
{
final
UtxoLoaded
currentState
=
state
as
UtxoLoaded
;
for
(
final
Utxo
utxo
in
currentState
.
utxos
)
{
if
(
coveredAmount
>
=
amount
)
{
break
;
}
final
List
<
Utxo
>
selectedUtxos
=
<
Utxo
>[];
double
total
=
0.0
;
double
availableAmount
=
utxo
.
amount
;
if
(
currentState
.
consumedUtxos
.
containsKey
(
utxo
.
txHash
))
{
availableAmount
=
currentState
.
consumedUtxos
[
utxo
.
txHash
]
!.
amount
;
if
(
availableAmount
==
0
)
{
// It's totally consumed, so skip it (but keep the record)
updatedConsumedUtxos
[
utxo
.
txHash
]
=
utxo
;
continue
;
}
}
final
List
<
Utxo
>
sortedUtxos
=
List
<
Utxo
>
.
from
(
currentState
.
utxos
)
.
.
sort
((
Utxo
a
,
Utxo
b
)
=
>
b
.
amount
.
compareTo
(
a
.
amount
));
final
double
consumeAmount
=
(
amount
-
coveredAmount
)
.
clamp
(
0
,
availableAmount
);
coveredAmount
+=
consumeAmount
;
// Update consumed UTXOs
final
Utxo
updatedUtxo
=
currentState
.
consumedUtxos
.
containsKey
(
utxo
.
txHash
)
?
currentState
.
consumedUtxos
[
utxo
.
txHash
]
!
.
copyWith
(
amount:
availableAmount
-
consumeAmount
)
:
utxo
.
copyWith
(
amount:
utxo
.
amount
-
consumeAmount
);
updatedConsumedUtxos
[
utxo
.
txHash
]
=
updatedUtxo
;
if
(
consumeAmount
>
0
)
selectedUtxos
.
add
(
utxo
.
copyWith
(
amount:
consumeAmount
));
for
(
final
Utxo
utxo
in
sortedUtxos
)
{
if
(
total
>
=
amount
)
{
break
;
}
selectedUtxos
.
add
(
utxo
);
total
+=
utxo
.
amount
;
}
if
(
coveredAmount
<
amount
)
{
if
(
total
<
amount
)
{
emit
(
UtxosError
(
'Insufficient UTXOs to cover the requested amount'
));
return
null
;
return
<
Utxo
>[]
;
}
emit
(
currentState
.
copyWith
(
consumedUtxos:
updatedConsumedUtxos
,
));
final
List
<
Utxo
>
updatedUtxos
=
currentState
.
utxos
.
where
((
Utxo
utxo
)
=
>
!
selectedUtxos
.
contains
(
utxo
))
.
toList
();
emit
(
currentState
.
copyWith
(
utxos:
updatedUtxos
));
return
selectedUtxos
;
}
else
{
emit
(
UtxosError
(
'Wrong utxo state'
));
return
null
;
return
<
Utxo
>[]
;
}
}
void
resetConsumedUtxos
()
{
if
(
state
is
UtxoLoaded
)
{
// Emit a new state with an empty map for consumed UTXOs
emit
((
state
as
UtxoLoaded
)
.
copyWith
(
consumedUtxos:
<
String
,
Utxo
>{}));
}
else
{
emit
(
UtxosError
(
'Wrong utxo state'
));
}
}
void
addUtxos
(
List
<
Utxo
>
newUtxos
)
{
if
(
state
is
UtxoLoaded
)
{
final
UtxoLoaded
currentState
=
state
as
UtxoLoaded
;
final
List
<
Utxo
>
updatedUtxos
=
List
<
Utxo
>
.
from
(
currentState
.
utxos
)
.
.
addAll
(
newUtxos
);
emit
(
currentState
.
copyWith
(
utxos:
updatedUtxos
));
}
else
{
emit
(
UtxosError
(
'Wrong utxo state'
));
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment