Skip to content

Commit

Permalink
refactor: LP.borrowerFacet expects Amount, not AmountKWR
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Dec 12, 2024
1 parent 81517e2 commit 50c1bd1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
10 changes: 3 additions & 7 deletions packages/fast-usdc/src/exos/advancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
30 changes: 12 additions & 18 deletions packages/fast-usdc/src/exos/liquidity-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,8 @@ export const prepareLiquidityPoolKit = (zone, zcf, USDC, tools) => {
'Liquidity Pool',
{
borrower: M.interface('borrower', {
borrow: M.call(
SeatShape,
harden({ USDC: makeNatAmountShape(USDC, 1n) }),
).returns(),
returnToPool: M.call(
SeatShape,
harden({ USDC: makeNatAmountShape(USDC, 1n) }),
).returns(),
borrow: M.call(SeatShape, makeNatAmountShape(USDC, 1n)).returns(),
returnToPool: M.call(SeatShape, makeNatAmountShape(USDC, 1n)).returns(),
}),
repayer: M.interface('repayer', {
repay: M.call(
Expand Down Expand Up @@ -157,22 +151,22 @@ 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,
);

// 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();
Expand All @@ -181,21 +175,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);
},
Expand Down
22 changes: 11 additions & 11 deletions packages/fast-usdc/test/exos/advancer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
},
});

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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)}`,
);
},
});
Expand Down Expand Up @@ -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))}`,
),
],
]);
Expand Down

0 comments on commit 50c1bd1

Please sign in to comment.