[Ethereum] How to map an address with a string array in solidity

arraysmappingsolidity

Goal: I wish to store an array of names with an address in a smart contract. It's more like Key-Value pairs where addresses are paired with a list of type strings.

Can I use mapping to implement this? See below for instance.

mapping(address => strings[]) userAddress2UserNames;

Or, there is a different approach to it?

Experiments I performed

I got no error when I compiled the above code but when I call a method which returns string[] from this mapping I got an error stated below.

TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature.

I'm not much aware of this version but I am assuming I should not do production level implementation with it because may be it's not stable. (Please correct me if I am wrong)

Please suggest me a way to achieve my goal.

Best Answer

How long are the user names? Maybe you can use bytes32 instead of string (each username would be limited up to 32 chars). By doing this, you wouldn't be combining two levels of dynamic arrays, which is not permitted in the current version.

Related Topic