Skip to content

Commit

Permalink
Struct cleanup (#72)
Browse files Browse the repository at this point in the history
* Clean up assignment struct

* Making structs more consistent in the patch implementations
  • Loading branch information
robdoesstuff committed Jan 13, 2024
1 parent 02303ff commit 9a8da1d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
7 changes: 7 additions & 0 deletions src/IPatchworkAssignable.sol
Expand Up @@ -9,6 +9,13 @@ import "./IPatchworkScoped.sol";
@notice Interface for contracts supporting Patchwork assignment
*/
interface IPatchworkAssignable is IPatchworkScoped {

/// Represents an assignment of a token from an external NFT contract to a token in this contract.
struct Assignment {
address tokenAddr; /// The address of the external NFT contract.
uint256 tokenId; /// The ID of the token in the external NFT contract.
}

/**
@notice Assigns a token to another
@param ourTokenId ID of our token
Expand Down
5 changes: 0 additions & 5 deletions src/IPatchworkMultiAssignable.sol
Expand Up @@ -10,11 +10,6 @@ import "./IPatchworkAssignable.sol";
*/
interface IPatchworkMultiAssignable is IPatchworkAssignable {

struct Assignment {
address tokenAddr; /// The address of the external NFT contract.
uint256 tokenId; /// The ID of the token in the external NFT contract.
}

/**
@notice Checks if this fragment is assigned to a target
@param ourTokenId the tokenId of the fragment
Expand Down
7 changes: 4 additions & 3 deletions src/Patchwork1155Patch.sol
Expand Up @@ -11,10 +11,11 @@ import "./IPatchwork1155Patch.sol";
*/
abstract contract Patchwork1155Patch is Patchwork721, IPatchwork1155Patch {

/// @dev A canonical path to an 1155 patched
struct PatchCanonical {
address addr;
uint256 tokenId;
address account;
address addr; // The address of the 1155
uint256 tokenId; // The tokenId of the 1155
address account; // The account for the 1155
}

/// @dev Mapping from token ID to the canonical address of the NFT that this patch is applied to.
Expand Down
6 changes: 0 additions & 6 deletions src/PatchworkFragmentSingle.sol
Expand Up @@ -9,12 +9,6 @@ import "./IPatchworkSingleAssignable.sol";
@dev base implementation of a Single-relation Fragment is IPatchworkSingleAssignable
*/
abstract contract PatchworkFragmentSingle is Patchwork721, IPatchworkSingleAssignable {

/// Represents an assignment of a token from an external NFT contract to a token in this contract.
struct Assignment {
address tokenAddr; /// The address of the external NFT contract.
uint256 tokenId; /// The ID of the token in the external NFT contract.
}

/// A mapping from token IDs in this contract to their assignments.
mapping(uint256 => Assignment) internal _assignments;
Expand Down
25 changes: 14 additions & 11 deletions src/PatchworkPatch.sol
Expand Up @@ -12,11 +12,14 @@ import "./IPatchworkPatch.sol";
*/
abstract contract PatchworkPatch is Patchwork721, IPatchworkPatch {

/// @dev Mapping from token ID to the address of the NFT that this patch is applied to.
mapping(uint256 => address) internal _patchedAddresses;
/// @dev A canonical path to an 721 patched
struct PatchCanonical {
address addr; // The address of the 721
uint256 tokenId; // The tokenId of the 721
}

/// @dev Mapping from token ID to the token ID of the NFT that this patch is applied to.
mapping(uint256 => uint256) internal _patchedTokenIds;
/// @dev Mapping from token ID to the canonical address and tokenId of the NFT that this patch is applied to.
mapping(uint256 => PatchCanonical) internal _patchedAddresses;

/// @dev Mapping of hash of original address + token ID for reverse lookups
mapping(bytes32 => uint256) internal _patchedAddressesRev; // hash of patched addr+tokenid to tokenId
Expand All @@ -35,7 +38,8 @@ abstract contract PatchworkPatch is Patchwork721, IPatchworkPatch {
*/
function ownerOf(uint256 tokenId) public view virtual override(ERC721, IERC721) returns (address) {
// Default is inherited ownership
return IERC721(_patchedAddresses[tokenId]).ownerOf(_patchedTokenIds[tokenId]);
PatchCanonical storage canonical = _patchedAddresses[tokenId];
return IERC721(canonical.addr).ownerOf(canonical.tokenId);
}

/**
Expand All @@ -46,8 +50,7 @@ abstract contract PatchworkPatch is Patchwork721, IPatchworkPatch {
@param withReverse store reverse lookup
*/
function _storePatch(uint256 tokenId, address originalAddress, uint256 originalTokenId, bool withReverse) internal virtual {
_patchedAddresses[tokenId] = originalAddress;
_patchedTokenIds[tokenId] = originalTokenId;
_patchedAddresses[tokenId] = PatchCanonical(originalAddress, originalTokenId);
if (withReverse) {
_patchedAddressesRev[keccak256(abi.encodePacked(originalAddress, originalTokenId))] = tokenId;
}
Expand All @@ -64,7 +67,7 @@ abstract contract PatchworkPatch is Patchwork721, IPatchworkPatch {
@dev See {IPatchworkPatch-updateOwnership}
*/
function updateOwnership(uint256 tokenId) public virtual {
address patchedAddr = _patchedAddresses[tokenId];
address patchedAddr = _patchedAddresses[tokenId].addr;
if (patchedAddr != address(0)) {
address owner_ = ownerOf(tokenId);
address curOwner = super.ownerOf(tokenId);
Expand Down Expand Up @@ -102,11 +105,11 @@ abstract contract PatchworkPatch is Patchwork721, IPatchworkPatch {
@dev See {ERC721-_burn}
*/
function _burn(uint256 tokenId) internal virtual override {
address originalAddress = _patchedAddresses[tokenId];
uint256 originalTokenId = _patchedTokenIds[tokenId];
PatchCanonical storage canonical = _patchedAddresses[tokenId];
address originalAddress = canonical.addr;
uint256 originalTokenId = canonical.tokenId;
IPatchworkProtocol(_manager).patchBurned(originalAddress, originalTokenId, address(this));
delete _patchedAddresses[tokenId];
delete _patchedTokenIds[tokenId];
delete _patchedAddressesRev[keccak256(abi.encodePacked(originalAddress, originalTokenId))];
super._burn(tokenId);
}
Expand Down

0 comments on commit 9a8da1d

Please sign in to comment.