XO wallet integration
This commit is contained in:
@@ -3,3 +3,4 @@ export * from './blockchain-electrum.js';
|
||||
export * from './blockchain-inmemory.js';
|
||||
export * from './traits.js';
|
||||
export * from './types.js';
|
||||
export * from './utils.js';
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// TODO: If we end up with several utilities, we might want to consider a refactor:
|
||||
// 1. Convert BlockchainTransactions into a class with a sort function.
|
||||
// 2. Have WalletTransactions extends from BlockchainTransactions.
|
||||
|
||||
import { type BlockchainTransactions } from './types.js';
|
||||
|
||||
export function sortTransactions(
|
||||
transactions: BlockchainTransactions,
|
||||
order: 'ASC' | 'DESC',
|
||||
): BlockchainTransactions {
|
||||
return transactions.sort((a, b) => {
|
||||
// Name the outcomes, or I'll go insane.
|
||||
const aFirst = order === 'DESC' ? -1 : 1;
|
||||
const bFirst = order === 'DESC' ? 1 : -1;
|
||||
|
||||
// Sort by mempool, then blockheight, then dependency.
|
||||
// height = 0 -- mempool
|
||||
// height > 0 -- confirmed
|
||||
// height < 0 -- mempool with dependencies
|
||||
|
||||
// If a is dependant on a tx in the mempool, while b is in the mempool or confirmed, sort a before b.
|
||||
if (a.height <= -1 && b.height >= 0) {
|
||||
return aFirst;
|
||||
}
|
||||
|
||||
// If b is dependant on a tx in the mempool, while a is in the mempool or confirmed, sort b before a.
|
||||
if (b.height <= -1 && a.height >= 0) {
|
||||
return bFirst;
|
||||
}
|
||||
|
||||
// If a is in the mempool, while b is confirmed, sort a before b.
|
||||
if (a.height <= 0 && b.height > 0) {
|
||||
return aFirst;
|
||||
}
|
||||
|
||||
// If b is in the mempool, while a is confirmed, sort b before a.
|
||||
if (b.height <= 0 && a.height > 0) {
|
||||
return bFirst;
|
||||
}
|
||||
|
||||
// If a has a higher blockheight than b, sort a before b.
|
||||
if (a.height > b.height) {
|
||||
return aFirst;
|
||||
}
|
||||
|
||||
// If b has a higher blockheight than a, sort b before a.
|
||||
if (b.height > a.height) {
|
||||
return bFirst;
|
||||
}
|
||||
|
||||
// There was no criteria to sort by, so return 0.
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user