From 05f23dd96cf9da8d483bedee4b4a8f11ec2ed6a1 Mon Sep 17 00:00:00 2001 From: "Turadg Aleahmad (aider)" Date: Tue, 10 Dec 2024 09:44:11 -0800 Subject: [PATCH 1/5] feat: Add organizeMakers utility to transform make* functions --- packages/fast-usdc/src/fast-usdc.contract.js | 11 +++++++---- packages/fast-usdc/src/utils/make-helpers.js | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 packages/fast-usdc/src/utils/make-helpers.js diff --git a/packages/fast-usdc/src/fast-usdc.contract.js b/packages/fast-usdc/src/fast-usdc.contract.js index 91dbdebdc2c..eac76af6937 100644 --- a/packages/fast-usdc/src/fast-usdc.contract.js +++ b/packages/fast-usdc/src/fast-usdc.contract.js @@ -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'); @@ -139,7 +140,9 @@ export const contract = async (zcf, privateArgs, zone, tools) => { 'test of forcing evidence', ); - const { makeLocalAccount, makeNobleAccount } = orchestrateAll(flows, {}); + const { make: { LocalAccount, NobleAccount } } = organizeMakers( + orchestrateAll(flows, {}) + ); const creatorFacet = zone.exo('Fast USDC Creator', undefined, { /** @type {(operatorId: string) => Promise>} */ @@ -228,13 +231,13 @@ export const contract = async (zcf, privateArgs, zone, tools) => { ); } - const nobleAccountV = zone.makeOnce('NobleAccount', () => makeNobleAccount()); + const nobleAccountV = zone.makeOnce('NobleAccount', () => NobleAccount()); const feedKit = zone.makeOnce('Feed Kit', () => makeFeedKit()); - const poolAccountV = zone.makeOnce('PoolAccount', () => makeLocalAccount()); + const poolAccountV = zone.makeOnce('PoolAccount', () => LocalAccount()); const settleAccountV = zone.makeOnce('SettleAccount', () => - makeLocalAccount(), + LocalAccount(), ); // when() is OK here since this clearly resolves promptly. /** @type {[HostInterface>, HostInterface>]} */ diff --git a/packages/fast-usdc/src/utils/make-helpers.js b/packages/fast-usdc/src/utils/make-helpers.js new file mode 100644 index 00000000000..472bee8aa68 --- /dev/null +++ b/packages/fast-usdc/src/utils/make-helpers.js @@ -0,0 +1,17 @@ +/** + * Takes an object of make* functions and returns an object with a single 'make' property + * containing those functions renamed without the 'make' prefix. + * + * @param {Record} makers - Object containing make* functions + * @returns {{ make: Record }} Transformed object + */ +export const organizeMakers = makers => { + const make = {}; + for (const [key, fn] of Object.entries(makers)) { + if (key.startsWith('make')) { + const newKey = key.slice(4); // Remove 'make' + make[newKey] = fn; + } + } + return harden({ make }); +}; From ed3579d5193e5eb613735f7b2dcae1131c082578 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Tue, 10 Dec 2024 09:45:14 -0800 Subject: [PATCH 2/5] fixup! feat: Add organizeMakers utility to transform make* functions --- packages/fast-usdc/src/fast-usdc.contract.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/fast-usdc/src/fast-usdc.contract.js b/packages/fast-usdc/src/fast-usdc.contract.js index eac76af6937..05d63656c42 100644 --- a/packages/fast-usdc/src/fast-usdc.contract.js +++ b/packages/fast-usdc/src/fast-usdc.contract.js @@ -140,9 +140,9 @@ export const contract = async (zcf, privateArgs, zone, tools) => { 'test of forcing evidence', ); - const { make: { LocalAccount, NobleAccount } } = organizeMakers( - orchestrateAll(flows, {}) - ); + const { + make: { LocalAccount, NobleAccount }, + } = organizeMakers(orchestrateAll(flows, {})); const creatorFacet = zone.exo('Fast USDC Creator', undefined, { /** @type {(operatorId: string) => Promise>} */ @@ -236,9 +236,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => { const feedKit = zone.makeOnce('Feed Kit', () => makeFeedKit()); const poolAccountV = zone.makeOnce('PoolAccount', () => LocalAccount()); - const settleAccountV = zone.makeOnce('SettleAccount', () => - LocalAccount(), - ); + const settleAccountV = zone.makeOnce('SettleAccount', () => LocalAccount()); // when() is OK here since this clearly resolves promptly. /** @type {[HostInterface>, HostInterface>]} */ const [poolAccount, settlementAccount] = await vowTools.when( From 16a2139df4029a3c44fd39c36b7405bca37dee3a Mon Sep 17 00:00:00 2001 From: "Turadg Aleahmad (aider)" Date: Tue, 10 Dec 2024 09:46:08 -0800 Subject: [PATCH 3/5] refactor: Add generic type to `organizeMakers` for improved type inference --- packages/fast-usdc/src/utils/make-helpers.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/fast-usdc/src/utils/make-helpers.js b/packages/fast-usdc/src/utils/make-helpers.js index 472bee8aa68..e4c0dea919a 100644 --- a/packages/fast-usdc/src/utils/make-helpers.js +++ b/packages/fast-usdc/src/utils/make-helpers.js @@ -2,8 +2,9 @@ * Takes an object of make* functions and returns an object with a single 'make' property * containing those functions renamed without the 'make' prefix. * - * @param {Record} makers - Object containing make* functions - * @returns {{ make: Record }} Transformed object + * @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 = {}; From d77bb995609326e50cc7a67e11777eba925b4a24 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Tue, 10 Dec 2024 09:47:43 -0800 Subject: [PATCH 4/5] feat: throw on non-make keys --- packages/fast-usdc/src/utils/make-helpers.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/fast-usdc/src/utils/make-helpers.js b/packages/fast-usdc/src/utils/make-helpers.js index e4c0dea919a..7a5c63c5b5d 100644 --- a/packages/fast-usdc/src/utils/make-helpers.js +++ b/packages/fast-usdc/src/utils/make-helpers.js @@ -9,10 +9,12 @@ export const organizeMakers = makers => { const make = {}; for (const [key, fn] of Object.entries(makers)) { - if (key.startsWith('make')) { - const newKey = key.slice(4); // Remove 'make' - make[newKey] = fn; + if (!key.startsWith('make')) { + throw new Error(`Unexpected key ${key}`); } + const newKey = key.slice(4); // Remove 'make' + make[newKey] = fn; } + // @ts-expect-error cast return harden({ make }); }; From c1e4cea2d61029f476f924b77ad969b661843786 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Tue, 10 Dec 2024 09:51:20 -0800 Subject: [PATCH 5/5] refactor: use `make` for FUC --- packages/fast-usdc/src/fast-usdc.contract.js | 88 ++++++++++---------- 1 file changed, 43 insertions(+), 45 deletions(-) diff --git a/packages/fast-usdc/src/fast-usdc.contract.js b/packages/fast-usdc/src/fast-usdc.contract.js index 05d63656c42..87ba9e6d0b5 100644 --- a/packages/fast-usdc/src/fast-usdc.contract.js +++ b/packages/fast-usdc/src/fast-usdc.contract.js @@ -102,48 +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, { + 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 { - make: { LocalAccount, NobleAccount }, - } = organizeMakers(orchestrateAll(flows, {})); - const creatorFacet = zone.exo('Fast USDC Creator', undefined, { /** @type {(operatorId: string) => Promise>} */ async makeOperatorInvitation(operatorId) { @@ -178,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(); @@ -216,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 */ @@ -231,12 +225,16 @@ export const contract = async (zcf, privateArgs, zone, tools) => { ); } - const nobleAccountV = zone.makeOnce('NobleAccount', () => NobleAccount()); + 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', () => LocalAccount()); - const settleAccountV = zone.makeOnce('SettleAccount', () => LocalAccount()); + const poolAccountV = zone.makeOnce('PoolAccount', () => make.LocalAccount()); + const settleAccountV = zone.makeOnce('SettleAccount', () => + make.LocalAccount(), + ); // when() is OK here since this clearly resolves promptly. /** @type {[HostInterface>, HostInterface>]} */ const [poolAccount, settlementAccount] = await vowTools.when( @@ -248,7 +246,7 @@ 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', @@ -256,7 +254,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => { }); const advancer = zone.makeOnce('Advancer', () => - makeAdvancer({ + make.Advancer({ borrowerFacet: poolKit.borrower, notifyFacet: settlerKit.notify, poolAccount,