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 fe0b633 commit 5da5aad
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions packages/fast-usdc/test/pool-share-math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
} from '@agoric/zoe/src/contractSupport/ratio.js';
import { mustMatch } from '@endo/patterns';
import {
borrowCalc,
depositCalc,
makeParity,
repayCalc,
withdrawCalc,
withFees,
} from '../src/pool-share-math.js';
Expand Down Expand Up @@ -295,3 +297,101 @@ testProp(
);
},
);

const makeInitialPoolStats = () => ({
totalBorrows: makeEmpty(brands.USDC),
totalRepays: makeEmpty(brands.USDC),
totalPoolFees: makeEmpty(brands.USDC),
totalContractFees: makeEmpty(brands.USDC),
});

test('basic borrow calculation', t => {
const { USDC } = brands;
const requested = make(USDC, 100n);
const available = make(USDC, 200n);
const outstandingLends = make(USDC, 50n);
const poolStats = makeInitialPoolStats();

const result = borrowCalc(requested, available, outstandingLends, poolStats);

t.deepEqual(
result.outstandingLends,
make(USDC, 150n),
'Outstanding lends should increase by borrowed amount',
);
t.deepEqual(
result.poolStats.totalBorrows,
make(USDC, 100n),
'Total borrows should increase by borrowed amount',
);
t.deepEqual(
Object.keys(result.poolStats),
Object.keys(poolStats),
'borrowCalc returns all poolStats fields',
);
});

test('borrow fails when requested amount exceeds available', t => {
const { USDC } = brands;
const requested = make(USDC, 200n);
const available = make(USDC, 100n);
const outstandingLends = make(USDC, 50n);
const poolStats = makeInitialPoolStats();

t.throws(
() => borrowCalc(requested, available, outstandingLends, poolStats),
{
message: /Cannot borrow/,
},
);
});

test('basic repay calculation', t => {
const { USDC } = brands;
const shareWorth = makeParity(make(USDC, 1n), brands.PoolShares);
const amounts = {
Principal: make(USDC, 100n),
PoolFee: make(USDC, 10n),
ContractFee: make(USDC, 5n),
};
const outstandingLends = make(USDC, 200n);
const poolStats = makeInitialPoolStats();

const result = repayCalc(shareWorth, amounts, outstandingLends, poolStats);

t.deepEqual(
result.outstandingLends,
make(USDC, 100n),
'Outstanding lends should decrease by principal',
);
t.deepEqual(
result.poolStats.totalRepays,
amounts.Principal,
'Total repays should increase by principal',
);
t.deepEqual(
result.poolStats.totalPoolFees,
amounts.PoolFee,
'Total pool fees should increase by pool fee',
);
t.deepEqual(
result.poolStats.totalContractFees,
amounts.ContractFee,
'Total contract fees should increase by contract fee',
);
t.deepEqual(
result.poolStats.totalBorrows,
poolStats.totalBorrows,
'Total borrows should remain unchanged',
);
t.deepEqual(
result.shareWorth.numerator,
make(USDC, 11n),
'Share worth numerator should increase by pool fee',
);
t.deepEqual(
Object.keys(result.poolStats),
Object.keys(poolStats),
'repayCalc returns all poolStats fields',
);
});

0 comments on commit 5da5aad

Please sign in to comment.