@@ -377,8 +377,9 @@ pub mod Predifi {
377377
378378 self . pool_odds. write (pool_id , initial_odds );
379379
380- // Add to pool count
381- self . pool_count. write (self . pool_count. read () + 1 );
380+ // Cache current pool count to avoid redundant read
381+ let current_count = self . pool_count. read ();
382+ self . pool_count. write (current_count + 1 );
382383
383384 // Emit pool created event
384385 self
@@ -526,6 +527,13 @@ pub mod Predifi {
526527 self . assert_greater_than_zero (pool_id );
527528 self . assert_valid_felt252 (option );
528529
530+ // Cache frequently used values to reduce storage reads
531+ let caller = get_caller_address ();
532+ let contract_address = get_contract_address ();
533+ let token_addr = self . token_addr. read ();
534+ let dispatcher = IERC20Dispatcher { contract_address : token_addr };
535+
536+ // Single pool read for validation and data access
529537 let mut pool = self . pools. read (pool_id );
530538 self . assert_pool_exists (@ pool );
531539 self . assert_pool_active (@ pool );
@@ -537,12 +545,7 @@ pub mod Predifi {
537545 self . assert_valid_pool_option (option , option1 , option2 );
538546 self . assert_amount_within_limits (amount , pool . minBetAmount, pool . maxBetAmount);
539547
540- // Transfer betting amount from the user to the contract
541- let caller = get_caller_address ();
542- let dispatcher = IERC20Dispatcher { contract_address : self . token_addr. read () };
543-
544548 // Check balance and allowance using SecurityTrait
545- let contract_address = get_contract_address ();
546549 self . assert_sufficient_balance (dispatcher , caller , amount );
547550 self . assert_sufficient_allowance (dispatcher , caller , contract_address , amount );
548551
@@ -552,7 +555,7 @@ pub mod Predifi {
552555 // Transfer the tokens
553556 dispatcher . transfer_from (caller , contract_address , amount );
554557
555- let mut pool = self . pools . read ( pool_id );
558+ // Update pool state in memory
556559 if option == option1 {
557560 pool . totalStakeOption1 += amount ;
558561 pool
@@ -567,37 +570,44 @@ pub mod Predifi {
567570 pool . totalBetAmountStrk += amount ;
568571 pool . totalBetCount += 1 ;
569572
570- // Update pool odds
571- let odds = self
572- . calculate_odds (pool . pool_id, pool . totalStakeOption1, pool . totalStakeOption2);
573- self . pool_odds. write (pool_id , odds );
574-
575573 // Calculate the user's shares
576574 let shares : u256 = if option == option1 {
577575 self . calculate_shares (amount , pool . totalStakeOption1, pool . totalStakeOption2)
578576 } else {
579577 self . calculate_shares (amount , pool . totalStakeOption2, pool . totalStakeOption1)
580578 };
581579
582- // Store user stake
580+ // Create user stake object
583581 let user_stake = UserStake {
584582 option : option == option2 ,
585583 amount : amount ,
586584 shares : shares ,
587585 timestamp : get_block_timestamp (),
588586 };
589- let address : ContractAddress = get_caller_address ();
590- self . user_stakes. write ((pool . pool_id, address ), user_stake );
591- self . pool_vote. write (pool . pool_id, option == option2 );
592- self . pool_stakes. write (pool . pool_id, user_stake );
593- self . pools. write (pool . pool_id, pool );
594- self . track_user_participation (address , pool_id );
587+
588+ // Batch storage writes to minimize gas costs
589+ self
590+ . pool_odds
591+ . write (
592+ pool_id ,
593+ self . calculate_odds (pool_id , pool . totalStakeOption1, pool . totalStakeOption2),
594+ );
595+ self . user_stakes. write ((pool_id , caller ), user_stake );
596+ self . pool_vote. write (pool_id , option == option2 );
597+ self . pool_stakes. write (pool_id , user_stake );
598+ self . pools. write (pool_id , pool );
599+ self . track_user_participation (caller , pool_id );
595600
596601 // End reentrancy guard
597602 self . reentrancy_guard. end ();
598603
599604 // Emit event
600- self . emit (Event :: BetPlaced (BetPlaced { pool_id , address , option , amount , shares }));
605+ self
606+ . emit (
607+ Event :: BetPlaced (
608+ BetPlaced { pool_id , address : caller , option , amount , shares },
609+ ),
610+ );
601611 }
602612
603613 /// @notice Stakes tokens to become eligible for validation rewards
@@ -619,13 +629,13 @@ pub mod Predifi {
619629 self . assert_pool_not_suspended (@ pool );
620630 self . assert_min_stake_amount (amount );
621631
632+ // Cache frequently used values to reduce redundant calls
622633 let address : ContractAddress = get_caller_address ();
623-
624- // Transfer stake amount from user to contract
625- let dispatcher = IERC20Dispatcher { contract_address : self . token_addr. read () };
634+ let contract_address = get_contract_address ();
635+ let token_addr = self . token_addr . read ();
636+ let dispatcher = IERC20Dispatcher { contract_address : token_addr };
626637
627638 // Check balance and allowance using SecurityTrait
628- let contract_address = get_contract_address ();
629639 self . assert_sufficient_balance (dispatcher , address , amount );
630640 self . assert_sufficient_allowance (dispatcher , address , contract_address , amount );
631641
@@ -635,22 +645,22 @@ pub mod Predifi {
635645 // Transfer the tokens
636646 dispatcher . transfer_from (address , contract_address , amount );
637647
638- // Add to previous stake if any
648+ // Read and update user stake in a single operation
639649 let mut stake = self . user_stakes. read ((pool_id , address ));
640650 stake . amount = amount + stake . amount;
641- // write the new stake
651+
652+ // Batch storage writes to minimize gas costs
642653 self . user_stakes. write ((pool_id , address ), stake );
643- // grant the validator role
644654 self . accesscontrol. _grant_role (VALIDATOR_ROLE , address );
645- // add caller to validator list
646655 self . validators. push (address );
647656 self . track_user_participation (address , pool_id );
648- // emit events
649- self . emit (UserStaked { pool_id , address , amount });
650- self . emit (ValidatorAdded { account : address , caller : get_caller_address () });
651657
652658 // End reentrancy guard
653659 self . reentrancy_guard. end ();
660+
661+ // emit events
662+ self . emit (UserStaked { pool_id , address , amount });
663+ self . emit (ValidatorAdded { account : address , caller : get_caller_address () });
654664 }
655665
656666
@@ -908,6 +918,11 @@ pub mod Predifi {
908918 /// @dev Allows users to withdraw funds from pools in emergency state.
909919 /// @param pool_id The pool ID to withdraw from.
910920 fn emergency_withdraw (ref self : ContractState , pool_id : u256 ) {
921+ // Cache frequently used values to reduce storage reads
922+ let caller = get_caller_address ();
923+ let token_addr = self . token_addr. read ();
924+ let strk_token = IERC20Dispatcher { contract_address : token_addr };
925+
911926 // Check if pool exists
912927 let pool = self . pools. read (pool_id );
913928 assert (pool . exists, Errors :: POOL_DOES_NOT_EXIST );
@@ -920,8 +935,6 @@ pub mod Predifi {
920935 Errors :: EMERGENCY_WITHDRAWALS_NOT_ALLOWED ,
921936 );
922937
923- let caller = get_caller_address ();
924-
925938 // Check if user has participated in this pool
926939 let has_participated = self . has_user_participated_in_pool (caller , pool_id );
927940 assert (has_participated , Errors :: UNAUTHORIZED_CALLER );
@@ -942,7 +955,6 @@ pub mod Predifi {
942955 );
943956
944957 // Transfer tokens back to user
945- let strk_token = IERC20Dispatcher { contract_address : self . token_addr. read () };
946958 strk_token . transfer (caller , withdrawal_amount );
947959
948960 // Emit emergency withdrawal event
@@ -989,7 +1001,7 @@ pub mod Predifi {
9891001 if action_type == 3 { // EmergencyWithdrawal
9901002 // For emergency withdrawal, action_data must contain a valid user address
9911003 // Use the helper function to validate the address and handle potential panics
992- let user_address = self . extract_user_address_from_action_data (action_data );
1004+ let _user_address = self . extract_user_address_from_action_data (action_data );
9931005 // Store the validated address for later use if needed
9941006 // Note: The validation is done here to fail fast if the address is invalid
9951007 }
@@ -1941,7 +1953,7 @@ pub mod Predifi {
19411953 /// @param total_stake_other_option Total stake for other option.
19421954 /// @return The calculated shares.
19431955 fn calculate_shares (
1944- ref self : ContractState ,
1956+ self : @ ContractState ,
19451957 amount : u256 ,
19461958 total_stake_selected_option : u256 ,
19471959 total_stake_other_option : u256 ,
@@ -1962,7 +1974,7 @@ pub mod Predifi {
19621974 /// @param total_stake_option2 Total stake for option 2.
19631975 /// @return The PoolOdds struct.
19641976 fn calculate_odds (
1965- ref self : ContractState ,
1977+ self : @ ContractState ,
19661978 pool_id : u256 ,
19671979 total_stake_option1 : u256 ,
19681980 total_stake_option2 : u256 ,
0 commit comments