admin管理员组文章数量:1410712
Is there any real world example of nested mappings of solidity? The previous day I was working with mapping functions and I found literally many real world applications for simple mapping but not for nested mappings. Please help enter image description here
Is there any real world example of nested mappings of solidity? The previous day I was working with mapping functions and I found literally many real world applications for simple mapping but not for nested mappings. Please help enter image description here
Share Improve this question asked Mar 18, 2022 at 5:01 Coder and DeveloperCoder and Developer 311 silver badge2 bronze badges3 Answers
Reset to default 4A very mon case is the approval mechanism on ERC-20 and other token standards.
mapping (address => mapping (address => uint256)) approvals;
This structure is used to keep track of token approvals. Example: "Alice (1st address) approves Bob (2nd address) to spend 100 (uint) of her tokens".
A more mon case than "approvals between people" is usually an approval from a person to a DApp. For example: "Alice approves Uniswap to pull 100 USDT from her wallet." And Uniswap is programed to take her USDT only at the moment when she's buying some other tokens against USDT.
It's also used for the same reason in the OpenZeppelin ERC20 implementation.
Yes, today I had an example of that: In a NFT contract I wanted to create a mapping of metadata linked to addresses. So I tried it:
//create a mapping
mapping(string => address) public metadataUri;
//set a mapping
metadataUri[_uri] = _address;
But in that case, if I have one more address with the same string, the first one will override all next addresses with the same string. So in that case I should have something like this:
//create a mapping
mapping(string => mapping(address => uint)) public metadataUri;
//set a mapping
metadataUri[_stringURI][_address] = _uint;
Yes The approval mechanisms in the token contracts use nested mappings. The ERC1155 contracts use nested mappings to record the balances of an address. Line number 24 at https://github./OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol
本文标签: javascriptIs there any real world example of nested mappings of solidityStack Overflow
版权声明:本文标题:javascript - Is there any real world example of nested mappings of solidity? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745036100a2638794.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论