Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert fast-usdc contract to .ts #10480

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/base-zone/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export {};
* @typedef {object} Stores
* @property {() => Stores} detached obtain store providers which are detached (the stores are anonymous rather than bound to `label` in the zone)
* @property {(specimen: unknown) => boolean} isStorable return true if the specimen can be stored in the zone, whether as exo-object state or in a store
* @property {(label: string, options?: StoreOptions) => MapStore<any, any>} mapStore provide a Map-like store named `label` in the zone
* @property {<K, V>(label: string, options?: StoreOptions) => MapStore<K, V>} mapStore provide a Map-like store named `label` in the zone
* @property {<K>(label: string, options?: StoreOptions) => SetStore<K>} setStore provide a Set-like store named `label` in the zone
* @property {<K, V>(
* label: string, options?: StoreOptions) => WeakMapStore<K, V>
Expand Down
2 changes: 1 addition & 1 deletion packages/builders/scripts/fast-usdc/init-fast-usdc.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const defaultProposalBuilder = async (
options: toExternalConfig(config, crossVatContext, FastUSDCConfigShape),
installKeys: {
fastUsdc: publishRef(
install('@agoric/fast-usdc/src/fast-usdc.contract.js'),
install('@agoric/fast-usdc/src/fast-usdc.contract.ts'),
),
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
import { makeTracer } from '@agoric/internal';
import { Fail } from '@endo/errors';
import { M } from '@endo/patterns';
import type { Zone } from '@agoric/zone';
import { CctpTxEvidenceShape } from '../type-guards.js';
import type { CctpTxEvidence } from '../types.js';

const trace = makeTracer('TxOperator');
const trace: (message: string) => void = makeTracer('TxOperator');

/**
* @import {Zone} from '@agoric/zone';
* @import {CctpTxEvidence} from '../types.js';
*/
interface OperatorPowers {
submitEvidence: (evidence: CctpTxEvidence, operatorKit: OperatorKit) => void;
}

/**
* @typedef {object} OperatorPowers
* @property {(evidence: CctpTxEvidence, operatorKit: OperatorKit) => void} submitEvidence
*/
interface OperatorStatus {
disabled?: boolean;
operatorId: string;
}

/**
* @typedef {object} OperatorStatus
* @property {boolean} [disabled]
* @property {string} operatorId
*/

/**
* @typedef {Readonly<{ operatorId: string, powers: OperatorPowers }> & {disabled: boolean}} State
*/
interface State {
operatorId: string;
powers: OperatorPowers;
disabled: boolean;
}

const OperatorKitI = {
admin: M.interface('Admin', {
disable: M.call().returns(),
}),

/**
* NB: when this kit is an offer result, the smart-wallet will detect the `invitationMakers`
* key and save it for future offers.
*/
invitationMakers: M.interface('InvitationMakers', {
SubmitEvidence: M.call(CctpTxEvidenceShape).returns(M.promise()),
}),
Expand All @@ -40,20 +41,18 @@ const OperatorKitI = {
}),
};

/**
* @param {Zone} zone
* @param {{ makeInertInvitation: Function }} staticPowers
*/
export const prepareOperatorKit = (zone, staticPowers) =>
export const prepareOperatorKit = (
zone: Zone,
staticPowers: { makeInertInvitation: Function },
) =>
zone.exoClassKit(
'Operator Kit',
OperatorKitI,
/**
* @param {string} operatorId
* @param {OperatorPowers} powers facet of the durable transaction feed
* @returns {State}
* @param operatorId
* @param powers facet of the durable transaction feed
*/
(operatorId, powers) => {
(operatorId: string, powers: OperatorPowers): State => {
return {
operatorId,
powers,
Expand All @@ -67,10 +66,6 @@ export const prepareOperatorKit = (zone, staticPowers) =>
this.state.disabled = true;
},
},
/**
* NB: when this kit is an offer result, the smart-wallet will detect the `invitationMakers`
* key and save it for future offers.
*/
invitationMakers: {
/**
* Provide an API call in the form of an invitation maker, so that the
Expand All @@ -80,10 +75,9 @@ export const prepareOperatorKit = (zone, staticPowers) =>
* place, rather than as a means of performing it as in the
* fluxAggregator contract used for price oracles.
*
* @param {CctpTxEvidence} evidence
* @returns {Promise<Invitation>}
* @param evidence
*/
async SubmitEvidence(evidence) {
async SubmitEvidence(evidence: CctpTxEvidence): Promise<Invitation> {
const { operator } = this.facets;
// TODO(bootstrap integration): cause this call to throw and confirm that it
// shows up in the the smart-wallet UpdateRecord `error` property
Expand All @@ -97,16 +91,15 @@ export const prepareOperatorKit = (zone, staticPowers) =>
/**
* submit evidence from this operator
*
* @param {CctpTxEvidence} evidence
* @param evidence
*/
async submitEvidence(evidence) {
async submitEvidence(evidence: CctpTxEvidence): Promise<void> {
const { state } = this;
!state.disabled || Fail`submitEvidence for disabled operator`;
const result = state.powers.submitEvidence(evidence, this.facets);
return result;
},
/** @returns {OperatorStatus} */
getStatus() {
getStatus(): OperatorStatus {
const { state } = this;
return {
operatorId: state.operatorId,
Expand All @@ -117,4 +110,4 @@ export const prepareOperatorKit = (zone, staticPowers) =>
},
);

/** @typedef {ReturnType<ReturnType<typeof prepareOperatorKit>>} OperatorKit */
export type OperatorKit = ReturnType<ReturnType<typeof prepareOperatorKit>>;
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { makeTracer } from '@agoric/internal';
import { prepareDurablePublishKit } from '@agoric/notifier';
import type { Zone } from '@agoric/zone';
import { M } from '@endo/patterns';
import { CctpTxEvidenceShape } from '../type-guards.js';
import type { CctpTxEvidence } from '../types.js';
import { defineInertInvitation } from '../utils/zoe.js';
import { prepareOperatorKit } from './operator-kit.js';

/**
* @import {Zone} from '@agoric/zone';
* @import {OperatorKit} from './operator-kit.js';
* @import {CctpTxEvidence} from '../types.js';
*/
import type { OperatorKit } from './operator-kit.ts';
import { prepareOperatorKit } from './operator-kit.ts';

const trace = makeTracer('TxFeed', true);

Expand All @@ -21,7 +18,6 @@ const TransactionFeedKitI = harden({
submitEvidence: M.call(CctpTxEvidenceShape, M.any()).returns(),
}),
creator: M.interface('Transaction Feed Creator', {
// TODO narrow the return shape to OperatorKit
initOperator: M.call(M.string()).returns(M.record()),
makeOperatorInvitation: M.call(M.string()).returns(M.promise()),
removeOperator: M.call(M.string()).returns(),
Expand All @@ -31,18 +27,18 @@ const TransactionFeedKitI = harden({
}),
});

/**
* @param {Zone} zone
* @param {ZCF} zcf
*/
export const prepareTransactionFeedKit = (zone, zcf) => {
const kinds = zone.mapStore('Kinds');
interface State {
operators: MapStore<string, OperatorKit>;
pending: MapStore<string, MapStore<string, CctpTxEvidence>>;
}

export const prepareTransactionFeedKit = (zone: Zone, zcf: ZCF) => {
const kinds = zone.mapStore<string, unknown>('Kinds');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this unknown?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just didn't know what to type it and this worked

const makeDurablePublishKit = prepareDurablePublishKit(
kinds,
'Transaction Feed',
);
/** @type {PublishKit<CctpTxEvidence>} */
const { publisher, subscriber } = makeDurablePublishKit();
const { publisher, subscriber } = makeDurablePublishKit<CctpTxEvidence>();

const makeInertInvitation = defineInertInvitation(zcf, 'submitting evidence');

Expand All @@ -53,15 +49,16 @@ export const prepareTransactionFeedKit = (zone, zcf) => {
return zone.exoClassKit(
'Fast USDC Feed',
TransactionFeedKitI,
() => {
/** @type {MapStore<string, OperatorKit>} */
const operators = zone.mapStore('operators', {
durable: true,
});
/** @type {MapStore<string, MapStore<string, CctpTxEvidence>>} */
const pending = zone.mapStore('pending', {
(): State => {
const operators = zone.mapStore<string, OperatorKit>('operators', {
durable: true,
});
const pending = zone.mapStore<string, MapStore<string, CctpTxEvidence>>(
'pending',
{
durable: true,
},
);
return { operators, pending };
},
{
Expand All @@ -71,24 +68,21 @@ export const prepareTransactionFeedKit = (zone, zcf) => {
* oracle network, with the able to submit data to submit evidence of
* CCTP transactions.
*
* @param {string} operatorId unique per contract instance
* @returns {Promise<Invitation<OperatorKit>>}
* @param operatorId unique per contract instance
*/
makeOperatorInvitation(operatorId) {
makeOperatorInvitation(
operatorId: string,
): Promise<Invitation<OperatorKit>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why type the return? It seems the same value is inferred

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just moved from what was there. I'll leave since it doesn't hurt to be explicit

const { creator } = this.facets;
trace('makeOperatorInvitation', operatorId);

return zcf.makeInvitation(
/** @type {OfferHandler<OperatorKit>} */
seat => {
seat.exit();
return creator.initOperator(operatorId);
},
INVITATION_MAKERS_DESC,
);
return zcf.makeInvitation(seat => {
seat.exit();
return creator.initOperator(operatorId);
}, INVITATION_MAKERS_DESC);
},
/** @param {string} operatorId */
initOperator(operatorId) {

initOperator(operatorId: string) {
const { operators, pending } = this.state;
trace('initOperator', operatorId);

Expand All @@ -105,8 +99,7 @@ export const prepareTransactionFeedKit = (zone, zcf) => {
return operatorKit;
},

/** @param {string} operatorId */
async removeOperator(operatorId) {
async removeOperator(operatorId: string) {
const { operators } = this.state;
trace('removeOperator', operatorId);
const operatorKit = operators.get(operatorId);
Expand All @@ -117,11 +110,10 @@ export const prepareTransactionFeedKit = (zone, zcf) => {
operatorPowers: {
/**
* Add evidence from an operator.
*
* @param {CctpTxEvidence} evidence
* @param {OperatorKit} operatorKit
* @param evidence
* @param operatorKit
*/
submitEvidence(evidence, operatorKit) {
submitEvidence(evidence: CctpTxEvidence, operatorKit: OperatorKit) {
const { pending } = this.state;
trace(
'submitEvidence',
Expand Down Expand Up @@ -177,4 +169,6 @@ export const prepareTransactionFeedKit = (zone, zcf) => {
};
harden(prepareTransactionFeedKit);

/** @typedef {ReturnType<ReturnType<typeof prepareTransactionFeedKit>>} TransactionFeedKit */
export type TransactionFeedKit = ReturnType<
ReturnType<typeof prepareTransactionFeedKit>
>;
Loading
Loading