4

I'd like to store a unique mapping from NFT's to some state data in my contract.

NFT's are globally unique based on: the NFT contract address, and the uint ID within the NFT contract.

I would therefore like to do a mapping that equates to:

mapping ((address, uint) => myStateData)

How can this be accomplished?

Thank you!

6

You can combine these two values into one hash like this then use it as the mapping key:

keccak256(<contractAddress>, <nftID>)

Another option is to have mappings of mappings. So:

mapping(address => mapping(uint => myStateData))
  • I really like the first option. Any considerations regarding performance or gas cost between these two choices? Also, is there a chance that two completely different NFT's could have a hash collision using that first option? – Arash Emami Oct 5 '18 at 3:34
  • Gas cost difference should be negligible, the first one may even be more efficient. The chance of collision is not worth worrying about, and not mitigated by the second option anyways – Tjaden Hess Oct 5 '18 at 3:40

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.