From 7fac17225523456d1290230b520cd72e125ed9cc Mon Sep 17 00:00:00 2001 From: 0xPatrick Date: Thu, 12 Dec 2024 17:08:58 -0500 Subject: [PATCH] refactor: `LP.borrowerFacet` expects `Amount`, not `AmountKWR` --- packages/fast-usdc/src/exos/advancer.js | 10 +++------ packages/fast-usdc/src/exos/liquidity-pool.js | 20 ++++++++--------- packages/fast-usdc/test/exos/advancer.test.ts | 22 +++++++++---------- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/packages/fast-usdc/src/exos/advancer.js b/packages/fast-usdc/src/exos/advancer.js index cc1facdb9eb..4fc29f6c253 100644 --- a/packages/fast-usdc/src/exos/advancer.js +++ b/packages/fast-usdc/src/exos/advancer.js @@ -161,9 +161,8 @@ export const prepareAdvancerKit = ( const advanceAmount = feeTools.calculateAdvance(fullAmount); const { zcfSeat: tmpSeat } = zcf.makeEmptySeatKit(); - const amountKWR = harden({ USDC: advanceAmount }); // throws if the pool has insufficient funds - borrowerFacet.borrow(tmpSeat, amountKWR); + borrowerFacet.borrow(tmpSeat, advanceAmount); // this cannot throw since `.isSeen()` is called in the same turn statusManager.advance(evidence); @@ -172,7 +171,7 @@ export const prepareAdvancerKit = ( tmpSeat, // @ts-expect-error LocalAccountMethods vs OrchestrationAccount poolAccount, - amountKWR, + harden({ USDC: advanceAmount }), ); void watch(depositV, this.facets.depositHandler, { fullAmount, @@ -230,10 +229,7 @@ export const prepareAdvancerKit = ( try { const { borrowerFacet, notifyFacet } = this.state; notifyFacet.notifyAdvancingResult(restCtx, false); - borrowerFacet.returnToPool( - tmpSeat, - harden({ USDC: advanceAmount }), - ); + borrowerFacet.returnToPool(tmpSeat, advanceAmount); } catch (e) { log('🚨 deposit to localOrchAccount failure recovery failed', e); } diff --git a/packages/fast-usdc/src/exos/liquidity-pool.js b/packages/fast-usdc/src/exos/liquidity-pool.js index 9cdd37fee94..66b548fbb3d 100644 --- a/packages/fast-usdc/src/exos/liquidity-pool.js +++ b/packages/fast-usdc/src/exos/liquidity-pool.js @@ -157,14 +157,14 @@ export const prepareLiquidityPoolKit = (zone, zcf, USDC, tools) => { borrower: { /** * @param {ZCFSeat} toSeat - * @param {{ USDC: Amount<'nat'>}} amountKWR + * @param {Amount<'nat'>} amount */ - borrow(toSeat, amountKWR) { + borrow(toSeat, amount) { const { encumberedBalance, poolSeat, poolStats } = this.state; // Validate amount is available in pool const post = borrowCalc( - amountKWR.USDC, + amount, poolSeat.getAmountAllocated('USDC', USDC), encumberedBalance, poolStats, @@ -172,7 +172,7 @@ export const prepareLiquidityPoolKit = (zone, zcf, USDC, tools) => { // COMMIT POINT // UNTIL #10684: ability to terminate an incarnation w/o terminating the contract - zcf.atomicRearrange(harden([[poolSeat, toSeat, amountKWR]])); + zcf.atomicRearrange(harden([[poolSeat, toSeat, { USDC: amount }]])); Object.assign(this.state, post); this.facets.external.publishPoolMetrics(); @@ -181,21 +181,21 @@ export const prepareLiquidityPoolKit = (zone, zcf, USDC, tools) => { * If something fails during advance, return funds to the pool. * * @param {ZCFSeat} borrowSeat - * @param {{ USDC: Amount<'nat'>}} amountKWR + * @param {Amount<'nat'>} amount */ - returnToPool(borrowSeat, amountKWR) { + returnToPool(borrowSeat, amount) { const { zcfSeat: repaySeat } = zcf.makeEmptySeatKit(); const returnAmounts = harden({ - Principal: amountKWR.USDC, + Principal: amount, PoolFee: makeEmpty(USDC), ContractFee: makeEmpty(USDC), }); const borrowSeatAllocation = borrowSeat.getCurrentAllocation(); - isGTE(borrowSeatAllocation.USDC, amountKWR.USDC) || - Fail`⚠️ borrowSeatAllocation ${q(borrowSeatAllocation)} less than amountKWR ${q(amountKWR)}`; + isGTE(borrowSeatAllocation.USDC, amount) || + Fail`⚠️ borrowSeatAllocation ${q(borrowSeatAllocation)} less than amountKWR ${q(amount)}`; // arrange payments in a format repay is expecting zcf.atomicRearrange( - harden([[borrowSeat, repaySeat, amountKWR, returnAmounts]]), + harden([[borrowSeat, repaySeat, { USDC: amount }, returnAmounts]]), ); return this.facets.repayer.repay(repaySeat, returnAmounts); }, diff --git a/packages/fast-usdc/test/exos/advancer.test.ts b/packages/fast-usdc/test/exos/advancer.test.ts index b6323e586a7..d724e4a1cf8 100644 --- a/packages/fast-usdc/test/exos/advancer.test.ts +++ b/packages/fast-usdc/test/exos/advancer.test.ts @@ -106,11 +106,11 @@ const createTestExtensions = (t, common: CommonSetup) => { } = { borrow: [], returnToPool: [] }; const mockBorrowerF = Far('LiquidityPool Borrow Facet', { - borrow: (seat: ZCFSeat, amounts: { USDC: NatAmount }) => { - mockBorrowerFacetCalls.borrow.push([seat, amounts]); + borrow: (seat: ZCFSeat, amount: NatAmount) => { + mockBorrowerFacetCalls.borrow.push([seat, amount]); }, - returnToPool: (seat: ZCFSeat, amounts: { USDC: NatAmount }) => { - mockBorrowerFacetCalls.returnToPool.push([seat, amounts]); + returnToPool: (seat: ZCFSeat, amount: NatAmount) => { + mockBorrowerFacetCalls.returnToPool.push([seat, amount]); }, }); @@ -236,9 +236,9 @@ test('updates status to OBSERVED on insufficient pool funds', async t => { } = t.context; const mockBorrowerFacet = Far('LiquidityPool Borrow Facet', { - borrow: (seat: ZCFSeat, amounts: { USDC: NatAmount }) => { + borrow: (seat: ZCFSeat, amount: NatAmount) => { throw new Error( - `Cannot borrow. Requested ${q(amounts.USDC)} must be less than pool balance ${q(usdc.make(1n))}.`, + `Cannot borrow. Requested ${q(amount)} must be less than pool balance ${q(usdc.make(1n))}.`, ); }, returnToPool: () => {}, // not expecting this to be called @@ -460,7 +460,7 @@ test('returns payment to LP if zoeTools.localTransfer fails', async t => { const expectedArguments = [ Far('MockZCFSeat', {}), - { USDC: usdc.make(146999999n) }, // net of fees + usdc.make(146999999n), // net of fees ]; t.is(borrow.length, 1, 'borrow is called before zt.localTransfer fails'); @@ -503,12 +503,12 @@ test('alerts if `returnToPool` fallback fails', async t => { } = t.context; const mockBorrowerFacet = Far('LiquidityPool Borrow Facet', { - borrow: (seat: ZCFSeat, amounts: { USDC: NatAmount }) => { + borrow: (seat: ZCFSeat, amount: NatAmount) => { // note: will not be tracked by `inspectBorrowerFacetCalls` }, - returnToPool: (seat: ZCFSeat, amounts: { USDC: NatAmount }) => { + returnToPool: (seat: ZCFSeat, amount: NatAmount) => { throw new Error( - `⚠️ borrowSeatAllocation ${q({ USDC: usdc.make(0n) })} less than amountKWR ${q(amounts)}`, + `⚠️ borrowSeatAllocation ${q({ USDC: usdc.make(0n) })} less than amountKWR ${q(amount)}`, ); }, }); @@ -536,7 +536,7 @@ test('alerts if `returnToPool` fallback fails', async t => { [ '🚨 deposit to localOrchAccount failure recovery failed', Error( - `⚠️ borrowSeatAllocation ${q({ USDC: usdc.make(0n) })} less than amountKWR ${q({ USDC: usdc.make(146999999n) })}`, + `⚠️ borrowSeatAllocation ${q({ USDC: usdc.make(0n) })} less than amountKWR ${q(usdc.make(146999999n))}`, ), ], ]);