0

I am trying to create a contract that would let a holder of an NFT call a function a single time until an event occurs at a later time and then that function may be called again. the following code function achieves the first half of what I am trying to accomplish

function registertokenId(uint256 tokenId) public {
    require(msg.sender == ownerOf(tokenId));
    require(tokenIsEntered == false);
    require(tokenEntered[tokenId] != true);
    s_entrants.push(payable(msg.sender));
    tokensEntered.push(tokenId);
    tokenEntered[tokenId] = true;

this works perfectly and as intended so far as I can tell tinkering on remix. however I am looking for an efficient way to reset all uint256 of tokenId entered into a mapping to be reset to false in a separate function.

while I am aware that the function:

function resetArrayTest() public {         
   tokenEntered[tokenId] = false;
}

resets the bool to false at the zero index of the array, I would like to find out if there is a way to reset them all at once.

Edit- Solved

this seems to clear the mapping of tokenIds assigned true values as intended rather than just at the zero index. I am assuming, that without the struct the solution function only deletes the memory at the 0th index. however if someone would like to elucidate exactly what is going on behind the scenes here I would greatly appreciate it. I have updated the full code to reflect the full changes.

mapping(uint256 => bool ) public tokenEntered;
constructor() ERC721("Test", "testing") {
     }
struct TokenTracker{
        bool tokenIsEntered;
        uint256[] tokensEntered;

    }

function mintNft() public returns (uint256) {
    _safeMint(msg.sender, s_tokenCounter);
    s_tokenCounter = s_tokenCounter + 1;
    return s_tokenCounter;
}

function tokenURI(uint256 tokenId) public view override returns (string memory) {
    // require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
    return TOKEN_URI;
}
function registertokenId(uint256 tokenId) public {
    require(msg.sender == ownerOf(tokenId));
    require(tokenIsEntered == false);
    require(tokenEntered[tokenId] != true);
    s_entrants.push(payable(msg.sender));
    tokensEntered.push(tokenId);
    tokenEntered[tokenId] = true;
}
function resetArrayTest() public {
    for (uint256 i = 0; i < tokensEntered.length; i++)
delete tokenEntered[tokensEntered[i]];
s_entrants = new address payable[](0);
}

function getTokenCounter() public view returns (uint256) {
    return s_tokenCounter;
}


function getNumberOfEntrants() public view returns (uint256){
    return s_entrants.length;
}
0

1 Answer 1

1

To recap, adding the struct

struct TokenTracker{
    bool tokenIsEntered;
    uint256[] tokensEntered;

}

allowed this line of code to reset the bool that was set to true

 for (uint256 i = 0; i < tokensEntered.length; i++)delete tokenEntered[tokensEntered[i]];

using a similar solution without the struct did not reset the values except the 0th index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.