How to mapping a struct EnumerableSet.AddressSet / UintSet?
How to mapping a struct EnumerableSet.AddressSet / UintSet?
Loading saved threads...
hellcodediver · External communityPost link
External question — Ethereum Stack Exchange
Author: hellcodediver
Original post: https://ethereum.stackexchange.com/questions/120650
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Hello guys I want to make mapping but got some issues.
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
contract fNftErc20 {
using EnumerableSet for EnumerableSet.AddressSet;
using EnumerableSet for EnumerableSet.UintSet;
struct fCollection{
EnumerableSet.AddressSet nftAddress;
EnumerableSet.UintSet nftIds;
bool mintable;
}
mapping(address => fCollection) private fractionalizationCollections;
function ad(address fnft, address[] calldata nft, uint256[] calldata id) public {
EnumerableSet.AddressSet lockedNft;
EnumerableSet.UintSet lockedId;
for(uint256 i = 0; i<nft.length; i++){
lockedId.add(id[i]);
lockedNft.add(nft[i]);
}
fractionalizationCollections[fnft] = fCollection(lockedNft, lockedId, true);
}
}
I want make something like this but dont know how make it true. If someone got idea please share with, thanks so much)))
Quote
Report
MShakeG · External communityPost link
External answer — Ethereum Stack Exchange
Author: MShakeG
Original post: https://ethereum.stackexchange.com/a/148082
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
The errors you are encountering are because you cannot assign a struct containing (nested) mappings directly, and you cannot construct a struct containing (nested) mappings.
Additionally
lockedNft
and
lockedId
cannot be declared locally i.e. in
memory
since
AddressSet
and
UintSet
contain mappings, and so while they can be moved into contract state i.e.
storage
since this would be gas heavy you can instead add the nft address and id onto a given
fCollection
as shown below.
In short, to fix these issues, you can update your contract by removing the direct assignments and constructors for the
fCollection
struct, and instead use a two-step process to update the mappings inside the struct. Here's the updated contract:
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
contract fNftErc20 {
using EnumerableSet for EnumerableSet.AddressSet;
using EnumerableSet for EnumerableSet.UintSet;
struct fCollection {
EnumerableSet.AddressSet nftAddress;
EnumerableSet.UintSet nftIds;
bool mintable;
}
mapping(address => fCollection) private fractionalizationCollections;
function ad(address fnft, address[] calldata nft, uint256[] calldata id) public {
// Check if the fCollection does not exist for the given fnft address
require(
!fractionalizationCollections[fnft].mintable,
"fCollection already exists for the given fnft address"
);
// Initialize the fCollection for the given fnft address
fractionalizationCollections[fnft].mintable = true;
for (uint256 i = 0; i < nft.length; i++) {
fractionalizationCollections[fnft].nftIds.add(id[i]);
fractionalizationCollections[fnft].nftAddress.add(nft[i]);
}
}
}
Quote
Report
Post Reply
Checking account access…