Skip to content

Commit 7c8b6e9

Browse files
feat(wallet): the wallet now only fetches UTXOs on tx history change
1 parent edb73ad commit 7c8b6e9

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

packages/wallet/src/Wallets/BaseWallet.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import {
5151
createUtxoTracker,
5252
createWalletUtil,
5353
currentEpochTracker,
54-
distinctBlock,
5554
distinctEraSummaries
5655
} from '../services';
5756
import { AddressType, Bip32Account, GroupedAddress, WitnessedTx, Witnesser, util } from '@cardano-sdk/key-management';
@@ -397,7 +396,6 @@ export class BaseWallet implements ObservableWallet {
397396
store: stores.tip,
398397
syncStatus: this.syncStatus
399398
});
400-
const tipBlockHeight$ = distinctBlock(this.tip$);
401399

402400
this.txSubmitProvider = new SmartTxSubmitProvider(
403401
{ retryBackoffConfig },
@@ -499,11 +497,11 @@ export class BaseWallet implements ObservableWallet {
499497

500498
this.utxo = createUtxoTracker({
501499
addresses$,
500+
history$: this.transactions.history$,
502501
logger: contextLogger(this.#logger, 'utxo'),
503502
onFatalError,
504503
retryBackoffConfig,
505504
stores,
506-
tipBlockHeight$,
507505
transactionsInFlight$: this.transactions.outgoing.inFlight$,
508506
utxoProvider: this.utxoProvider
509507
});

packages/wallet/src/services/UtxoTracker.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { RetryBackoffConfig } from 'backoff-rxjs';
66
import { TxInFlight, UtxoTracker } from './types';
77
import { WalletStores } from '../persistence';
88
import { coldObservableProvider } from '@cardano-sdk/util-rxjs';
9+
import { sortUtxoByTxIn } from '@cardano-sdk/input-selection';
910
import chunk from 'lodash/chunk.js';
1011
import uniqWith from 'lodash/uniqWith.js';
1112

@@ -17,7 +18,7 @@ export interface UtxoTrackerProps {
1718
addresses$: Observable<Cardano.PaymentAddress[]>;
1819
stores: Pick<WalletStores, 'utxo' | 'unspendableUtxo'>;
1920
transactionsInFlight$: Observable<TxInFlight[]>;
20-
tipBlockHeight$: Observable<Cardano.BlockNo>;
21+
history$: Observable<Cardano.HydratedTx[]>;
2122
retryBackoffConfig: RetryBackoffConfig;
2223
logger: Logger;
2324
onFatalError?: (value: unknown) => void;
@@ -31,7 +32,7 @@ export interface UtxoTrackerInternals {
3132
export const createUtxoProvider = (
3233
utxoProvider: UtxoProvider,
3334
addresses$: Observable<Cardano.PaymentAddress[]>,
34-
tipBlockHeight$: Observable<Cardano.BlockNo>,
35+
history$: Observable<Cardano.HydratedTx[]>,
3536
retryBackoffConfig: RetryBackoffConfig,
3637
onFatalError?: (value: unknown) => void
3738
) =>
@@ -49,10 +50,10 @@ export const createUtxoProvider = (
4950
utxos = [...utxos, ...(await utxoProvider.utxoByAddresses({ addresses }))];
5051
}
5152

52-
return utxos;
53+
return utxos.sort(sortUtxoByTxIn);
5354
},
5455
retryBackoffConfig,
55-
trigger$: tipBlockHeight$
56+
trigger$: history$
5657
})
5758
)
5859
);
@@ -64,13 +65,13 @@ export const createUtxoTracker = (
6465
stores,
6566
transactionsInFlight$,
6667
retryBackoffConfig,
67-
tipBlockHeight$,
68+
history$,
6869
logger,
6970
onFatalError
7071
}: UtxoTrackerProps,
7172
{
7273
utxoSource$ = new PersistentCollectionTrackerSubject<Cardano.Utxo>(
73-
() => createUtxoProvider(utxoProvider, addresses$, tipBlockHeight$, retryBackoffConfig, onFatalError),
74+
() => createUtxoProvider(utxoProvider, addresses$, history$, retryBackoffConfig, onFatalError),
7475
stores.utxo
7576
),
7677
unspendableUtxoSource$ = new PersistentCollectionTrackerSubject(

packages/wallet/src/services/util/equals.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,27 @@ export const tipEquals = (a: Cardano.Tip, b: Cardano.Tip) => a.hash === b.hash;
77

88
export const txEquals = (a: Cardano.HydratedTx, b: Cardano.HydratedTx) => a.id === b.id;
99

10+
export const sameSortedArrayItems = <T>(arrayA: T[], arrayB: T[], itemEquals: (a: T, b: T) => boolean): boolean => {
11+
if (arrayA.length !== arrayB.length) return false;
12+
13+
for (const [i, element] of arrayA.entries()) {
14+
if (!itemEquals(element, arrayB[i])) {
15+
return false;
16+
}
17+
}
18+
19+
return true;
20+
};
21+
1022
export const transactionsEquals = (a: Cardano.HydratedTx[], b: Cardano.HydratedTx[]) => sameArrayItems(a, b, txEquals);
1123

1224
export const txInEquals = (a: Cardano.TxIn, b: Cardano.TxIn) => a.txId === b.txId && a.index === b.index;
1325

14-
export const utxoEquals = (a: Cardano.Utxo[], b: Cardano.Utxo[]) =>
15-
sameArrayItems(a, b, ([aTxIn], [bTxIn]) => txInEquals(aTxIn, bTxIn));
26+
export const utxoEquals = (a: Cardano.Utxo[], b: Cardano.Utxo[]) => {
27+
if (a === b) return true;
28+
29+
return sameSortedArrayItems(a, b, ([aTxIn], [bTxIn]) => txInEquals(aTxIn, bTxIn));
30+
};
1631

1732
export const eraSummariesEquals = (a: EraSummary[], b: EraSummary[]) =>
1833
sameArrayItems(a, b, (es1, es2) => es1.start.slot === es2.start.slot);

packages/wallet/test/services/UtxoTracker.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const createStubOutputs = (numOutputs: number) =>
2020
describe('createUtxoTracker', () => {
2121
// these variables are not relevant for this test, overwriting utxoSource$
2222
let retryBackoffConfig: RetryBackoffConfig;
23-
let tipBlockHeight$: Observable<Cardano.BlockNo>;
23+
let history$: Observable<Cardano.HydratedTx[]>;
2424
let utxoProvider: UtxoProvider;
2525
const logger = dummyLogger;
2626

@@ -88,13 +88,13 @@ describe('createUtxoTracker', () => {
8888
const utxoTracker = createUtxoTracker(
8989
{
9090
addresses$: cold('a|', { a: [ownAddress!] }),
91+
history$,
9192
logger,
9293
retryBackoffConfig,
9394
stores: {
9495
unspendableUtxo: store,
9596
utxo: store
9697
},
97-
tipBlockHeight$,
9898
transactionsInFlight$,
9999
utxoProvider
100100
},
@@ -142,13 +142,13 @@ describe('createUtxoTracker', () => {
142142
const utxoTracker = createUtxoTracker(
143143
{
144144
addresses$: cold('a', { a: [address!] }),
145+
history$,
145146
logger,
146147
retryBackoffConfig,
147148
stores: {
148149
unspendableUtxo: store,
149150
utxo: store
150151
},
151-
tipBlockHeight$,
152152
transactionsInFlight$,
153153
utxoProvider
154154
},
@@ -172,13 +172,13 @@ describe('createUtxoTracker', () => {
172172
const utxoTracker = createUtxoTracker(
173173
{
174174
addresses$: cold('a|', { a: [address!] }),
175+
history$,
175176
logger,
176177
retryBackoffConfig,
177178
stores: {
178179
unspendableUtxo: store,
179180
utxo: store
180181
},
181-
tipBlockHeight$,
182182
transactionsInFlight$,
183183
utxoProvider
184184
},
@@ -211,13 +211,13 @@ describe('createUtxoTracker', () => {
211211
const utxoTracker = createUtxoTracker(
212212
{
213213
addresses$: cold('a|', { a: [address!] }),
214+
history$,
214215
logger,
215216
retryBackoffConfig,
216217
stores: {
217218
unspendableUtxo: store,
218219
utxo: store
219220
},
220-
tipBlockHeight$,
221221
transactionsInFlight$,
222222
utxoProvider
223223
},
@@ -241,13 +241,13 @@ describe('createUtxoTracker', () => {
241241
const utxoTracker = createUtxoTracker(
242242
{
243243
addresses$: cold('a|', { a: [address!] }),
244+
history$,
244245
logger,
245246
retryBackoffConfig,
246247
stores: {
247248
unspendableUtxo: store,
248249
utxo: store
249250
},
250-
tipBlockHeight$,
251251
transactionsInFlight$,
252252
utxoProvider
253253
},

0 commit comments

Comments
 (0)