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

'make' namespace for kinds #10666

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
85 changes: 42 additions & 43 deletions packages/fast-usdc/src/fast-usdc.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { prepareTransactionFeedKit } from './exos/transaction-feed.js';
import * as flows from './fast-usdc.flows.js';
import { FastUSDCTermsShape, FeeConfigShape } from './type-guards.js';
import { defineInertInvitation } from './utils/zoe.js';
import { organizeMakers } from './utils/make-helpers.js';

const trace = makeTracer('FastUsdc');

Expand Down Expand Up @@ -101,46 +102,42 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
const { USDC } = terms.brands;
const { withdrawToSeat } = tools.zoeTools;
const { baggage, chainHub, orchestrateAll, vowTools } = tools;
const makeSettler = prepareSettler(zone, {
statusManager,
USDC,
withdrawToSeat,
feeConfig,
vowTools: tools.vowTools,
zcf,
chainHub,
});

const { localTransfer } = makeZoeTools(zcf, vowTools);
const makeAdvancer = prepareAdvancer(zone, {
chainHub,
feeConfig,
localTransfer,
usdc: harden({
brand: terms.brands.USDC,
denom: terms.usdcDenom,

const { make } = organizeMakers({
...orchestrateAll(flows, {}),
makeSettler: prepareSettler(zone, {
Copy link
Member

Choose a reason for hiding this comment

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

I thought about a record like this, but sometimes you have to pass makeA into prepareB.

Copy link
Member Author

Choose a reason for hiding this comment

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

oh good point. That thought crossed my mind while I was refactoring but it consolidating like this was too delicious

statusManager,
USDC,
withdrawToSeat,
feeConfig,
vowTools: tools.vowTools,
zcf,
chainHub,
}),
statusManager,
vowTools,
zcf,
makeAdvancer: prepareAdvancer(zone, {
chainHub,
feeConfig,
localTransfer,
usdc: harden({
brand: terms.brands.USDC,
denom: terms.usdcDenom,
}),
statusManager,
vowTools,
zcf,
}),
makeFeedKit: prepareTransactionFeedKit(zone, zcf),
makeLiquidityPoolKit: prepareLiquidityPoolKit(
zone,
zcf,
terms.brands.USDC,
{ makeRecorderKit },
),
makeTestInvitation: defineInertInvitation(zcf, 'test of forcing evidence'),
});

const makeFeedKit = prepareTransactionFeedKit(zone, zcf);

const makeLiquidityPoolKit = prepareLiquidityPoolKit(
zone,
zcf,
terms.brands.USDC,
{ makeRecorderKit },
);

const makeTestInvitation = defineInertInvitation(
zcf,
'test of forcing evidence',
);

const { makeLocalAccount, makeNobleAccount } = orchestrateAll(flows, {});

const creatorFacet = zone.exo('Fast USDC Creator', undefined, {
/** @type {(operatorId: string) => Promise<Invitation<OperatorKit>>} */
async makeOperatorInvitation(operatorId) {
Expand Down Expand Up @@ -175,7 +172,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
*/
makeTestPushInvitation(evidence) {
void advancer.handleTransactionEvent(evidence);
return makeTestInvitation();
return make.TestInvitation();
},
makeDepositInvitation() {
return poolKit.public.makeDepositInvitation();
Expand Down Expand Up @@ -213,7 +210,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
);

const poolKit = zone.makeOnce('Liquidity Pool kit', () =>
makeLiquidityPoolKit(shareMint, privateArgs.storageNode),
make.LiquidityPoolKit(shareMint, privateArgs.storageNode),
);

/** Chain, connection, and asset info can only be registered once */
Expand All @@ -228,13 +225,15 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
);
}

const nobleAccountV = zone.makeOnce('NobleAccount', () => makeNobleAccount());
const nobleAccountV = zone.makeOnce('NobleAccount', () =>
make.NobleAccount(),
);

const feedKit = zone.makeOnce('Feed Kit', () => makeFeedKit());
const feedKit = zone.makeOnce('Feed Kit', () => make.FeedKit());

const poolAccountV = zone.makeOnce('PoolAccount', () => makeLocalAccount());
const poolAccountV = zone.makeOnce('PoolAccount', () => make.LocalAccount());
const settleAccountV = zone.makeOnce('SettleAccount', () =>
makeLocalAccount(),
make.LocalAccount(),
);
// when() is OK here since this clearly resolves promptly.
/** @type {[HostInterface<OrchestrationAccount<{chainId: 'agoric-3';}>>, HostInterface<OrchestrationAccount<{chainId: 'agoric-3';}>>]} */
Expand All @@ -247,15 +246,15 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
const [_agoric, _noble, agToNoble] = await vowTools.when(
chainHub.getChainsAndConnection('agoric', 'noble'),
);
const settlerKit = makeSettler({
const settlerKit = make.Settler({
repayer: poolKit.repayer,
sourceChannel: agToNoble.transferChannel.counterPartyChannelId,
remoteDenom: 'uusdc',
settlementAccount,
});

const advancer = zone.makeOnce('Advancer', () =>
makeAdvancer({
make.Advancer({
borrowerFacet: poolKit.borrower,
notifyFacet: settlerKit.notify,
poolAccount,
Expand Down
20 changes: 20 additions & 0 deletions packages/fast-usdc/src/utils/make-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Takes an object of make* functions and returns an object with a single 'make' property
* containing those functions renamed without the 'make' prefix.
*
* @template {Record<`make${string}`, Function>} T
* @param {T} makers - Object containing make* functions
* @returns {{ make: { [K in keyof T as K extends `make${infer R}` ? R : never]: T[K] } }} Transformed object
*/
export const organizeMakers = makers => {
const make = {};
for (const [key, fn] of Object.entries(makers)) {
if (!key.startsWith('make')) {
throw new Error(`Unexpected key ${key}`);
}
const newKey = key.slice(4); // Remove 'make'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const newKey = key.slice(4); // Remove 'make'
const newKey = key.slice('make'.length);

make[newKey] = fn;
}
// @ts-expect-error cast
return harden({ make });
Copy link
Member

Choose a reason for hiding this comment

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

why a record? why not return make;? in case there are other facets later or something? that doesn't seem like your style.

Copy link
Member Author

Choose a reason for hiding this comment

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

To enforce that the name used is make. Though that could be done by a lint rule, like React's useState does to enforce that the tuple it returns of [state object, state setter] has the the consistent naming [myState, setMyState].

};
Loading