Skip to content

Commit 61946ff

Browse files
committed
feat: breakout spend reducers filters
1 parent 0fe9515 commit 61946ff

File tree

2 files changed

+60
-46
lines changed

2 files changed

+60
-46
lines changed

src/vms/pvm/etna-builder/spend-reducers/useSpendableLockedUTXOs.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,38 @@ import {
1717
} from '../../../../utils';
1818
import { verifySignaturesMatch } from '../../../utils/calculateSpend/utils';
1919
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+
};
2152

2253
export const useSpendableLockedUTXOs: SpendReducerFunction = (
2354
state,
@@ -26,29 +57,7 @@ export const useSpendableLockedUTXOs: SpendReducerFunction = (
2657
// 1. Filter out the UTXOs that are not usable.
2758
const usableUTXOs: Utxo<StakeableLockOut<TransferOutput>>[] = state.utxos
2859
// 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));
5261

5362
// 2. Verify signatures match.
5463
const verifiedUsableUTXOs = verifySignaturesMatch(

src/vms/pvm/etna-builder/spend-reducers/useUnlockedUTXOs.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,32 @@ import {
1515
} from '../../../../utils';
1616
import { verifySignaturesMatch } from '../../../utils/calculateSpend/utils';
1717
import { IncorrectStakeableLockOutError } from './errors';
18-
import type { SpendReducerFunction } from './types';
18+
import type { SpendReducerFunction, SpendReducerState } from './types';
19+
20+
/**
21+
* Is responsible for filtering out the usable UTXOs from the list of UTXOs.
22+
*
23+
* @internal - Only exported for testing.
24+
*/
25+
export const getUsableUTXOsFilter =
26+
(state: SpendReducerState) =>
27+
(
28+
utxo: Utxo,
29+
): utxo is Utxo<TransferOutput | StakeableLockOut<TransferOutput>> => {
30+
if (isTransferOut(utxo.output)) {
31+
return true;
32+
}
33+
34+
if (isStakeableLockOut(utxo.output)) {
35+
if (!isTransferOut(utxo.output.transferOut)) {
36+
throw IncorrectStakeableLockOutError;
37+
}
38+
39+
return utxo.output.getLocktime() < state.spendOptions.minIssuanceTime;
40+
}
41+
42+
return false;
43+
};
1944

2045
export const useUnlockedUTXOs: SpendReducerFunction = (
2146
state,
@@ -26,27 +51,7 @@ export const useUnlockedUTXOs: SpendReducerFunction = (
2651
const usableUTXOs: Utxo<TransferOutput | StakeableLockOut<TransferOutput>>[] =
2752
state.utxos
2853
// Filter out non stakeable lockouts and lockouts that are not stakeable yet.
29-
.filter(
30-
(
31-
utxo,
32-
): utxo is Utxo<TransferOutput | StakeableLockOut<TransferOutput>> => {
33-
if (isTransferOut(utxo.output)) {
34-
return true;
35-
}
36-
37-
if (isStakeableLockOut(utxo.output)) {
38-
if (!isTransferOut(utxo.output.transferOut)) {
39-
throw IncorrectStakeableLockOutError;
40-
}
41-
42-
return (
43-
utxo.output.getLocktime() < state.spendOptions.minIssuanceTime
44-
);
45-
}
46-
47-
return false;
48-
},
49-
);
54+
.filter(getUsableUTXOsFilter(state));
5055

5156
// 2. Verify signatures match.
5257
const verifiedUsableUTXOs = verifySignaturesMatch(

0 commit comments

Comments
 (0)