-
Notifications
You must be signed in to change notification settings - Fork 54
/
withMint.ts
45 lines (41 loc) · 1.3 KB
/
withMint.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
import { Cardano } from '@cardano-sdk/core';
import { FilterByPolicyIds } from './types';
import { ProjectionOperator } from '../../types';
import { computeCompactTxId } from './util';
import { map } from 'rxjs';
import { unifiedProjectorOperator } from '../utils';
export interface Mint {
assetId: Cardano.AssetId;
policyId: Cardano.PolicyId;
assetName: Cardano.AssetName;
quantity: bigint;
compactTxId: number;
txMetadata?: Cardano.TxMetadata;
}
export interface WithMint {
mint: Mint[];
}
export const withMint = unifiedProjectorOperator<{}, WithMint>((evt) => ({
...evt,
mint: evt.block.body.flatMap(({ body: { mint }, auxiliaryData }, txIndex) =>
[...(mint?.entries() || [])].map(
([assetId, quantity]): Mint => ({
assetId,
assetName: Cardano.AssetId.getAssetName(assetId),
compactTxId: computeCompactTxId(evt.block.header.blockNo, txIndex),
policyId: Cardano.AssetId.getPolicyId(assetId),
quantity,
txMetadata: auxiliaryData?.blob
})
)
)
}));
export const filterMintByPolicyIds =
<PropsIn extends WithMint>({ policyIds }: FilterByPolicyIds): ProjectionOperator<PropsIn> =>
(evt$) =>
evt$.pipe(
map((evt) => ({
...evt,
mint: evt.mint.filter(({ policyId }) => policyIds.includes(policyId))
}))
);