0

I have been writing a smart contract for crowd funding, and if a user donate some money his address will be mapped with the amount of money that he donated. But if I withdraw the amount the address is still mapping to zero (I also wrote code for setting amount to zero), is there any way I can completely reset the mapping to empty so that next time it will start with no records.

    function withdraw() public checkOwner{
        (bool sent,) = owner.call{value : address(this).balance}("");
        require(sent,"Fund Transfer Failed");

        // resetting donor details
        for(uint256 i = 0; i< donor.length; i++){
            address d = donor[i]; 
            DonorToAmount[d] = 0;
        }

        // resetting array
        donor = new address[](0);
    }
0

2 Answers 2

0

I think this should solve question, it resets mapping and array.

    function withdraw() public checkOwner{

        (bool sent,) = owner.call{value : address(this).balance}("");
        require(sent,"Fund Transfer Failed");

        // resetting donor details
        for(uint256 i = 0; i< donorArray.length; i++){

            donorMapping[donorArray[i]] = 0
        }

        // resetting array
        delete donorArray;
    }
4
  • the delete keyword just sets the element to zero. this doesn't achieve anything different to what to op's code did
    – sola24
    Sep 13, 2022 at 14:10
  • Please do not spam around and downvote real answer. delete is the proper way to make that operation. Either remove your downvote, or I will report you.
    – Abdulhakim
    Sep 13, 2022 at 17:01
  • 2
    If donorArray is an array delete donorArray does reset it to empty. Now the question was about mappings. In that case delete doesn't work. You should clarify what is the type of donorArray and donorMapping.
    – Ismael
    Sep 13, 2022 at 21:50
  • 2
    Also reporting shouldn't be used to signal a wrong answer.
    – Ismael
    Sep 13, 2022 at 21:52
0

there is no concept of empty or null in solidity. the default value of zero is the closest there is. all memory locations are by default, initially, zero.

It is zero before it is set it to something else, and setting it back to zero is also synonymous to not storing anything at that location, to the point where you can get some gas refund by setting storage back to zero.

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