0

how do i go about removing values from inside a struct and or a mapping. For example iam creating a smart contract that acts kind of like a vault, iam not yet finished but i have a create an account feature.I would like to add a delete an account feature aswell, i thought about just changing the values inside the struct but there bust be a better way. Here is some of my code:

// Global variables
uint256 uniqueId;

// Account details storage
struct AccDetails {
    string accNickName;
    uint256 creationTimeStamp;
}
mapping(address => AccDetails) public accDetails;

mapping(address => uint256) public balanceOf;
mapping(address => uint256) addrTouniqueIdentifier;
mapping(uint256 => address) uniqueIdentifierToAddr;

// Variables set on contract deployment
constructor() {
    uniqueId = 1;
}


function createAccount(string memory _accNickName) public {
    if(accDetails[msg.sender].creationTimeStamp == 0) {
        accDetails[msg.sender] = AccDetails(_accNickName, block.timestamp);
        addrTouniqueIdentifier[msg.sender] = uniqueId;
        uniqueIdentifierToAddr[uniqueId] = msg.sender;

        emit accountCreated(msg.sender,_accNickName, block.timestamp);

        uniqueId = uniqueId.add(1);
    }else {
        revert("You already have an account created.");
    }
}
1

2 Answers 2

0

Not sure if this is the best way but for me this works,

function deleteAccount() public {
    require(accDetails[msg.sender].creationTimeStamp != 0, "You do not have an account.");
    if(balanceOf[msg.sender] > 0) {
        uint256 _amount = balanceOf[msg.sender];
        balanceOf[msg.sender] = 0;
        payable(msg.sender).transfer(_amount);

        emit withdrawalComplete(msg.sender, accDetails[msg.sender].accNickName, _amount, balanceOf[msg.sender].add(_amount), balanceOf[msg.sender]);
    }
    uint256 _oldUniqueIdentifier = addrTouniqueIdentifier[msg.sender];

    accDetails[msg.sender] = AccDetails("", 0);
    uniqueIdentifierToAddr[addrTouniqueIdentifier[msg.sender]] = address(0);
    addrTouniqueIdentifier[msg.sender] = 0;

    emit accountDeleted(msg.sender, _oldUniqueIdentifier, block.timestamp);
}
0

Based on your code, deleteAccount will look something like this:

function deleteAccount(address _addr) public {
        delete accDetails[_addr];
        delete balanceOf[_addr];
        delete addrTouniqueIdentifier[_addr];
        delete uniqueIdentifierToAddr[/*balance of the deleted acc*/];
    }

You will have to transfer the deleted account's balance to another address:

function send(address payable _to) public payable {
        (bool sent, bytes memory data) = _to.call{value: msg.value}("");
        require(sent, "Failed to send Ether");
    }

Putting everything together:

function send(address payable _to) public payable {
        (bool sent, bytes memory data) = _to.call{value: msg.value}("");
        require(sent, "Failed to send Ether");
    }

    function deleteAccount(address _addr) public {
        send(payable(to));
        // Reset the value to the default value.
        delete accDetails[_addr];
        delete balanceOf[_addr];
        delete addrTouniqueIdentifier[_addr];
        delete uniqueIdentifierToAddr[/*balance of the deleted acc*/];
    }

Also:

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.