Skip to content

Commit

Permalink
feat: CctpTxEvidenceShape, PendingTxShape typeGuards
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Nov 12, 2024
1 parent 5e4139c commit 5a7b3d2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/fast-usdc/src/typeGuards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { M } from '@endo/patterns';
import { PendingTxStatus } from './constants.js';

/**
* @import {TypedPattern} from '@agoric/internal';
* @import {CctpTxEvidence, PendingTx} from './types.js';
*/

/** @type {TypedPattern<string>} */
export const EvmHashShape = M.string({
stringLengthLimit: 66,
});
harden(EvmHashShape);

/** @type {TypedPattern<CctpTxEvidence>} */
export const CctpTxEvidenceShape = {
aux: {
forwardingChannel: M.string(),
recipientAddress: M.string(),
},
blockHash: EvmHashShape,
blockNumber: M.bigint(),
blockTimestamp: M.bigint(),
chainId: M.number(),
tx: {
amount: M.bigint(),
forwardingAddress: M.string(),
},
txHash: EvmHashShape,
};
harden(CctpTxEvidenceShape);

/** @type {TypedPattern<PendingTx>} */
// @ts-expect-error TypedPattern can't handle spreading?
export const PendingTxShape = {
...CctpTxEvidenceShape,
status: M.or(...Object.values(PendingTxStatus)),
};
harden(PendingTxShape);
39 changes: 39 additions & 0 deletions packages/fast-usdc/test/typeGuards.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import { mustMatch } from '@endo/patterns';
import { TxStatus, PendingTxStatus } from '../src/constants.js';
import { CctpTxEvidenceShape, PendingTxShape } from '../src/typeGuards.js';
import type { CctpTxEvidence } from '../src/types.js';

import { MockCctpTxEvidences } from './fixtures.js';

test('CctpTxEvidenceShape', t => {
const specimen: CctpTxEvidence = harden(
MockCctpTxEvidences.AGORIC_PLUS_OSMO(),
);

t.notThrows(() => mustMatch(specimen, CctpTxEvidenceShape));
});

test('PendingTxShape', t => {
const specimen: CctpTxEvidence & { status: TxStatus } = harden({
...MockCctpTxEvidences.AGORIC_PLUS_OSMO(),
status: PendingTxStatus.Observed,
});

t.notThrows(() => mustMatch(specimen, PendingTxShape));

t.notThrows(() =>
mustMatch(
harden({ ...specimen, status: PendingTxStatus.Advanced }),
PendingTxShape,
),
);

t.throws(() =>
mustMatch(
harden({ ...specimen, status: TxStatus.Settled }),
PendingTxShape,
),
);
});

0 comments on commit 5a7b3d2

Please sign in to comment.