Skip to content

Commit 2e97aa8

Browse files
fixup! feat(input-selection): add large first input selection strategy
1 parent c64fabc commit 2e97aa8

File tree

7 files changed

+50
-35
lines changed

7 files changed

+50
-35
lines changed

packages/input-selection/src/GreedySelection/GreedyInputSelector.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
import { Cardano, coalesceValueQuantities } from '@cardano-sdk/core';
33
import { InputSelectionError, InputSelectionFailure } from '../InputSelectionError';
44
import { InputSelectionParameters, InputSelector, SelectionResult } from '../types';
5-
import { addTokenMaps, getCoinQuantity, hasNegativeAssetValue, subtractTokenMaps, toValues } from '../util';
6-
import { sortUtxoByTxIn, splitChangeAndComputeFee } from './util';
5+
import {
6+
addTokenMaps,
7+
getCoinQuantity,
8+
hasNegativeAssetValue,
9+
sortUtxoByTxIn,
10+
subtractTokenMaps,
11+
toValues
12+
} from '../util';
13+
import { splitChangeAndComputeFee } from './util';
714

815
/** Greedy selection initialization properties. */
916
export interface GreedySelectorProps {

packages/input-selection/src/GreedySelection/util.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -226,27 +226,6 @@ export const splitChange = async (
226226
return distributeAssets(sortedOutputs, computeMinimumCoinQuantity, tokenBundleSizeExceedsLimit, fee);
227227
};
228228

229-
/**
230-
* Sorts the given TxIn set first by txId and then by index.
231-
*
232-
* @param lhs The left-hand side of the comparison operation.
233-
* @param rhs The left-hand side of the comparison operation.
234-
*/
235-
export const sortTxIn = (lhs: Cardano.TxIn, rhs: Cardano.TxIn) => {
236-
const txIdComparison = lhs.txId.localeCompare(rhs.txId);
237-
if (txIdComparison !== 0) return txIdComparison;
238-
239-
return lhs.index - rhs.index;
240-
};
241-
242-
/**
243-
* Sorts the given Utxo set first by TxIn.
244-
*
245-
* @param lhs The left-hand side of the comparison operation.
246-
* @param rhs The left-hand side of the comparison operation.
247-
*/
248-
export const sortUtxoByTxIn = (lhs: Cardano.Utxo, rhs: Cardano.Utxo) => sortTxIn(lhs[0], rhs[0]);
249-
250229
/**
251230
* Given a set of input and outputs, compute the fee. Then extract the fee from the change output
252231
* with the highest value.

packages/input-selection/src/LargeFirstSelection/LargeFirstInputSelector.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { Cardano, coalesceValueQuantities } from '@cardano-sdk/core';
33
import { ChangeAddressResolver } from '../ChangeAddress';
44
import {
55
ImplicitTokens,
6+
MAX_U64,
67
addTokenMaps,
78
getCoinQuantity,
89
hasNegativeAssetValue,
910
mintToImplicitTokens,
1011
sortByAssetQuantity,
1112
sortByCoins,
13+
sortUtxoByTxIn,
1214
stubMaxSizeAddress,
1315
subtractTokenMaps,
1416
toValues
@@ -21,9 +23,9 @@ import {
2123
SelectionResult
2224
} from '../types';
2325
import { InputSelectionError, InputSelectionFailure } from '../InputSelectionError';
24-
import { MAX_U64 } from '../RoundRobinRandomImprove';
25-
import { computeChangeAndAdjustForFee } from '../RoundRobinRandomImprove/change';
26-
import { sortUtxoByTxIn } from '../GreedySelection';
26+
27+
import { computeChangeAndAdjustForFee } from '../change';
28+
2729
import uniq from 'lodash/uniq.js';
2830

2931
const DETERMINISTIC_RANDOM: typeof Math.random = () => 0;

packages/input-selection/src/RoundRobinRandomImprove/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { Cardano } from '@cardano-sdk/core';
22
import { ChangeAddressResolver } from '../ChangeAddress';
33
import { InputSelectionError, InputSelectionFailure } from '../InputSelectionError';
44
import { InputSelectionParameters, InputSelector, SelectionResult } from '../types';
5-
import { assertIsBalanceSufficient, preProcessArgs, stubMaxSizeAddress, toValues } from '../util';
6-
import { computeChangeAndAdjustForFee } from './change';
5+
import {
6+
MAX_U64,
7+
assertIsBalanceSufficient,
8+
preProcessArgs,
9+
sortUtxoByTxIn,
10+
stubMaxSizeAddress,
11+
toValues
12+
} from '../util';
13+
import { computeChangeAndAdjustForFee } from '../change';
714
import { roundRobinSelection } from './roundRobin';
8-
import { sortUtxoByTxIn } from '../GreedySelection';
9-
10-
export const MAX_U64 = 18_446_744_073_709_551_615n;
1115

1216
interface RoundRobinRandomImproveOptions {
1317
changeAddressResolver: ChangeAddressResolver;

packages/input-selection/src/RoundRobinRandomImprove/change.ts renamed to packages/input-selection/src/change.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Cardano, coalesceValueQuantities } from '@cardano-sdk/core';
2-
import { ComputeMinimumCoinQuantity, TokenBundleSizeExceedsLimit, TxCosts } from '../types';
3-
import { InputSelectionError, InputSelectionFailure } from '../InputSelectionError';
2+
import { ComputeMinimumCoinQuantity, TokenBundleSizeExceedsLimit, TxCosts } from './types';
3+
import { InputSelectionError, InputSelectionFailure } from './InputSelectionError';
44
import {
55
RequiredImplicitValue,
66
UtxoSelection,
77
assetQuantitySelector,
88
getCoinQuantity,
99
stubMaxSizeAddress,
1010
toValues
11-
} from '../util';
11+
} from './util';
1212
import minBy from 'lodash/minBy.js';
1313
import orderBy from 'lodash/orderBy.js';
1414
import pick from 'lodash/pick.js';

packages/input-selection/src/util.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,33 @@ import { ComputeMinimumCoinQuantity, ImplicitValue, TokenBundleSizeExceedsLimit
66
import { InputSelectionError, InputSelectionFailure } from './InputSelectionError';
77
import uniq from 'lodash/uniq.js';
88

9+
export const MAX_U64 = 18_446_744_073_709_551_615n;
10+
911
export const stubMaxSizeAddress = Cardano.PaymentAddress(
1012
'addr_test1qqydn46r6mhge0kfpqmt36m6q43knzsd9ga32n96m89px3nuzcjqw982pcftgx53fu5527z2cj2tkx2h8ux2vxsg475qypp3m9'
1113
);
1214

15+
/**
16+
* Sorts the given TxIn set first by txId and then by index.
17+
*
18+
* @param lhs The left-hand side of the comparison operation.
19+
* @param rhs The left-hand side of the comparison operation.
20+
*/
21+
export const sortTxIn = (lhs: Cardano.TxIn, rhs: Cardano.TxIn) => {
22+
const txIdComparison = lhs.txId.localeCompare(rhs.txId);
23+
if (txIdComparison !== 0) return txIdComparison;
24+
25+
return lhs.index - rhs.index;
26+
};
27+
28+
/**
29+
* Sorts the given Utxo set first by TxIn.
30+
*
31+
* @param lhs The left-hand side of the comparison operation.
32+
* @param rhs The left-hand side of the comparison operation.
33+
*/
34+
export const sortUtxoByTxIn = (lhs: Cardano.Utxo, rhs: Cardano.Utxo) => sortTxIn(lhs[0], rhs[0]);
35+
1336
export interface ImplicitTokens {
1437
spend(assetId: Cardano.AssetId): bigint;
1538
input(assetId: Cardano.AssetId): bigint;

packages/input-selection/test/change.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Cardano } from '@cardano-sdk/core';
2-
import { coalesceChangeBundlesForMinCoinRequirement } from '../src/RoundRobinRandomImprove/change';
2+
import { coalesceChangeBundlesForMinCoinRequirement } from '../src/change';
33

44
const TOKEN1_ASSET_ID = Cardano.AssetId('5c677ba4dd295d9286e0e22786fea9ed735a6ae9c07e7a45ae4d95c84249530000');
55
const TOKEN2_ASSET_ID = Cardano.AssetId('5c677ba4dd295d9286e0e22786fea9ed735a6ae9c07e7a45ae4d95c84249530001');

0 commit comments

Comments
 (0)