0

I have a exchange type of use case in which the customer needs access to a centralised wallet based on different access methods (eg email, apikeys, actual wallet address). I tried mapping twice to the same struct and remix kicked out an error.

Here is the sample code -

struct ClientWallet {
    uint clientBalance;  
    bool clientAccountExists; 
    bytes32 apiKeyHash;
}



mapping (address => ClientWallet) public ClientWallets; // direct access
mapping (bytes32 => ClientWallet) public ClientWallets; // hash of email

I can code a workaround by using more structs and variables but this seems pointless. I tried mapping bytes32 to address and then to ClientWallet but that failed.

Any ideas about this?

3

You have two variables with the same name (ClientWallets). If you give them different names, your code should compile. But note that the two mappings won't "point" to the same values. E.g. if you do:

mapping(address => ClientWallet) public ownerToWallet;
mapping(bytes32 => ClientWallet) public hashToWallet;

...

hashToWallet[something] = ownerToWallet[msg.sender];

you'll end up with two copies of the same struct. Modifying one won't affect the other.

What you likely want is something like this:


mapping(address => ClientWallet) public wallets;
mapping(bytes32 => address) public emailHashToAddress;

...

// e.g. to access a wallet by email hash
wallets[emailHashToAddress[hash]]
  • I was thinking of this type of construction. That is a good solution. If there is no mapping (ie email not mapped to the actual wallet) would the mapping return null? I had previously coded a bool to account for this in a struct. Therefore if we use a require and no struct would that code as - require(wallets[emailHashToAddress[hash]!=0x)? – Trevor Lee Oakley May 22 at 5:18
  • Solidity doesn't have a null. You'd get the zero value of the struct, which means clientBalance and apiKeyHash are 0s, and clientAccountExists is false. – smarx May 22 at 5:23
  • So you probably want to keep clientAccountExists (unless one of the other fields can't be 0 for an existing client). – smarx May 22 at 5:23
  • I used this - require (emails[hashEmail] != address(0)); – Trevor Lee Oakley May 22 at 11:28

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.