0

I've looked through all answers here but still have the question for the latest version of Solidity.
Perhaps, there exist new libraries
Is it possible to optimize this

mapping(address => uint) balances;
mapping(address => bool) staked;

Into just one mapping but with the function to check if an address ever staked at this moment? Using Solidity version 0.7.5

1

Why don't you combine this data into one mapping of structs? Example:

struct sampleStruct {
    uint balance;
    bool staked;
}

mapping(address => sampleStruct) sampleStructs;

Then your method that check if an address ever staked will look like this:

function checkIfStaked(address _address) public view returns(bool) {
    if (sampleStructs[_address].staked) {
        return true;
    } else {
        return false;
    }
}
4
  • 1
    Or simply return sampleStructs[_address].staked;. Dec 19 '20 at 14:47
  • 1
    This works also since staked is a boolean. Dec 19 '20 at 14:51
  • @MiroslavNedelchev If there's no such an address in the mapping yet, will it return 0 or null as long as null doesn't exist in Solidity? Dec 19 '20 at 15:20
  • 1
    @DmitryDyachkov it will return false for not existing addresses in the mapping. Is this what you need? Dec 19 '20 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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