Skip to content

Commit 3312fd5

Browse files
committed
refactor(wallet): create enums for event names
1 parent f7f7d45 commit 3312fd5

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

packages/wallet/src/InMemoryTransactionTracker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { CardanoProvider, ProviderError, CardanoSerializationLib, CSL, ProviderF
55
import { TransactionError, TransactionFailure } from './TransactionError';
66
import { dummyLogger, Logger } from 'ts-log';
77
import delay from 'delay';
8+
import { TransactionTrackerEvent } from '.';
89

910
export type Milliseconds = number;
1011

@@ -47,7 +48,7 @@ export class InMemoryTransactionTracker extends Emittery<TransactionTrackerEvent
4748

4849
const promise = this.#trackTransaction(hash, invalidHereafter);
4950
this.#pendingTransactions.set(hash, promise);
50-
this.emit('transaction', { transaction, confirmed: promise }).catch(this.#logger.error);
51+
this.emit(TransactionTrackerEvent.Transaction, { transaction, confirmed: promise }).catch(this.#logger.error);
5152
void promise.catch(() => void 0).then(() => this.#pendingTransactions.delete(hash));
5253

5354
return promise;

packages/wallet/src/InMemoryUtxoRepository.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { CardanoProvider, Ogmios, CardanoSerializationLib, CSL } from '@cardano-
55
import { dummyLogger, Logger } from 'ts-log';
66
import { ImplicitCoin, InputSelector, SelectionConstraints, SelectionResult } from '@cardano-sdk/cip2';
77
import { KeyManager } from './KeyManagement';
8-
import { OnTransactionArgs, TransactionTracker, UtxoRepositoryEvents } from '.';
8+
import {
9+
OnTransactionArgs,
10+
TransactionTracker,
11+
TransactionTrackerEvent,
12+
UtxoRepositoryEvent,
13+
UtxoRepositoryEvents
14+
} from '.';
915
import { cslToOgmios } from '@cardano-sdk/core/src/Ogmios';
1016
import Emittery from 'emittery';
1117
import { TransactionError, TransactionFailure } from './TransactionError';
@@ -47,7 +53,7 @@ export class InMemoryUtxoRepository extends Emittery<UtxoRepositoryEvents> imple
4753
this.#delegationAndRewards = { rewards: undefined, delegate: undefined };
4854
this.#inputSelector = inputSelector;
4955
this.#keyManager = keyManager;
50-
txTracker.on('transaction', (args) => {
56+
txTracker.on(TransactionTrackerEvent.Transaction, (args) => {
5157
// not blocking to make it testable easier
5258
this.#onTransaction(args).catch(this.#logger.error);
5359
});
@@ -136,7 +142,7 @@ export class InMemoryUtxoRepository extends Emittery<UtxoRepositoryEvents> imple
136142
} catch (error) {
137143
unlock(false);
138144
if (!(error instanceof TransactionError) || error.reason !== TransactionFailure.Timeout) {
139-
await this.emit('transactionUntracked', transaction).catch(this.#logger.error);
145+
await this.emit(UtxoRepositoryEvent.TransactionUntracked, transaction).catch(this.#logger.error);
140146
}
141147
}
142148
}

packages/wallet/src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { ImplicitCoin, SelectionConstraints, SelectionResult } from '@cardano-sd
33
import { CSL } from '@cardano-sdk/core';
44
import Emittery from 'emittery';
55

6+
export enum UtxoRepositoryEvent {
7+
TransactionUntracked = 'transactionUntracked'
8+
}
9+
610
export type UtxoRepositoryEvents = { transactionUntracked: CSL.Transaction };
711
export interface UtxoRepository extends Emittery<UtxoRepositoryEvents> {
812
allUtxos: Schema.Utxo;
@@ -25,6 +29,11 @@ export interface OnTransactionArgs {
2529
*/
2630
confirmed: Promise<void>;
2731
}
32+
33+
export enum TransactionTrackerEvent {
34+
Transaction = 'transaction'
35+
}
36+
2837
export type TransactionTrackerEvents = { transaction: OnTransactionArgs };
2938
export interface TransactionTracker extends Emittery<TransactionTrackerEvents> {
3039
/**

packages/wallet/test/InMemoryTransactionTracker.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { InMemoryTransactionTracker } from '../src/InMemoryTransactionTracker';
44
import { TransactionFailure } from '../src/TransactionError';
55
import { ledgerTip, providerStub, ProviderStub, queryTransactionsResult } from './ProviderStub';
66
import mockDelay from 'delay';
7+
import { TransactionTrackerEvent } from '../src';
78

89
jest.mock('delay', () => jest.fn().mockResolvedValue(void 0));
910

@@ -42,7 +43,7 @@ describe('InMemoryTransactionTracker', () => {
4243

4344
beforeEach(() => {
4445
onTransaction = jest.fn();
45-
txTracker.on('transaction', onTransaction);
46+
txTracker.on(TransactionTrackerEvent.Transaction, onTransaction);
4647
});
4748

4849
it('invalid transaction (no ttl)', async () => {

packages/wallet/test/InMemoryUtxoRepository.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { roundRobinRandomImprove, InputSelector } from '@cardano-sdk/cip2';
33
import { loadCardanoSerializationLib, CardanoSerializationLib, CSL, Ogmios } from '@cardano-sdk/core';
44
import { flushPromises, SelectionConstraints } from '@cardano-sdk/util-dev';
55
import { providerStub, delegate, rewards, ProviderStub, utxo, delegationAndRewards } from './ProviderStub';
6-
import { InMemoryUtxoRepository, KeyManagement, UtxoRepository } from '../src';
6+
import {
7+
InMemoryUtxoRepository,
8+
KeyManagement,
9+
TransactionTrackerEvent,
10+
UtxoRepository,
11+
UtxoRepositoryEvent
12+
} from '../src';
713
import { MockTransactionTracker } from './mockTransactionTracker';
814
import { ogmiosToCsl } from '@cardano-sdk/core/src/Ogmios';
915
import { TxIn, TxOut } from '@cardano-ogmios/schema';
@@ -87,7 +93,7 @@ describe('InMemoryUtxoRepository', () => {
8793
let onTransactionUntracked: jest.Mock;
8894

8995
const trackTransaction = async (confirmed: Promise<void>) => {
90-
await txTracker.emit('transaction', {
96+
await txTracker.emit(TransactionTrackerEvent.Transaction, {
9197
transaction,
9298
confirmed
9399
});
@@ -109,7 +115,7 @@ describe('InMemoryUtxoRepository', () => {
109115
await utxoRepository.sync();
110116
numUtxoPreTransaction = utxoRepository.allUtxos.length;
111117
onTransactionUntracked = jest.fn();
112-
utxoRepository.on('transactionUntracked', onTransactionUntracked);
118+
utxoRepository.on(UtxoRepositoryEvent.TransactionUntracked, onTransactionUntracked);
113119
});
114120

115121
it('preconditions', () => {

packages/wallet/test/integration/withdrawal.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
TransactionError,
1212
TransactionFailure,
1313
TransactionTracker,
14-
UtxoRepository
14+
UtxoRepository,
15+
UtxoRepositoryEvent
1516
} from '@cardano-sdk/wallet';
1617
import { providerStub } from '../ProviderStub';
1718
// Not testing with a real provider
@@ -50,7 +51,7 @@ describe('integration/withdrawal', () => {
5051

5152
it('does not throw', async () => {
5253
// This is not testing anything, just a usage example
53-
utxoRepository.on('transactionUntracked', (tx) => {
54+
utxoRepository.on(UtxoRepositoryEvent.TransactionUntracked, (tx) => {
5455
// UtxoRepository is not sure whether it's UTxO can be spent due to failing to track transaction confirmation.
5556
// SubmitTxResult.confirmed has rejected. Calling trackTransaction will lock UTxO again:
5657
txTracker.trackTransaction(tx).catch((error) => {

0 commit comments

Comments
 (0)