Skip to content

Commit

Permalink
fixup! feat: liquidity pool borrower and repayer facets
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Nov 18, 2024
1 parent 1dda57a commit 3af5251
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/fast-usdc/src/exos/liquidity-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export const prepareLiquidityPoolKit = (zone, zcf, USDC, tools) => {
shareWorth,
} = this.state;
checkPoolBalance(poolSeat, shareWorth, USDC, encumberedBalance);

// determine new shareWorth, encumberedBalance, and poolMetrics
const post = repayCalc(
shareWorth,
Expand Down
6 changes: 5 additions & 1 deletion packages/fast-usdc/src/pool-share-math.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@agoric/zoe/src/contractSupport/ratio.js';
import { Fail, q } from '@endo/errors';

const { getValue, add, isEmpty, isGTE, subtract } = AmountMath;
const { getValue, add, isEmpty, isEqual, isGTE, subtract } = AmountMath;

/**
* @import {PoolStats} from './types';
Expand Down Expand Up @@ -164,6 +164,10 @@ export const repayCalc = (
encumberedBalance,
poolStats,
) => {
!isGTE(amounts.Principal, encumberedBalance) ||
isEqual(amounts.Principal, encumberedBalance) ||
Fail`Cannot repay ${q(amounts.Principal)}; Outstanding lends ${q(encumberedBalance)}.`;

return harden({
shareWorth: withFees(shareWorth, amounts.PoolFee),
encumberedBalance: subtract(encumberedBalance, amounts.Principal),
Expand Down
55 changes: 50 additions & 5 deletions packages/fast-usdc/test/pool-share-math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,16 @@ const makeInitialPoolStats = () => ({
test('basic borrow calculation', t => {
const { USDC } = brands;
const requested = make(USDC, 100n);
const available = make(USDC, 200n);
const poolSeatAllocation = make(USDC, 200n);
const encumberedBalance = make(USDC, 50n);
const poolStats = makeInitialPoolStats();

const result = borrowCalc(requested, available, encumberedBalance, poolStats);
const result = borrowCalc(
requested,
poolSeatAllocation,
encumberedBalance,
poolStats,
);

t.deepEqual(
result.encumberedBalance,
Expand All @@ -331,18 +336,29 @@ test('basic borrow calculation', t => {
);
});

test('borrow fails when requested amount exceeds available', t => {
test('borrow fails when requested exceeds or equals pool seat allocation', t => {
const { USDC } = brands;
const requested = make(USDC, 200n);
const available = make(USDC, 100n);
const poolSeatAllocation = make(USDC, 100n);
const encumberedBalance = make(USDC, 50n);
const poolStats = makeInitialPoolStats();

t.throws(
() => borrowCalc(requested, available, encumberedBalance, poolStats),
() =>
borrowCalc(requested, poolSeatAllocation, encumberedBalance, poolStats),
{
message: /Cannot borrow/,
},
);
t.throws(
() => borrowCalc(requested, make(USDC, 200n), encumberedBalance, poolStats),
{
message: /Cannot borrow/,
},
'request must exceed pool seat allocation',
);
t.notThrows(() =>
borrowCalc(requested, make(USDC, 201n), encumberedBalance, poolStats),
);
});

Expand Down Expand Up @@ -395,3 +411,32 @@ test('basic repay calculation', t => {
'repayCalc returns all poolStats fields',
);
});

test('repay fails when principal exceeds encumbered balance', t => {
const { USDC } = brands;

const shareWorth = makeParity(make(USDC, 1n), brands.PoolShares);
const amounts = {
Principal: make(USDC, 200n),
PoolFee: make(USDC, 10n),
ContractFee: make(USDC, 5n),
};
const encumberedBalance = make(USDC, 100n);
const poolStats = {
...makeInitialPoolStats(),
totalBorrows: make(USDC, 100n),
};

t.throws(() => repayCalc(shareWorth, amounts, encumberedBalance, poolStats), {
message: /Cannot repay/,
});

t.notThrows(
() =>
repayCalc(shareWorth, amounts, make(USDC, 200n), {
...makeInitialPoolStats(),
totalBorrows: make(USDC, 200n),
}),
'repay succeeds when principal equals encumbered balance',
);
});

0 comments on commit 3af5251

Please sign in to comment.