-
Notifications
You must be signed in to change notification settings - Fork 55
/
pouchDbWalletStores.ts
92 lines (87 loc) · 4.88 KB
/
pouchDbWalletStores.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Assets } from '../../types';
import { Cardano, EraSummary, Reward } from '@cardano-sdk/core';
import { CreatePouchDbStoresDependencies } from './types';
import { EMPTY, combineLatest, map } from 'rxjs';
import { GroupedAddress, WitnessedTx } from '@cardano-sdk/key-management';
import { OutgoingOnChainTx, TxInFlight } from '../../services';
import { PouchDbCollectionStore } from './PouchDbCollectionStore';
import { PouchDbDocumentStore } from './PouchDbDocumentStore';
import { PouchDbKeyValueStore } from './PouchDbKeyValueStore';
import { WalletStores } from '../types';
export class PouchDbTipStore extends PouchDbDocumentStore<Cardano.Tip> {}
export class PouchDbProtocolParametersStore extends PouchDbDocumentStore<Cardano.ProtocolParameters> {}
export class PouchDbGenesisParametersStore extends PouchDbDocumentStore<Cardano.CompactGenesis> {}
export class PouchDbEraSummariesStore extends PouchDbDocumentStore<EraSummary[]> {}
export class PouchDbAssetsStore extends PouchDbDocumentStore<Assets> {}
export class PouchDbAddressesStore extends PouchDbDocumentStore<GroupedAddress[]> {}
export class PouchDbInFlightTransactionsStore extends PouchDbDocumentStore<TxInFlight[]> {}
export class PouchDbVolatileTransactionsStore extends PouchDbDocumentStore<OutgoingOnChainTx[]> {}
export class PouchDbPolicyIdsStore extends PouchDbDocumentStore<Cardano.PolicyId[]> {}
export class PouchDbSignedTransactionsStore extends PouchDbDocumentStore<WitnessedTx[]> {}
export class PouchDbTransactionsStore extends PouchDbCollectionStore<Cardano.HydratedTx> {}
export class PouchDbUtxoStore extends PouchDbCollectionStore<Cardano.Utxo> {}
export class PouchDbRewardsHistoryStore extends PouchDbKeyValueStore<Cardano.RewardAccount, Reward[]> {}
export class PouchDbStakePoolsStore extends PouchDbKeyValueStore<Cardano.PoolId, Cardano.StakePool> {}
export class PouchDbRewardsBalancesStore extends PouchDbKeyValueStore<Cardano.RewardAccount, Cardano.Lovelace> {}
/**
* @param {string} walletName used to derive underlying db names
*/
export const createPouchDbWalletStores = (
walletName: string,
{ logger }: CreatePouchDbStoresDependencies
): WalletStores => {
const baseDbName = walletName.replace(/[^\da-z]/gi, '');
const docsDbName = `${baseDbName}Docs`;
return {
addresses: new PouchDbAddressesStore(docsDbName, 'addresses', logger),
assets: new PouchDbAssetsStore(docsDbName, 'assets', logger),
destroy() {
if (!this.destroyed) {
// since the database of document stores is shared, destroying any document store destroys all of them
this.destroyed = true;
logger.debug('Destroying PouchDb WalletStores...');
const destroyDocumentsDb = this.tip.destroy();
return combineLatest([
destroyDocumentsDb,
this.transactions.destroy(),
this.utxo.destroy(),
this.unspendableUtxo.destroy(),
this.rewardsHistory.destroy(),
this.stakePools.destroy(),
this.rewardsBalances.destroy()
]).pipe(map(() => void 0));
}
return EMPTY;
},
destroyed: false,
eraSummaries: new PouchDbEraSummariesStore(docsDbName, 'EraSummaries', logger),
genesisParameters: new PouchDbGenesisParametersStore(docsDbName, 'genesisParameters', logger),
inFlightTransactions: new PouchDbInFlightTransactionsStore(docsDbName, 'transactionsInFlight_v2', logger),
policyIds: new PouchDbPolicyIdsStore(docsDbName, 'policyIds', logger),
protocolParameters: new PouchDbProtocolParametersStore(docsDbName, 'protocolParameters', logger),
rewardsBalances: new PouchDbRewardsBalancesStore(`${baseDbName}RewardsBalances`, logger),
rewardsHistory: new PouchDbRewardsHistoryStore(`${baseDbName}RewardsHistory`, logger),
signedTransactions: new PouchDbSignedTransactionsStore(docsDbName, 'signedTransactions', logger),
stakePools: new PouchDbStakePoolsStore(`${baseDbName}StakePools`, logger),
tip: new PouchDbTipStore(docsDbName, 'tip', logger),
transactions: new PouchDbTransactionsStore(
{
computeDocId: ({ blockHeader: { blockNo }, index }) =>
/**
* Multiplied by 100k to distinguish between blockNo=1,index=0 and blockNo=0,index=1
* Assuming there can never be more >=100k transactions in a block
*/
(blockNo * 100_000 + index).toString(),
dbName: `${baseDbName}Transactions_v2`
},
logger
),
// Review: technically this could also cause a conflict when updating.
// However it is extremely unlikely that it would have inline datums,
// and renaming this store is not an option as it's being used
// for storing collateral settings
unspendableUtxo: new PouchDbUtxoStore({ dbName: `${baseDbName}UnspendableUtxo` }, logger),
utxo: new PouchDbUtxoStore({ dbName: `${baseDbName}Utxo_v2` }, logger),
volatileTransactions: new PouchDbVolatileTransactionsStore(docsDbName, 'volatileTransactions_v3', logger)
};
};