-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(suite, suite-common): add utxo sorting
- Loading branch information
Showing
9 changed files
with
232 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/suite/src/utils/wallet/__tests__/utxoSortingUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { testMocks } from '@suite-common/test-utils'; | ||
|
||
import { sortUtxos } from '../utxoSortingUtils'; | ||
|
||
const UTXOS = [ | ||
testMocks.getUtxo({ amount: '1', blockHeight: undefined, txid: 'txid1', vout: 0 }), | ||
testMocks.getUtxo({ amount: '1', blockHeight: undefined, txid: 'txid2', vout: 1 }), | ||
testMocks.getUtxo({ amount: '2', blockHeight: 1, txid: 'txid2', vout: 0 }), | ||
testMocks.getUtxo({ amount: '2', blockHeight: 2, txid: 'txid3', vout: 0 }), | ||
]; | ||
|
||
const ACCOUNT_TRANSACTIONS = [ | ||
testMocks.getWalletTransaction({ txid: 'txid1', blockTime: undefined }), | ||
testMocks.getWalletTransaction({ txid: 'txid2', blockTime: 1 }), | ||
testMocks.getWalletTransaction({ txid: 'txid3', blockTime: 2 }), | ||
]; | ||
|
||
describe('sortUtxos', () => { | ||
it('should sort UTXOs by newest first', () => { | ||
const sortedUtxos = sortUtxos(UTXOS, 'newestFirst', ACCOUNT_TRANSACTIONS); | ||
expect(sortedUtxos).toEqual([UTXOS[3], UTXOS[1], UTXOS[2], UTXOS[0]]); | ||
}); | ||
|
||
it('should sort UTXOs by oldest first', () => { | ||
const sortedUtxos = sortUtxos(UTXOS, 'oldestFirst', ACCOUNT_TRANSACTIONS); | ||
expect(sortedUtxos).toEqual([UTXOS[0], UTXOS[2], UTXOS[1], UTXOS[3]]); | ||
}); | ||
|
||
it('should sort UTXOs by largest first', () => { | ||
const sortedUtxos = sortUtxos(UTXOS, 'largestFirst', ACCOUNT_TRANSACTIONS); | ||
expect(sortedUtxos).toEqual([UTXOS[3], UTXOS[2], UTXOS[1], UTXOS[0]]); | ||
}); | ||
|
||
it('should sort UTXOs by smallest first', () => { | ||
const sortedUtxos = sortUtxos(UTXOS, 'smallestFirst', ACCOUNT_TRANSACTIONS); | ||
expect(sortedUtxos).toEqual([UTXOS[0], UTXOS[1], UTXOS[2], UTXOS[3]]); | ||
}); | ||
|
||
it('should return the original array if utxoSorting is undefined', () => { | ||
const sortedUtxos = sortUtxos(UTXOS, undefined, ACCOUNT_TRANSACTIONS); | ||
expect(sortedUtxos).toEqual(UTXOS); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { UtxoSorting, WalletAccountTransaction } from '@suite-common/wallet-types'; | ||
import type { AccountUtxo } from '@trezor/connect'; | ||
import { BigNumber } from '@trezor/utils'; | ||
|
||
type UtxoSortingFunction = (a: AccountUtxo, b: AccountUtxo) => number; | ||
|
||
type UtxoSortingFunctionWithContext = (context: { | ||
accountTransactions: WalletAccountTransaction[]; | ||
}) => UtxoSortingFunction; | ||
|
||
const performSecondarySorting: UtxoSortingFunction = (a, b) => { | ||
const secondaryComparison = b.txid.localeCompare(a.txid); | ||
if (secondaryComparison === 0) { | ||
return b.vout - a.vout; | ||
} | ||
|
||
return secondaryComparison; | ||
}; | ||
|
||
const sortFromLargestToSmallest: UtxoSortingFunctionWithContext = () => (a, b) => { | ||
const comparisonResult = new BigNumber(b.amount).comparedTo(new BigNumber(a.amount)); | ||
|
||
if (comparisonResult === 0) { | ||
return performSecondarySorting(a, b); | ||
} | ||
|
||
return comparisonResult; | ||
}; | ||
|
||
const sortFromNewestToOldest: UtxoSortingFunctionWithContext = | ||
({ accountTransactions }) => | ||
(a, b) => { | ||
let valueA; | ||
let valueB; | ||
if (a.blockHeight > 0 && b.blockHeight > 0) { | ||
valueA = a.blockHeight; | ||
valueB = b.blockHeight; | ||
} else { | ||
// Pending transactions do not have blockHeight, so we must use blockTime of the transaction instead. | ||
const getBlockTime = (txid: string) => { | ||
const transaction = accountTransactions.find( | ||
transaction => transaction.txid === txid, | ||
); | ||
|
||
return transaction?.blockTime ?? 0; | ||
}; | ||
valueA = getBlockTime(a.txid); | ||
valueB = getBlockTime(b.txid); | ||
} | ||
|
||
const comparisonResult = valueB - valueA; | ||
|
||
if (comparisonResult === 0) { | ||
return performSecondarySorting(a, b); | ||
} | ||
|
||
return comparisonResult; | ||
}; | ||
|
||
const utxoSortMap: Record<UtxoSorting, UtxoSortingFunctionWithContext> = { | ||
largestFirst: sortFromLargestToSmallest, | ||
smallestFirst: | ||
context => | ||
(...params) => | ||
sortFromLargestToSmallest(context)(...params) * -1, | ||
|
||
newestFirst: sortFromNewestToOldest, | ||
oldestFirst: | ||
context => | ||
(...params) => | ||
sortFromNewestToOldest(context)(...params) * -1, | ||
}; | ||
|
||
export const sortUtxos = ( | ||
utxos: AccountUtxo[], | ||
utxoSorting: UtxoSorting | undefined, | ||
accountTransactions: WalletAccountTransaction[], | ||
): AccountUtxo[] => { | ||
if (utxoSorting === undefined) { | ||
return utxos; | ||
} | ||
|
||
return [...utxos].sort(utxoSortMap[utxoSorting]({ accountTransactions })); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ages/suite/src/views/wallet/send/Options/BitcoinOptions/CoinControl/UtxoSortingSelect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ReactNode } from 'react'; | ||
|
||
import { UtxoSorting } from '@suite-common/wallet-types'; | ||
import { Option, Select } from '@trezor/components'; | ||
|
||
import { Translation } from 'src/components/suite'; | ||
import { useSendFormContext } from 'src/hooks/wallet'; | ||
|
||
const sortingOptions: { value: UtxoSorting; label: ReactNode }[] = [ | ||
{ value: 'newestFirst', label: <Translation id="TR_NEWEST_FIRST" /> }, | ||
{ value: 'oldestFirst', label: <Translation id="TR_OLDEST_FIRST" /> }, | ||
{ value: 'smallestFirst', label: <Translation id="TR_SMALLEST_FIRST" /> }, | ||
{ value: 'largestFirst', label: <Translation id="TR_LARGEST_FIRST" /> }, | ||
]; | ||
|
||
export const UtxoSortingSelect = () => { | ||
const { | ||
utxoSelection: { utxoSorting, selectUtxoSorting }, | ||
} = useSendFormContext(); | ||
|
||
const selectedOption = sortingOptions.find(option => option.value === utxoSorting); | ||
|
||
const handleChange = ({ value }: Option) => selectUtxoSorting(value); | ||
|
||
return ( | ||
<Select | ||
options={sortingOptions} | ||
value={selectedOption} | ||
onChange={handleChange} | ||
size="small" | ||
width={240} | ||
data-testid="@coin-control/utxo-sorting-select" | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters