Skip to content

Commit 5da5aad

Browse files
committed
fixup! feat: liquidity pool borrower and repayer facets
1 parent fe0b633 commit 5da5aad

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

packages/fast-usdc/test/pool-share-math.test.ts

+100
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import {
77
} from '@agoric/zoe/src/contractSupport/ratio.js';
88
import { mustMatch } from '@endo/patterns';
99
import {
10+
borrowCalc,
1011
depositCalc,
1112
makeParity,
13+
repayCalc,
1214
withdrawCalc,
1315
withFees,
1416
} from '../src/pool-share-math.js';
@@ -295,3 +297,101 @@ testProp(
295297
);
296298
},
297299
);
300+
301+
const makeInitialPoolStats = () => ({
302+
totalBorrows: makeEmpty(brands.USDC),
303+
totalRepays: makeEmpty(brands.USDC),
304+
totalPoolFees: makeEmpty(brands.USDC),
305+
totalContractFees: makeEmpty(brands.USDC),
306+
});
307+
308+
test('basic borrow calculation', t => {
309+
const { USDC } = brands;
310+
const requested = make(USDC, 100n);
311+
const available = make(USDC, 200n);
312+
const outstandingLends = make(USDC, 50n);
313+
const poolStats = makeInitialPoolStats();
314+
315+
const result = borrowCalc(requested, available, outstandingLends, poolStats);
316+
317+
t.deepEqual(
318+
result.outstandingLends,
319+
make(USDC, 150n),
320+
'Outstanding lends should increase by borrowed amount',
321+
);
322+
t.deepEqual(
323+
result.poolStats.totalBorrows,
324+
make(USDC, 100n),
325+
'Total borrows should increase by borrowed amount',
326+
);
327+
t.deepEqual(
328+
Object.keys(result.poolStats),
329+
Object.keys(poolStats),
330+
'borrowCalc returns all poolStats fields',
331+
);
332+
});
333+
334+
test('borrow fails when requested amount exceeds available', t => {
335+
const { USDC } = brands;
336+
const requested = make(USDC, 200n);
337+
const available = make(USDC, 100n);
338+
const outstandingLends = make(USDC, 50n);
339+
const poolStats = makeInitialPoolStats();
340+
341+
t.throws(
342+
() => borrowCalc(requested, available, outstandingLends, poolStats),
343+
{
344+
message: /Cannot borrow/,
345+
},
346+
);
347+
});
348+
349+
test('basic repay calculation', t => {
350+
const { USDC } = brands;
351+
const shareWorth = makeParity(make(USDC, 1n), brands.PoolShares);
352+
const amounts = {
353+
Principal: make(USDC, 100n),
354+
PoolFee: make(USDC, 10n),
355+
ContractFee: make(USDC, 5n),
356+
};
357+
const outstandingLends = make(USDC, 200n);
358+
const poolStats = makeInitialPoolStats();
359+
360+
const result = repayCalc(shareWorth, amounts, outstandingLends, poolStats);
361+
362+
t.deepEqual(
363+
result.outstandingLends,
364+
make(USDC, 100n),
365+
'Outstanding lends should decrease by principal',
366+
);
367+
t.deepEqual(
368+
result.poolStats.totalRepays,
369+
amounts.Principal,
370+
'Total repays should increase by principal',
371+
);
372+
t.deepEqual(
373+
result.poolStats.totalPoolFees,
374+
amounts.PoolFee,
375+
'Total pool fees should increase by pool fee',
376+
);
377+
t.deepEqual(
378+
result.poolStats.totalContractFees,
379+
amounts.ContractFee,
380+
'Total contract fees should increase by contract fee',
381+
);
382+
t.deepEqual(
383+
result.poolStats.totalBorrows,
384+
poolStats.totalBorrows,
385+
'Total borrows should remain unchanged',
386+
);
387+
t.deepEqual(
388+
result.shareWorth.numerator,
389+
make(USDC, 11n),
390+
'Share worth numerator should increase by pool fee',
391+
);
392+
t.deepEqual(
393+
Object.keys(result.poolStats),
394+
Object.keys(poolStats),
395+
'repayCalc returns all poolStats fields',
396+
);
397+
});

0 commit comments

Comments
 (0)