@@ -12,8 +12,70 @@ describe("Factory", function () {
1212
1313 // Deploy RaffleNFT (not upgradeable)
1414 const RaffleNFT = await ethers . getContractFactory ( "RaffleNFT" ) ;
15- const nft = await RaffleNFT . deploy ( "TestNFT" , "TNFT" ) ;
15+ const nft = await RaffleNFT . deploy ( "TestNFT" , "TNFT" , "https://example.com/metadata.json" ) ;
1616 await nft . waitForDeployment ( ) ;
1717 expect ( nft . target ) . to . properAddress ;
1818 } ) ;
1919} ) ;
20+
21+ describe ( "RaffleFactory full flow" , function ( ) {
22+ it ( "Should deploy ERC20, RaffleFactory, and create Raffle via factory with correct checks" , async function ( ) {
23+ const [ owner , manager ] = await ethers . getSigners ( ) ;
24+
25+ // Deploy test ERC20 token
26+ const ERC20TestToken = await ethers . getContractFactory ( "ERC20TestToken" , manager ) ;
27+
28+ const erc20 = await ERC20TestToken . deploy ( "TestToken" , "TTK" ) ;
29+ await erc20 . waitForDeployment ( ) ;
30+ expect ( erc20 . target ) . to . properAddress ;
31+
32+ // Mint tokens to manager
33+ await erc20 . mint ( manager . address , 1000 ) ;
34+ expect ( await erc20 . balanceOf ( manager . address ) ) . to . equal ( 1000 ) ;
35+
36+ // Deploy RaffleFactory as UUPS proxy (owner)
37+ const RaffleFactory = await ethers . getContractFactory ( "RaffleFactory" , owner ) ;
38+ const factory = await upgrades . deployProxy ( RaffleFactory , [ ] , { kind : "uups" } ) ;
39+ await factory . waitForDeployment ( ) ;
40+ expect ( factory . target ) . to . properAddress ;
41+
42+ // Grant MANAGER_ROLE to manager
43+ const MANAGER_ROLE = await factory . MANAGER_ROLE ( ) ;
44+ await factory . grantRole ( MANAGER_ROLE , manager . address ) ;
45+ expect ( await factory . hasRole ( MANAGER_ROLE , manager . address ) ) . to . be . true ;
46+
47+ // Manager approves factory to spend tokens
48+ await erc20 . connect ( manager ) . approve ( factory . target , 500 ) ;
49+ expect ( await erc20 . allowance ( manager . address , factory . target ) ) . to . equal ( 500 ) ;
50+
51+ // Manager creates a new Raffle
52+ const tokenURI = "https://example.com/metadata.json" ;
53+ const tx = await factory . connect ( manager ) . createRaffle (
54+ "TestRaffle" ,
55+ "TRFL" ,
56+ tokenURI ,
57+ erc20 . target ,
58+ 500
59+ ) ;
60+ const receipt = await tx . wait ( ) ;
61+
62+ // Check event
63+ const event = receipt . logs . find ( l => l . fragment && l . fragment . name === "RaffleCreated" ) ;
64+ expect ( event ) . to . exist ;
65+ const raffleAddress = event . args . raffleAddress ;
66+ expect ( raffleAddress ) . to . properAddress ;
67+
68+ // Check balances
69+ expect ( await erc20 . balanceOf ( manager . address ) ) . to . equal ( 500 ) ;
70+ expect ( await erc20 . balanceOf ( factory . target ) ) . to . equal ( 0 ) ;
71+ expect ( await erc20 . balanceOf ( raffleAddress ) ) . to . equal ( 500 ) ;
72+
73+ // Check RaffleNFT state
74+ const RaffleNFT = await ethers . getContractFactory ( "RaffleNFT" ) ;
75+ const raffle = await RaffleNFT . attach ( raffleAddress ) ;
76+ expect ( await raffle . prizeToken ( ) ) . to . equal ( erc20 . target ) ;
77+ expect ( await raffle . amount ( ) ) . to . equal ( 500 ) ;
78+ expect ( await raffle . started ( ) ) . to . be . true ;
79+ expect ( await raffle . tokenURI ( 0 ) ) . to . equal ( tokenURI ) ;
80+ } ) ;
81+ } ) ;
0 commit comments