@@ -17,7 +17,38 @@ import {
17
17
} from '../../../../utils' ;
18
18
import { verifySignaturesMatch } from '../../../utils/calculateSpend/utils' ;
19
19
import { IncorrectStakeableLockOutError } from './errors' ;
20
- import type { SpendReducerFunction } from './types' ;
20
+ import type { SpendReducerFunction , SpendReducerState } from './types' ;
21
+
22
+ /**
23
+ * Is responsible for filtering out the usable UTXOs from the list of UTXOs.
24
+ *
25
+ * @internal - Only exported for testing.
26
+ */
27
+ export const getUsableUTXOsFilter =
28
+ ( state : SpendReducerState ) =>
29
+ ( utxo : Utxo ) : utxo is Utxo < StakeableLockOut < TransferOutput > > => {
30
+ // 1a. Ensure UTXO output is a StakeableLockOut.
31
+ if ( ! isStakeableLockOut ( utxo . output ) ) {
32
+ return false ;
33
+ }
34
+
35
+ // 1b. Ensure UTXO is stakeable.
36
+ if ( ! ( state . spendOptions . minIssuanceTime < utxo . output . getLocktime ( ) ) ) {
37
+ return false ;
38
+ }
39
+
40
+ // 1c. Ensure transferOut is a TransferOutput.
41
+ if ( ! isTransferOut ( utxo . output . transferOut ) ) {
42
+ throw IncorrectStakeableLockOutError ;
43
+ }
44
+
45
+ // 1d. Filter out UTXOs that aren't needed for staking.
46
+ if ( ( state . toStake . get ( utxo . assetId . value ( ) ) ?? 0n ) === 0n ) {
47
+ return false ;
48
+ }
49
+
50
+ return true ;
51
+ } ;
21
52
22
53
export const useSpendableLockedUTXOs : SpendReducerFunction = (
23
54
state ,
@@ -26,29 +57,7 @@ export const useSpendableLockedUTXOs: SpendReducerFunction = (
26
57
// 1. Filter out the UTXOs that are not usable.
27
58
const usableUTXOs : Utxo < StakeableLockOut < TransferOutput > > [ ] = state . utxos
28
59
// Filter out non stakeable lockouts and lockouts that are not stakeable yet.
29
- . filter ( ( utxo ) : utxo is Utxo < StakeableLockOut < TransferOutput > > => {
30
- // 1a. Ensure UTXO output is a StakeableLockOut.
31
- if ( ! isStakeableLockOut ( utxo . output ) ) {
32
- return false ;
33
- }
34
-
35
- // 1b. Ensure UTXO is stakeable.
36
- if ( ! ( state . spendOptions . minIssuanceTime < utxo . output . getLocktime ( ) ) ) {
37
- return false ;
38
- }
39
-
40
- // 1c. Ensure there are funds to stake.
41
- if ( ( state . toStake . get ( utxo . assetId . value ( ) ) ?? 0n ) === 0n ) {
42
- return false ;
43
- }
44
-
45
- // 1d. Ensure transferOut is a TransferOutput.
46
- if ( ! isTransferOut ( utxo . output . transferOut ) ) {
47
- throw IncorrectStakeableLockOutError ;
48
- }
49
-
50
- return true ;
51
- } ) ;
60
+ . filter ( getUsableUTXOsFilter ( state ) ) ;
52
61
53
62
// 2. Verify signatures match.
54
63
const verifiedUsableUTXOs = verifySignaturesMatch (
0 commit comments