@@ -5,7 +5,16 @@ import { ethers, upgrades } from 'hardhat';
55import { EscrowFactory , HMToken , Staking } from '../typechain-types' ;
66import { faker } from '@faker-js/faker' ;
77
8- let owner : Signer , launcher : Signer , admin : Signer ;
8+ let owner : Signer ,
9+ launcher1 : Signer ,
10+ admin : Signer ,
11+ launcher2 : Signer ,
12+ exchangeOracle : Signer ,
13+ recordingOracle : Signer ,
14+ reputationOracle : Signer ;
15+ let exchangeOracleAddress : string ,
16+ recordingOracleAddress : string ,
17+ reputationOracleAddress : string ;
918
1019let token : HMToken , escrowFactory : EscrowFactory , staking : Staking ;
1120let stakingAddress : string , tokenAddress : string ;
@@ -24,7 +33,18 @@ async function stake(staker: Signer, amount: bigint = FIXTURE_STAKE_AMOUNT) {
2433
2534describe ( 'EscrowFactory' , function ( ) {
2635 before ( async ( ) => {
27- [ owner , launcher , admin ] = await ethers . getSigners ( ) ;
36+ [
37+ owner ,
38+ launcher1 ,
39+ admin ,
40+ launcher2 ,
41+ exchangeOracle ,
42+ recordingOracle ,
43+ reputationOracle ,
44+ ] = await ethers . getSigners ( ) ;
45+ exchangeOracleAddress = await exchangeOracle . getAddress ( ) ;
46+ recordingOracleAddress = await recordingOracle . getAddress ( ) ;
47+ reputationOracleAddress = await reputationOracle . getAddress ( ) ;
2848
2949 const HMToken = await ethers . getContractFactory (
3050 'contracts/HMToken.sol:HMToken'
@@ -39,7 +59,10 @@ describe('EscrowFactory', function () {
3959
4060 await token
4161 . connect ( owner )
42- . transfer ( await launcher . getAddress ( ) , ethers . parseEther ( '100000' ) ) ;
62+ . transfer ( await launcher1 . getAddress ( ) , ethers . parseEther ( '100000' ) ) ;
63+ await token
64+ . connect ( owner )
65+ . transfer ( await launcher2 . getAddress ( ) , ethers . parseEther ( '100000' ) ) ;
4366
4467 const Staking = await ethers . getContractFactory ( 'Staking' ) ;
4568 staking = await Staking . deploy (
@@ -50,8 +73,6 @@ describe('EscrowFactory', function () {
5073 ) ;
5174 stakingAddress = await staking . getAddress ( ) ;
5275
53- await token . connect ( launcher ) . approve ( await staking . getAddress ( ) , 1000 ) ;
54-
5576 const EscrowFactory = await ethers . getContractFactory (
5677 'contracts/EscrowFactory.sol:EscrowFactory'
5778 ) ;
@@ -109,7 +130,7 @@ describe('EscrowFactory', function () {
109130
110131 it ( 'reverts when caller is not the owner' , async ( ) => {
111132 await expect (
112- escrowFactory . connect ( launcher ) . setStakingAddress ( stakingAddress )
133+ escrowFactory . connect ( launcher1 ) . setStakingAddress ( stakingAddress )
113134 ) . to . be . revertedWith ( 'Ownable: caller is not the owner' ) ;
114135 } ) ;
115136 } ) ;
@@ -147,7 +168,7 @@ describe('EscrowFactory', function () {
147168
148169 it ( 'reverts when caller is not the owner' , async ( ) => {
149170 await expect (
150- escrowFactory . connect ( launcher ) . setMinimumStake ( 0 )
171+ escrowFactory . connect ( launcher1 ) . setMinimumStake ( 0 )
151172 ) . to . be . revertedWith ( 'Ownable: caller is not the owner' ) ;
152173 } ) ;
153174 } ) ;
@@ -176,7 +197,7 @@ describe('EscrowFactory', function () {
176197
177198 it ( 'reverts when caller is not the owner' , async ( ) => {
178199 await expect (
179- escrowFactory . connect ( launcher ) . setAdmin ( await admin . getAddress ( ) )
200+ escrowFactory . connect ( launcher1 ) . setAdmin ( await admin . getAddress ( ) )
180201 ) . to . be . revertedWith ( 'Ownable: caller is not the owner' ) ;
181202 } ) ;
182203 } ) ;
@@ -197,18 +218,18 @@ describe('EscrowFactory', function () {
197218 it ( 'reverts when launcher has insufficient stake' , async ( ) => {
198219 await expect (
199220 escrowFactory
200- . connect ( launcher )
221+ . connect ( launcher1 )
201222 . createEscrow ( tokenAddress , FIXTURE_REQUESTER_ID )
202223 ) . to . be . revertedWith ( 'Insufficient stake' ) ;
203224 } ) ;
204225 } ) ;
205226
206227 describe ( 'succeeds' , ( ) => {
207228 it ( 'creates an escrow successfully' , async ( ) => {
208- await stake ( launcher ) ;
229+ await stake ( launcher1 ) ;
209230
210231 const tx = await escrowFactory
211- . connect ( launcher )
232+ . connect ( launcher1 )
212233 . createEscrow ( tokenAddress , FIXTURE_REQUESTER_ID ) ;
213234
214235 await expect ( tx )
@@ -229,4 +250,137 @@ describe('EscrowFactory', function () {
229250 } ) ;
230251 } ) ;
231252 } ) ;
253+
254+ describe ( 'createFundAndSetupEscrow()' , ( ) => {
255+ const fee = faker . number . int ( { min : 1 , max : 5 } ) ;
256+ const manifestUrl = faker . internet . url ( ) ;
257+ const manifestHash = faker . string . alphanumeric ( 46 ) ;
258+ const fundAmount = ethers . parseEther (
259+ faker . finance . amount ( { min : 1 , max : 100 } )
260+ ) ;
261+ describe ( 'reverts' , ( ) => {
262+ it ( 'reverts when fund amount is 0' , async ( ) => {
263+ await expect (
264+ escrowFactory
265+ . connect ( launcher2 )
266+ . createFundAndSetupEscrow (
267+ tokenAddress ,
268+ 0 ,
269+ FIXTURE_REQUESTER_ID ,
270+ reputationOracleAddress ,
271+ recordingOracleAddress ,
272+ exchangeOracleAddress ,
273+ fee ,
274+ fee ,
275+ fee ,
276+ manifestUrl ,
277+ manifestHash
278+ )
279+ ) . to . be . revertedWith ( 'Amount is 0' ) ;
280+ } ) ;
281+
282+ it ( 'reverts when launcher has insufficient stake' , async ( ) => {
283+ await expect (
284+ escrowFactory
285+ . connect ( launcher2 )
286+ . createFundAndSetupEscrow (
287+ tokenAddress ,
288+ fundAmount ,
289+ FIXTURE_REQUESTER_ID ,
290+ reputationOracleAddress ,
291+ recordingOracleAddress ,
292+ exchangeOracleAddress ,
293+ fee ,
294+ fee ,
295+ fee ,
296+ manifestUrl ,
297+ manifestHash
298+ )
299+ ) . to . be . revertedWith ( 'Insufficient stake' ) ;
300+ } ) ;
301+
302+ it ( 'reverts when allowance is too low' , async ( ) => {
303+ await stake ( launcher2 ) ;
304+ await token
305+ . connect ( launcher2 )
306+ . approve ( await escrowFactory . getAddress ( ) , fundAmount / 2n ) ;
307+
308+ await expect (
309+ escrowFactory
310+ . connect ( launcher2 )
311+ . createFundAndSetupEscrow (
312+ tokenAddress ,
313+ fundAmount ,
314+ FIXTURE_REQUESTER_ID ,
315+ reputationOracleAddress ,
316+ recordingOracleAddress ,
317+ exchangeOracleAddress ,
318+ fee ,
319+ fee ,
320+ fee ,
321+ manifestUrl ,
322+ manifestHash
323+ )
324+ ) . to . be . revertedWith ( 'Spender allowance too low' ) ;
325+ } ) ;
326+ } ) ;
327+
328+ describe ( 'succeeds' , ( ) => {
329+ it ( 'creates an escrow successfully' , async ( ) => {
330+ await stake ( launcher2 ) ;
331+
332+ await token
333+ . connect ( launcher2 )
334+ . approve ( await escrowFactory . getAddress ( ) , fundAmount ) ;
335+
336+ const tx = await escrowFactory
337+ . connect ( launcher2 )
338+ . createFundAndSetupEscrow (
339+ tokenAddress ,
340+ fundAmount ,
341+ FIXTURE_REQUESTER_ID ,
342+ reputationOracleAddress ,
343+ recordingOracleAddress ,
344+ exchangeOracleAddress ,
345+ fee ,
346+ fee ,
347+ fee ,
348+ manifestUrl ,
349+ manifestHash
350+ ) ;
351+
352+ const receipt = await tx . wait ( ) ;
353+ const event = (
354+ receipt ?. logs ?. find ( ( { topics } ) =>
355+ topics . includes ( ethers . id ( 'LaunchedV2(address,address,string)' ) )
356+ ) as EventLog
357+ ) ?. args ;
358+
359+ expect ( event ) . to . not . be . undefined ;
360+ const escrowAddress = event [ 1 ] ;
361+
362+ const escrow = await ethers . getContractAt (
363+ 'contracts/Escrow.sol:Escrow' ,
364+ escrowAddress
365+ ) ;
366+
367+ await expect ( tx )
368+ . to . emit ( escrowFactory , 'LaunchedV2' )
369+ . withArgs ( tokenAddress , escrowAddress , FIXTURE_REQUESTER_ID )
370+ . to . emit ( escrow , 'PendingV2' )
371+ . withArgs (
372+ manifestUrl ,
373+ manifestHash ,
374+ reputationOracleAddress ,
375+ recordingOracleAddress ,
376+ exchangeOracleAddress
377+ )
378+ . to . emit ( escrow , 'Fund' )
379+ . withArgs ( fundAmount ) ;
380+
381+ expect ( await escrowFactory . hasEscrow ( escrowAddress ) ) . to . be . true ;
382+ expect ( await escrowFactory . lastEscrow ( ) ) . to . equal ( escrowAddress ) ;
383+ } ) ;
384+ } ) ;
385+ } ) ;
232386} ) ;
0 commit comments