@@ -471,24 +471,98 @@ describe("Offset Helper - autoOffset", function () {
471
471
} ) ;
472
472
473
473
describe ( "Testing autoRetire()" , function ( ) {
474
- it ( "Should retire using an NCT deposit" , async function ( ) {
475
- await ( await nct . approve ( offsetHelper . address , ONE_ETHER ) ) . wait ( ) ;
474
+ it ( "should retire using an NCT deposit" , async function ( ) {
475
+ // first we set the initial state
476
+ const state : {
477
+ userNctBalance : BigNumber ;
478
+ contractNctBalance : BigNumber ;
479
+ nctSupply : BigNumber ;
480
+ } [ ] = [ ] ;
481
+ state . push ( {
482
+ userNctBalance : await nct . balanceOf ( addr2 . address ) ,
483
+ contractNctBalance : await nct . balanceOf ( offsetHelper . address ) ,
484
+ nctSupply : await nct . totalSupply ( ) ,
485
+ } ) ;
476
486
487
+ // we deposit NCT into OH
488
+ await ( await nct . approve ( offsetHelper . address , ONE_ETHER ) ) . wait ( ) ;
477
489
await ( await offsetHelper . deposit ( addresses . nct , ONE_ETHER ) ) . wait ( ) ;
478
490
491
+ // and we check the state after the deposit
492
+ state . push ( {
493
+ userNctBalance : await nct . balanceOf ( addr2 . address ) ,
494
+ contractNctBalance : await nct . balanceOf ( offsetHelper . address ) ,
495
+ nctSupply : await nct . totalSupply ( ) ,
496
+ } ) ;
497
+ expect (
498
+ formatEther ( state [ 0 ] . userNctBalance . sub ( state [ 1 ] . userNctBalance ) ) ,
499
+ "User should have 1 less NCT post deposit"
500
+ ) . to . equal ( formatEther ( ONE_ETHER ) ) ;
501
+ expect (
502
+ formatEther (
503
+ state [ 1 ] . contractNctBalance . sub ( state [ 0 ] . contractNctBalance )
504
+ ) ,
505
+ "Contract should have 1 more NCT post deposit"
506
+ ) . to . equal ( formatEther ( ONE_ETHER ) ) ;
507
+ expect (
508
+ formatEther ( state [ 0 ] . nctSupply ) ,
509
+ "NCT supply should be the same post deposit"
510
+ ) . to . equal ( formatEther ( state [ 1 ] . nctSupply ) ) ;
511
+
512
+ // we redeem NCT for TCO2 within OH
479
513
const redeemReceipt = await (
480
514
await offsetHelper . autoRedeem ( addresses . nct , ONE_ETHER )
481
515
) . wait ( ) ;
482
516
483
- if ( ! redeemReceipt . events ) {
484
- return ;
485
- }
517
+ // and we check the state after the redeem
518
+ state . push ( {
519
+ userNctBalance : await nct . balanceOf ( addr2 . address ) ,
520
+ contractNctBalance : await nct . balanceOf ( offsetHelper . address ) ,
521
+ nctSupply : await nct . totalSupply ( ) ,
522
+ } ) ;
523
+ expect (
524
+ formatEther ( state [ 1 ] . userNctBalance ) ,
525
+ "User should have the same amount of NCT post redeem"
526
+ ) . to . equal ( formatEther ( state [ 2 ] . userNctBalance ) ) ;
527
+ expect (
528
+ formatEther (
529
+ state [ 1 ] . contractNctBalance . sub ( state [ 2 ] . contractNctBalance )
530
+ ) ,
531
+ "Contract should have 1 less NCT post redeem"
532
+ ) . to . equal ( formatEther ( ONE_ETHER ) ) ;
533
+ expect (
534
+ formatEther ( state [ 1 ] . nctSupply . sub ( state [ 2 ] . nctSupply ) ) ,
535
+ "NCT supply should be less by 1 post redeem"
536
+ ) . to . equal ( formatEther ( ONE_ETHER ) ) ;
537
+
538
+ // we get the tco2s and amounts that were redeemed
539
+ if ( ! redeemReceipt . events ) throw new Error ( "No events emitted" ) ;
486
540
const tco2s =
487
541
redeemReceipt . events [ redeemReceipt . events . length - 1 ] . args ?. tco2s ;
488
542
const amounts =
489
543
redeemReceipt . events [ redeemReceipt . events . length - 1 ] . args ?. amounts ;
490
544
545
+ // we retire the tco2s
491
546
await offsetHelper . autoRetire ( tco2s , amounts ) ;
547
+
548
+ // and we check the state after the retire
549
+ state . push ( {
550
+ userNctBalance : await nct . balanceOf ( addr2 . address ) ,
551
+ contractNctBalance : await nct . balanceOf ( offsetHelper . address ) ,
552
+ nctSupply : await nct . totalSupply ( ) ,
553
+ } ) ;
554
+ expect (
555
+ formatEther ( state [ 2 ] . userNctBalance ) ,
556
+ "User should have the same amount of NCT post retire"
557
+ ) . to . equal ( formatEther ( state [ 3 ] . userNctBalance ) ) ;
558
+ expect (
559
+ formatEther ( state [ 2 ] . contractNctBalance ) ,
560
+ "Contract should have the same amount of NCT post retire"
561
+ ) . to . equal ( formatEther ( state [ 3 ] . contractNctBalance ) ) ;
562
+ expect (
563
+ formatEther ( state [ 2 ] . nctSupply ) ,
564
+ "NCT supply should be the same post retire"
565
+ ) . to . equal ( formatEther ( state [ 3 ] . nctSupply ) ) ;
492
566
} ) ;
493
567
494
568
it ( "Should fail because we haven't redeemed any TCO2" , async function ( ) {
@@ -500,24 +574,98 @@ describe("Offset Helper - autoOffset", function () {
500
574
) . to . be . revertedWith ( "Insufficient TCO2 balance" ) ;
501
575
} ) ;
502
576
503
- it ( "Should retire using an NCT deposit" , async function ( ) {
504
- await ( await bct . approve ( offsetHelper . address , ONE_ETHER ) ) . wait ( ) ;
577
+ it ( "should retire using an BCT deposit" , async function ( ) {
578
+ // first we set the initial state
579
+ const state : {
580
+ userBctBalance : BigNumber ;
581
+ contractBctBalance : BigNumber ;
582
+ bctSupply : BigNumber ;
583
+ } [ ] = [ ] ;
584
+ state . push ( {
585
+ userBctBalance : await bct . balanceOf ( addr2 . address ) ,
586
+ contractBctBalance : await bct . balanceOf ( offsetHelper . address ) ,
587
+ bctSupply : await bct . totalSupply ( ) ,
588
+ } ) ;
505
589
590
+ // we deposit BCT into OH
591
+ await ( await bct . approve ( offsetHelper . address , ONE_ETHER ) ) . wait ( ) ;
506
592
await ( await offsetHelper . deposit ( addresses . bct , ONE_ETHER ) ) . wait ( ) ;
507
593
594
+ // and we check the state after the deposit
595
+ state . push ( {
596
+ userBctBalance : await bct . balanceOf ( addr2 . address ) ,
597
+ contractBctBalance : await bct . balanceOf ( offsetHelper . address ) ,
598
+ bctSupply : await bct . totalSupply ( ) ,
599
+ } ) ;
600
+ expect (
601
+ formatEther ( state [ 0 ] . userBctBalance . sub ( state [ 1 ] . userBctBalance ) ) ,
602
+ "User should have 1 less BCT post deposit"
603
+ ) . to . equal ( formatEther ( ONE_ETHER ) ) ;
604
+ expect (
605
+ formatEther (
606
+ state [ 1 ] . contractBctBalance . sub ( state [ 0 ] . contractBctBalance )
607
+ ) ,
608
+ "Contract should have 1 more BCT post deposit"
609
+ ) . to . equal ( formatEther ( ONE_ETHER ) ) ;
610
+ expect (
611
+ formatEther ( state [ 0 ] . bctSupply ) ,
612
+ "BCT supply should be the same post deposit"
613
+ ) . to . equal ( formatEther ( state [ 1 ] . bctSupply ) ) ;
614
+
615
+ // we redeem BCT for TCO2 within OH
508
616
const redeemReceipt = await (
509
617
await offsetHelper . autoRedeem ( addresses . bct , ONE_ETHER )
510
618
) . wait ( ) ;
511
619
512
- if ( ! redeemReceipt . events ) {
513
- return ;
514
- }
620
+ // and we check the state after the redeem
621
+ state . push ( {
622
+ userBctBalance : await bct . balanceOf ( addr2 . address ) ,
623
+ contractBctBalance : await bct . balanceOf ( offsetHelper . address ) ,
624
+ bctSupply : await bct . totalSupply ( ) ,
625
+ } ) ;
626
+ expect (
627
+ formatEther ( state [ 1 ] . userBctBalance ) ,
628
+ "User should have the same amount of BCT post redeem"
629
+ ) . to . equal ( formatEther ( state [ 2 ] . userBctBalance ) ) ;
630
+ expect (
631
+ formatEther (
632
+ state [ 1 ] . contractBctBalance . sub ( state [ 2 ] . contractBctBalance )
633
+ ) ,
634
+ "Contract should have 1 less BCT post redeem"
635
+ ) . to . equal ( formatEther ( ONE_ETHER ) ) ;
636
+ expect (
637
+ formatEther ( state [ 1 ] . bctSupply . sub ( state [ 2 ] . bctSupply ) ) ,
638
+ "BCT supply should be less by 1 post redeem"
639
+ ) . to . equal ( formatEther ( ONE_ETHER ) ) ;
640
+
641
+ // we get the tco2s and amounts that were redeemed
642
+ if ( ! redeemReceipt . events ) throw new Error ( "No events emitted" ) ;
515
643
const tco2s =
516
644
redeemReceipt . events [ redeemReceipt . events . length - 1 ] . args ?. tco2s ;
517
645
const amounts =
518
646
redeemReceipt . events [ redeemReceipt . events . length - 1 ] . args ?. amounts ;
519
647
648
+ // we retire the tco2s
520
649
await offsetHelper . autoRetire ( tco2s , amounts ) ;
650
+
651
+ // and we check the state after the retire
652
+ state . push ( {
653
+ userBctBalance : await bct . balanceOf ( addr2 . address ) ,
654
+ contractBctBalance : await bct . balanceOf ( offsetHelper . address ) ,
655
+ bctSupply : await bct . totalSupply ( ) ,
656
+ } ) ;
657
+ expect (
658
+ formatEther ( state [ 2 ] . userBctBalance ) ,
659
+ "User should have the same amount of BCT post retire"
660
+ ) . to . equal ( formatEther ( state [ 3 ] . userBctBalance ) ) ;
661
+ expect (
662
+ formatEther ( state [ 2 ] . contractBctBalance ) ,
663
+ "Contract should have the same amount of BCT post retire"
664
+ ) . to . equal ( formatEther ( state [ 3 ] . contractBctBalance ) ) ;
665
+ expect (
666
+ formatEther ( state [ 2 ] . bctSupply ) ,
667
+ "BCT supply should be the same post retire"
668
+ ) . to . equal ( formatEther ( state [ 3 ] . bctSupply ) ) ;
521
669
} ) ;
522
670
} ) ;
523
671
0 commit comments