@@ -7,8 +7,10 @@ import {
7
7
} from '@agoric/zoe/src/contractSupport/ratio.js' ;
8
8
import { mustMatch } from '@endo/patterns' ;
9
9
import {
10
+ borrowCalc ,
10
11
depositCalc ,
11
12
makeParity ,
13
+ repayCalc ,
12
14
withdrawCalc ,
13
15
withFees ,
14
16
} from '../src/pool-share-math.js' ;
@@ -295,3 +297,101 @@ testProp(
295
297
) ;
296
298
} ,
297
299
) ;
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 : / C a n n o t b o r r o w / ,
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