Skip to content

Commit

Permalink
feat(core): added a new inspector for extracting transaction metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelCastilloB committed Sep 21, 2022
1 parent a9289f3 commit d7e1402
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/core/src/util/txInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CertificateType,
Ed25519KeyHash,
Lovelace,
Metadatum,
PolicyId,
RewardAccount,
Script,
Expand Down Expand Up @@ -33,6 +34,7 @@ type TxInspector<T extends Inspectors> = (tx: TxAlonzo) => {
export type SendReceiveValueInspection = Value;
export type DelegationInspection = StakeDelegationCertificate[];
export type StakeKeyRegistrationInspection = StakeAddressCertificate[];

export type WithdrawalInspection = Lovelace;
export interface SentInspection {
inputs: TxIn[];
Expand All @@ -50,6 +52,8 @@ export interface MintedAsset {

export type AssetsMintedInspection = MintedAsset[];

export type MetadataInspection = Metadatum;

// Inspector types
interface SentInspectorArgs {
addresses?: Address[];
Expand All @@ -69,6 +73,7 @@ export type SignedCertificatesInspector = (
certificateTypes?: CertificateType[]
) => Inspector<SignedCertificatesInspection>;
export type AssetsMintedInspector = Inspector<AssetsMintedInspection>;
export type MetadataInspector = Inspector<MetadataInspection>;

/**
* Inspects a transaction for values (coins + assets) in inputs
Expand Down Expand Up @@ -291,6 +296,13 @@ export const assetsMintedInspector: AssetsMintedInspector = mintInspector((quant
*/
export const assetsBurnedInspector: AssetsMintedInspector = mintInspector((quantity: bigint) => quantity < 0);

/**
* Inspects a transaction for its metadata.
*
* @param {TxAlonzo} tx transaction to inspect.
*/
export const metadataInspector: MetadataInspector = (tx) => tx.auxiliaryData?.body?.blob ?? new Map();

/**
* Returns a function to convert lower level transaction data to a higher level object, using the provided inspectors.
*
Expand Down
11 changes: 11 additions & 0 deletions packages/core/test/util/txInspector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
assetsMintedInspector,
createTxInspector,
delegationInspector,
metadataInspector,
sentInspector,
signedCertificatesInspector,
stakeKeyDeregistrationInspector,
Expand Down Expand Up @@ -526,4 +527,14 @@ describe('txInspector', () => {
expect(burned[0].script).toBeUndefined();
});
});

describe('metadata inspector', () => {
it('inspects a transaction with metadata and produces an inspection with the metadatum', () => {
const tx = buildMockTx();
const inspectTx = createTxInspector({ metadata: metadataInspector });
const { metadata } = inspectTx(tx);

expect(metadata).toEqual(txMetadatum);
});
});
});

0 comments on commit d7e1402

Please sign in to comment.