11import { expect } from "chai" ;
2- import { ethers } from "hardhat" ;
2+ import { ethers , network } from "hardhat" ;
33import { Contract , Signer } from "ethers" ;
44
55import { TimeHelpers , setupBondFactory } from "./helpers" ;
66
7+ const START_TIME = 2499998400 ;
8+ const mockTime = ( x : number ) => START_TIME + x ;
9+
710let bondFactory : Contract , token : Contract , issuer : Contract , deployer : Signer , otherUser : Signer ;
811describe ( "BondIssuer" , function ( ) {
912 beforeEach ( async function ( ) {
@@ -16,15 +19,20 @@ describe("BondIssuer", function () {
1619 await token . init ( "Test token" , "TEST" ) ;
1720 const BondIssuer = await ethers . getContractFactory ( "BondIssuer" ) ;
1821 issuer = await BondIssuer . deploy ( bondFactory . address , token . address ) ;
19- await issuer . init ( 86400 , [ 200 , 300 , 500 ] , 3600 , 120 ) ;
22+ await issuer . init ( 86400 , [ 200 , 300 , 500 ] , 3600 , 900 ) ;
23+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 0 ) ) ;
24+ } ) ;
25+
26+ afterEach ( async function ( ) {
27+ await network . provider . send ( "hardhat_reset" ) ;
2028 } ) ;
2129
2230 describe ( "#setup" , function ( ) {
2331 it ( "should set storage parameters" , async function ( ) {
2432 expect ( await issuer . owner ( ) ) . to . eq ( await deployer . getAddress ( ) ) ;
2533 expect ( await issuer . bondFactory ( ) ) . to . eq ( bondFactory . address ) ;
2634 expect ( await issuer . minIssueTimeIntervalSec ( ) ) . to . eq ( 3600 ) ;
27- expect ( await issuer . issueWindowOffsetSec ( ) ) . to . eq ( 120 ) ;
35+ expect ( await issuer . issueWindowOffsetSec ( ) ) . to . eq ( 900 ) ;
2836 expect ( await issuer . maxMaturityDuration ( ) ) . to . eq ( 86400 ) ;
2937 expect ( await issuer . collateral ( ) ) . to . eq ( token . address ) ;
3038 expect ( await issuer . trancheRatios ( 0 ) ) . to . eq ( 200 ) ;
@@ -94,7 +102,7 @@ describe("BondIssuer", function () {
94102 describe ( "#issue" , function ( ) {
95103 describe ( "when sufficient time has passed" , function ( ) {
96104 it ( "should issue a new bond" , async function ( ) {
97- await TimeHelpers . setNextBlockTimestamp ( 2499998400 ) ;
105+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 901 ) ) ;
98106 const tx = await issuer . issue ( ) ;
99107 const txR = await tx . wait ( ) ;
100108 const bondIssuedEvent = txR . events [ txR . events . length - 1 ] ;
@@ -103,34 +111,74 @@ describe("BondIssuer", function () {
103111 expect ( tx ) . to . emit ( issuer , "BondIssued" ) ;
104112 expect ( await issuer . isInstance ( bond ) ) . to . eq ( true ) ;
105113 expect ( await issuer . callStatic . getLatestBond ( ) ) . to . eq ( bond ) ;
106- expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( 2499998520 ) ;
114+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( mockTime ( 900 ) ) ;
107115
108116 expect ( await issuer . issuedCount ( ) ) . to . eq ( 1 ) ;
109117 expect ( await issuer . issuedBondAt ( 0 ) ) . to . eq ( bond ) ;
110118 await expect ( issuer . issuedBondAt ( 1 ) ) . to . be . reverted ;
111119
112- await TimeHelpers . setNextBlockTimestamp ( 2500002120 ) ;
120+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 4495 ) ) ;
121+ await expect ( issuer . issue ( ) ) . not . to . emit ( issuer , "BondIssued" ) ;
122+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( mockTime ( 900 ) ) ;
123+
124+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 4501 ) ) ;
113125 await expect ( issuer . issue ( ) ) . to . emit ( issuer , "BondIssued" ) ;
114- expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( 2500002120 ) ;
126+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( mockTime ( 4500 ) ) ;
115127
116128 expect ( await issuer . issuedCount ( ) ) . to . eq ( 2 ) ;
117129 await expect ( issuer . issuedBondAt ( 1 ) ) . to . not . be . reverted ;
130+
131+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 4505 ) ) ;
132+ await expect ( issuer . issue ( ) ) . not . to . emit ( issuer , "BondIssued" ) ;
133+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( mockTime ( 4500 ) ) ;
118134 } ) ;
119135 } ) ;
120136
121- describe ( "when sufficient time has not passed" , function ( ) {
122- it ( "should not issue a new bond" , async function ( ) {
123- await TimeHelpers . setNextBlockTimestamp ( 2500005720 ) ;
137+ describe ( "for various elapsed times lastIssueWindowTimestamp" , function ( ) {
138+ beforeEach ( async function ( ) {
139+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( "0" ) ;
140+ } ) ;
141+
142+ it ( "should should snap down" , async function ( ) {
143+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 3500 ) ) ;
124144 await expect ( issuer . issue ( ) ) . to . emit ( issuer , "BondIssued" ) ;
125- expect ( await issuer . issuedCount ( ) ) . to . eq ( 1 ) ;
145+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( mockTime ( 900 ) ) ;
146+ } ) ;
126147
127- await TimeHelpers . setNextBlockTimestamp ( 2500009310 ) ;
128- await expect ( issuer . issue ( ) ) . not . to . emit ( issuer , "BondIssued" ) ;
129- expect ( await issuer . issuedCount ( ) ) . to . eq ( 1 ) ;
148+ it ( "should should snap down" , async function ( ) {
149+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 3595 ) ) ;
150+ await expect ( issuer . issue ( ) ) . to . emit ( issuer , "BondIssued" ) ;
151+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( mockTime ( 900 ) ) ;
152+ } ) ;
130153
131- await TimeHelpers . setNextBlockTimestamp ( 2500009320 ) ;
154+ it ( "should should snap down" , async function ( ) {
155+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 3600 ) ) ;
132156 await expect ( issuer . issue ( ) ) . to . emit ( issuer , "BondIssued" ) ;
133- expect ( await issuer . issuedCount ( ) ) . to . eq ( 2 ) ;
157+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( mockTime ( 900 ) ) ;
158+ } ) ;
159+
160+ it ( "should should snap down" , async function ( ) {
161+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 4495 ) ) ;
162+ await expect ( issuer . issue ( ) ) . to . emit ( issuer , "BondIssued" ) ;
163+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( mockTime ( 900 ) ) ;
164+ } ) ;
165+
166+ it ( "should should snap down" , async function ( ) {
167+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 4500 ) ) ;
168+ await expect ( issuer . issue ( ) ) . to . emit ( issuer , "BondIssued" ) ;
169+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( mockTime ( 4500 ) ) ;
170+ } ) ;
171+
172+ it ( "should should snap down" , async function ( ) {
173+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 4501 ) ) ;
174+ await expect ( issuer . issue ( ) ) . to . emit ( issuer , "BondIssued" ) ;
175+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( mockTime ( 4500 ) ) ;
176+ } ) ;
177+
178+ it ( "should should snap down" , async function ( ) {
179+ await TimeHelpers . setNextBlockTimestamp ( mockTime ( 4600 ) ) ;
180+ await expect ( issuer . issue ( ) ) . to . emit ( issuer , "BondIssued" ) ;
181+ expect ( await issuer . lastIssueWindowTimestamp ( ) ) . to . eq ( mockTime ( 4500 ) ) ;
134182 } ) ;
135183 } ) ;
136184 } ) ;
0 commit comments