hi. can someone help doing this please. WHat i am doing ins not working

//Check if msg.sender address is in address[] players
//return true if true and false is false
//require msg.sender is NOT in address[] players in oneEntry()

address[] public players

modifier oneEntry() {

require( msg.sender != address(1) , "Only one entry per round");
_;


}

Apr 1, 2023, 10:29 PM
You need to iterate through all elements in players array and check one by one
address(1) is just 0x0000..0001
Probably not a good idea to do so coz as the players array grows, the gas required for loop also grows and eventually your function will run out of gas
Apr 1, 2023, 10:43 PM
that was just a place holder
there will only be 10. and i will be clearing it each round
Apr 1, 2023, 10:51 PM
To check if an address is already in an array, you can loop through the array and compare each element with the address you want to check. Here's an example implementation:

address[] public players;

modifier oneEntry() {
bool alreadyEntered = false;
for (uint i = 0; i < players.length; i++) {
if (players[i] == msg.sender) {
alreadyEntered = true;
break;
}
}
require(!alreadyEntered, "Only one entry per round");
_;
}
Apr 1, 2023, 10:58 PM
use a bool or Uint mapping variable to keep track if an address has participated
or this
Apr 2, 2023, 2:54 AM

© 2024 Draquery.com All rights reserved.