-
-
Notifications
You must be signed in to change notification settings - Fork 274
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
258 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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
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: '2', 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 }), | ||
]; | ||
|
||
const findTx = (txid: string) => ACCOUNT_TRANSACTIONS.find(tx => tx.txid === txid); | ||
|
||
describe(sortUtxos.name, () => { | ||
it('sorts UTXOs by newest first', () => { | ||
const sortedUtxos = sortUtxos(UTXOS, 'newestFirst', ACCOUNT_TRANSACTIONS); | ||
expect( | ||
sortedUtxos.map(it => [ | ||
it.blockHeight ?? findTx(it.txid)?.blockTime, | ||
`${it.txid}:${it.vout}`, // for stable sorting | ||
]), | ||
).toEqual([ | ||
[2, 'txid3:0'], | ||
[1, 'txid2:1'], | ||
[1, 'txid2:0'], | ||
[undefined, 'txid1:0'], | ||
]); | ||
}); | ||
|
||
it('sorts UTXOs by oldest first', () => { | ||
const sortedUtxos = sortUtxos(UTXOS, 'oldestFirst', ACCOUNT_TRANSACTIONS); | ||
expect( | ||
sortedUtxos.map(it => [ | ||
it.blockHeight ?? findTx(it.txid)?.blockTime, | ||
`${it.txid}:${it.vout}`, // for stable sorting | ||
]), | ||
).toEqual([ | ||
[undefined, 'txid1:0'], | ||
[1, 'txid2:0'], | ||
[1, 'txid2:1'], | ||
[2, 'txid3:0'], | ||
]); | ||
}); | ||
|
||
it('sorts by size, largest first', () => { | ||
const sortedUtxos = sortUtxos(UTXOS.slice(0, 2), 'largestFirst', ACCOUNT_TRANSACTIONS); | ||
expect(sortedUtxos.map(it => it.amount)).toEqual(['2', '1']); | ||
}); | ||
|
||
it('sorts by size, smallest first', () => { | ||
const sortedUtxos = sortUtxos(UTXOS.slice(0, 2), 'smallestFirst', ACCOUNT_TRANSACTIONS); | ||
expect(sortedUtxos.map(it => it.amount)).toEqual(['1', '2']); | ||
}); | ||
|
||
it('sorts by secondary sorting by `txid` and `vout` in case of same values', () => { | ||
const sortedUtxos = sortUtxos(UTXOS.slice(1, 4), 'smallestFirst', ACCOUNT_TRANSACTIONS); | ||
expect(sortedUtxos.map(it => `${it.txid}:${it.vout}`)).toEqual([ | ||
'txid2:0', | ||
'txid2:1', | ||
'txid3:0', | ||
]); | ||
}); | ||
|
||
it('returns 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,79 @@ | ||
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 wrapSecondarySorting = | ||
(sortFunction: UtxoSortingFunctionWithContext): UtxoSortingFunctionWithContext => | ||
context => | ||
(a, b) => { | ||
const result = sortFunction(context)(a, b); | ||
|
||
if (result !== 0) { | ||
return result; | ||
} | ||
|
||
return performSecondarySorting(a, b); | ||
}; | ||
|
||
const sortFromLargestToSmallest: UtxoSortingFunctionWithContext = () => (a, b) => | ||
new BigNumber(b.amount).comparedTo(new BigNumber(a.amount)); | ||
|
||
const sortFromNewestToOldest: UtxoSortingFunctionWithContext = | ||
({ accountTransactions }) => | ||
(a, b) => { | ||
if (a.blockHeight > 0 && b.blockHeight > 0) { | ||
return b.blockHeight - a.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; | ||
}; | ||
|
||
return getBlockTime(b.txid) - getBlockTime(a.txid); | ||
} | ||
}; | ||
|
||
const utxoSortMap: Record<UtxoSorting, UtxoSortingFunctionWithContext> = { | ||
largestFirst: wrapSecondarySorting(sortFromLargestToSmallest), | ||
smallestFirst: | ||
context => | ||
(...params) => | ||
wrapSecondarySorting(sortFromLargestToSmallest)(context)(...params) * -1, | ||
|
||
newestFirst: wrapSecondarySorting(sortFromNewestToOldest), | ||
oldestFirst: | ||
context => | ||
(...params) => | ||
wrapSecondarySorting(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