Skip to content

Commit

Permalink
Merge pull request #15 from ProjectOpenSea/ryan/expose-setTrait
Browse files Browse the repository at this point in the history
Dynamic Traits: expose setTrait and add note to add access role restriction
  • Loading branch information
ryanio authored Oct 18, 2023
2 parents 47f130c + 91b6c8e commit 9b567dc
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Github Actions automatically updated formatting with forge fmt\nf3389931e6f7d25a65c1ba2d055c8a43606b40e5
# Github Actions automatically updated formatting with forge fmt\n3b40d0f5cfd25b67a335ee2c8d7c9660d5bfd7f5
# Github Actions automatically updated formatting with forge fmt\n2d96311b29055c5b9a0b632176ce3b8d78a23a89
# Github Actions automatically updated formatting with forge fmt\ne2d336bbb0331c7716c16fed64f51be6c270ad02
9 changes: 5 additions & 4 deletions src/dynamic-traits/DynamicTraits.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.19;
import {EnumerableSet} from "openzeppelin-contracts/contracts/utils/structs/EnumerableSet.sol";
import {IERC7496} from "./interfaces/IERC7496.sol";

abstract contract DynamicTraits is IERC7496 {
contract DynamicTraits is IERC7496 {
using EnumerableSet for EnumerableSet.Bytes32Set;

/// @notice Thrown when a new trait value is not different from the existing value
Expand Down Expand Up @@ -58,13 +58,14 @@ abstract contract DynamicTraits is IERC7496 {
}

/**
* @notice Set the value of a trait for a given token ID. If newTrait is bytes32(0), sets the zero value hash.
* Reverts if the trait value is the zero value hash.
* @notice Set the value of a trait for a given token ID.
* Reverts if the trait value is unchanged.
* @dev IMPORTANT: Override this method with access role restriction.
* @param tokenId The token ID to set the trait value for
* @param traitKey The trait key to set the value of
* @param newValue The new trait value to set
*/
function _setTrait(uint256 tokenId, bytes32 traitKey, bytes32 newValue) internal {
function setTrait(uint256 tokenId, bytes32 traitKey, bytes32 newValue) public virtual {
bytes32 existingValue = _traits[tokenId][traitKey];

if (existingValue == newValue) {
Expand Down
4 changes: 2 additions & 2 deletions src/dynamic-traits/ERC721DynamicTraits.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ contract ERC721DynamicTraits is DynamicTraits, Ownable, ERC721 {
_traitMetadataURI = "https://example.com";
}

function setTrait(uint256 tokenId, bytes32 traitKey, bytes32 value) external virtual override onlyOwner {
function setTrait(uint256 tokenId, bytes32 traitKey, bytes32 value) public virtual override onlyOwner {
// Revert if the token doesn't exist.
_requireOwned(tokenId);

_setTrait(tokenId, traitKey, value);
DynamicTraits.setTrait(tokenId, traitKey, value);
}

function getTraitValue(uint256 tokenId, bytes32 traitKey)
Expand Down
4 changes: 2 additions & 2 deletions src/dynamic-traits/ERC721OnchainTraits.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ contract ERC721OnchainTraits is OnchainTraits, ERC721 {
_traitMetadataURI = "https://example.com";
}

function setTrait(uint256 tokenId, bytes32 traitKey, bytes32 value) external virtual override onlyOwner {
function setTrait(uint256 tokenId, bytes32 traitKey, bytes32 value) public virtual override onlyOwner {
// Revert if the token doesn't exist.
_requireOwned(tokenId);

_setTrait(tokenId, traitKey, value);
DynamicTraits.setTrait(tokenId, traitKey, value);
}

function getTraitValue(uint256 tokenId, bytes32 traitKey)
Expand Down
4 changes: 2 additions & 2 deletions src/dynamic-traits/OnchainTraits.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ abstract contract OnchainTraits is Ownable, DynamicTraits {
* @param traitKey The trait key to get the value of
* @param newValue The new trait value
*/
function setTrait(uint256 tokenId, bytes32 traitKey, bytes32 newValue) external virtual override {
function setTrait(uint256 tokenId, bytes32 traitKey, bytes32 newValue) public virtual override {
TraitLabelStorage memory labelStorage = traitLabelStorage[traitKey];
StoredTraitLabel storedTraitLabel = labelStorage.storedLabel;
if (!StoredTraitLabelLib.exists(storedTraitLabel)) {
Expand All @@ -122,7 +122,7 @@ abstract contract OnchainTraits is Ownable, DynamicTraits {
if (labelStorage.valuesRequireValidation) {
TraitLabelLib.validateAcceptableValue(StoredTraitLabelLib.load(storedTraitLabel), traitKey, newValue);
}
_setTrait(tokenId, traitKey, newValue);
DynamicTraits.setTrait(tokenId, traitKey, newValue);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/dynamic-traits/interfaces/IERC7496.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {IERC165} from "forge-std/interfaces/IERC165.sol";

interface IERC7496 is IERC165 {
/* Events */
event TraitUpdated(bytes32 indexed traitKey, uint256 indexed tokenId, bytes32 trait);
event TraitUpdatedBulkRange(bytes32 indexed traitKeyPattern, uint256 fromTokenId, uint256 toTokenId);
event TraitUpdatedBulkList(bytes32 indexed traitKeyPattern, uint256[] tokenIds);
event TraitUpdated(bytes32 indexed traitKey, uint256 tokenId, bytes32 trait);
event TraitUpdatedBulkRange(bytes32 indexed traitKey, uint256 fromTokenId, uint256 toTokenId, bytes32 traitValue);
event TraitUpdatedBulkList(bytes32 indexed traitKey, uint256[] tokenIds, bytes32 traitValue);
event TraitMetadataURIUpdated();

/* Getters */
Expand Down
8 changes: 5 additions & 3 deletions test/dynamic-traits/ERC721DynamicTraits.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ contract ERC721DynamicTraitsTest is Test {
ERC721DynamicTraitsMintable token;

/* Events */
event TraitUpdated(bytes32 indexed traitKey, uint256 indexed tokenId, bytes32 value);
event TraitUpdatedBulkConsecutive(bytes32 indexed traitKeyPattern, uint256 fromTokenId, uint256 toTokenId);
event TraitUpdatedBulkList(bytes32 indexed traitKeyPattern, uint256[] tokenIds);
event TraitUpdated(bytes32 indexed traitKey, uint256 tokenId, bytes32 traitValue);
event TraitUpdatedBulkConsecutive(
bytes32 indexed traitKeyPattern, uint256 fromTokenId, uint256 toTokenId, bytes32 traitValue
);
event TraitUpdatedBulkList(bytes32 indexed traitKeyPattern, uint256[] tokenIds, bytes32 traitValue);
event TraitMetadataURIUpdated();

function setUp() public {
Expand Down

0 comments on commit 9b567dc

Please sign in to comment.