From 5a7b3d25cb7853e9109f74a7b45feb29b8ff69fe Mon Sep 17 00:00:00 2001 From: 0xPatrick Date: Tue, 5 Nov 2024 15:35:38 -0500 Subject: [PATCH] feat: `CctpTxEvidenceShape`, `PendingTxShape` typeGuards --- packages/fast-usdc/src/typeGuards.js | 39 ++++++++++++++++++++++ packages/fast-usdc/test/typeGuards.test.ts | 39 ++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 packages/fast-usdc/src/typeGuards.js create mode 100644 packages/fast-usdc/test/typeGuards.test.ts diff --git a/packages/fast-usdc/src/typeGuards.js b/packages/fast-usdc/src/typeGuards.js new file mode 100644 index 00000000000..2f10318fc57 --- /dev/null +++ b/packages/fast-usdc/src/typeGuards.js @@ -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} */ +export const EvmHashShape = M.string({ + stringLengthLimit: 66, +}); +harden(EvmHashShape); + +/** @type {TypedPattern} */ +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} */ +// @ts-expect-error TypedPattern can't handle spreading? +export const PendingTxShape = { + ...CctpTxEvidenceShape, + status: M.or(...Object.values(PendingTxStatus)), +}; +harden(PendingTxShape); diff --git a/packages/fast-usdc/test/typeGuards.test.ts b/packages/fast-usdc/test/typeGuards.test.ts new file mode 100644 index 00000000000..9920fd7ce34 --- /dev/null +++ b/packages/fast-usdc/test/typeGuards.test.ts @@ -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, + ), + ); +});