Skip to content

Instantly share code, notes, and snippets.

@wafflemakr
Created March 5, 2021 20:16
Show Gist options
  • Save wafflemakr/3eb0b51d502e88fd3cb0010e05c8714d to your computer and use it in GitHub Desktop.
Save wafflemakr/3eb0b51d502e88fd3cb0010e05c8714d to your computer and use it in GitHub Desktop.
Using Enumerable Set From OZ
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.12;
import "./Enumerable.sol";
contract Test{
using EnumerableSet for EnumerableSet.UintSet;
mapping (address => EnumerableSet.UintSet) private ownedCards;
function addCardToUser(address user, uint cardId) external{
ownedCards[user].add(cardId);
}
function removeCardFromUser(address user, uint cardId) external{
ownedCards[user].remove(cardId);
}
function isCardOwned(address user, uint cardId) external view returns(bool){
return ownedCards[user].contains(cardId);
}
function getAllCards(address user) external view returns(uint[] memory){
uint totalCards = ownedCards[user].length();
uint[] memory cards = new uint[](totalCards);
for(uint i=0; i<totalCards; i++){
cards[i] = ownedCards[user].at(i);
}
return cards;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment