Skip to content

Commit

Permalink
updated confusing comments, set ownerOf to a variable in listable to …
Browse files Browse the repository at this point in the history
…prevent calling it twice, added a checker on max quantity, added logic to remove from the recipient array in TransferFee
  • Loading branch information
cblanquera committed Aug 1, 2021
1 parent 058a32e commit 470a3f4
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 29 deletions.
2 changes: 1 addition & 1 deletion contracts/token/ERC721/extensions/ERC721ContractURI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.0;

//abstract implementation of ERC721
//implementation of ERC721
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";


Expand Down
1 change: 0 additions & 1 deletion contracts/token/ERC721/extensions/ERC721Exchangable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pragma solidity ^0.8.0;
//custom abstract implementation of ERC721
import "./ERC721Listable.sol";


abstract contract ERC721Exchangable is ERC721Listable {
/**
* @dev Allows for a sender to avail of the offer price
Expand Down
16 changes: 6 additions & 10 deletions contracts/token/ERC721/extensions/ERC721Listable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

pragma solidity ^0.8.0;

//abstract implementation of ERC721
//used to store an item's metadata (ex. https://game.example/item-id-8u5h2m.json)
//it already has IERC721Metadata and IERC721Enumerable, so no need to add it
//usage: _setTokenURI(tokenId, tokenURI)
//implementation of ERC721
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

abstract contract ERC721Listable is ERC721 {
Expand All @@ -29,31 +26,30 @@ abstract contract ERC721Listable is ERC721 {
* @dev Allows token owners to list their tokens for sale
*/
function list(uint256 tokenId, uint256 amount) public {
//get the owner
address owner = ownerOf(tokenId);
//error if the sender is not the owner
// even the contract owner cannot list a token
require(owner == msg.sender, "Only the token owner can list a token");
require(ownerOf(tokenId) == msg.sender, "Only the token owner can list a token");
//add the listing
listings[tokenId] = amount;
//emit that something was listed
emit Listed(owner, tokenId, amount);
emit Listed(msg.sender, tokenId, amount);
}

/**
* @dev Allows token owners to remove their token sale listing
*/
function delist(uint256 tokenId) public {
address owner = ownerOf(tokenId);
//error if the sender is not the owner
// even the contract owner cannot delist a token
require(
ownerOf(tokenId) == msg.sender,
owner == msg.sender,
"Only the token owner can delist a token"
);
//remove the listing
delete listings[tokenId];
//emit that something was delisted
emit Delisted(ownerOf(tokenId), tokenId);
emit Delisted(owner, tokenId);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions contracts/token/ERC721/extensions/ERC721MaxQuantity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

pragma solidity ^0.8.0;

//abstract implementation of ERC721
//used to store an item's metadata (ex. https://game.example/item-id-8u5h2m.json)
//it already has IERC721Metadata and IERC721Enumerable, so no need to add it
//usage: _setTokenURI(tokenId, tokenURI)
//implementation of ERC721
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

//A simple way to get a counter that can only be incremented or decremented.
Expand All @@ -21,12 +18,13 @@ abstract contract ERC721MaxQuantity is ERC721 {

Counters.Counter private _tokenIds;

uint256 public maxQuantity = 1;
uint256 public maxQuantity;

/**
* @dev Constructor function
*/
constructor (uint256 _maxQuantity) {
require(_maxQuantity > 0, "Max quantity should be greater than 0");
maxQuantity = _maxQuantity;
}

Expand Down
24 changes: 19 additions & 5 deletions contracts/token/ERC721/extensions/ERC721TransferFees.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

pragma solidity ^0.8.0;

//abstract implementation of ERC721
//used to store an item's metadata (ex. https://game.example/item-id-8u5h2m.json)
//it already has IERC721Metadata and IERC721Enumerable, so no need to add it
//usage: _setTokenURI(tokenId, tokenURI)
//implementation of ERC721
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

abstract contract ERC721TransferFees is ERC721 {
Expand All @@ -29,7 +26,7 @@ abstract contract ERC721TransferFees is ERC721 {
);
require(
(totalFees + fee) <= TOTAL_ALLOWABLE_FEES,
'Total fee should be more than 0'
'Exceeds total fees'
);

//if no recipient
Expand All @@ -56,7 +53,24 @@ abstract contract ERC721TransferFees is ERC721 {
*/
function _removeFee(address recipient) internal {
require(fees[recipient] != 0, 'Recipient has no fees');
//deduct total fees
totalFees -= fees[recipient];
//remove fees from the map
delete fees[recipient];
//Tricky logic to remove an element from an array...
//if there are at least 2 elements in the array,
if (_recipients.length > 1) {
//find the recipient
for (uint i = 0; i < _recipients.length; i++) {
if(_recipients[i] == recipient) {
//move the last element to the deleted element
_recipients[i] = _recipients[_recipients.length - 1];
break;
}
}
}

//either way remove the last element
_recipients.pop();
}
}
3 changes: 3 additions & 0 deletions contracts/token/ERC721/presets/ERC721PresetAirDrop.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ contract ERC721PresetAirDrop is ERC721AirDroppable {
ERC721(_name, _symbol)
ERC721AirDroppable(merkleroot)
{}

//no methods or configuration needs to be added for this preset
//minting is disabled for this preset
}
2 changes: 2 additions & 0 deletions contracts/token/ERC721/presets/ERC721PresetContractURI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ contract ERC721PresetContractURI is ERC721, ERC721ContractURI {
ERC721ContractURI(_uri)
{}

//no methods or configuration needs to be added for this preset

/**
* @dev Basic mint and transfer
*/
Expand Down
2 changes: 1 addition & 1 deletion contracts/token/ERC721/presets/ERC721PresetExchangable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract ERC721PresetExchangable is ERC721, ERC721Exchangable {
string memory _symbol
) ERC721(_name, _symbol) {}

//no methods or configuration needs to be added for this mock
//no methods or configuration needs to be added for this preset

/**
* @dev Basic mint and transfer
Expand Down
4 changes: 2 additions & 2 deletions contracts/token/ERC721/presets/ERC721PresetListable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

//custom abstract implementation of ERC721
//used to set secondary transfer fees
//used to allow token owners to list their tokens for sale
import "./../extensions/ERC721Listable.sol";

contract ERC721PresetListable is ERC721, ERC721Listable {
Expand All @@ -30,7 +30,7 @@ contract ERC721PresetListable is ERC721, ERC721Listable {
string memory _symbol
) ERC721(_name, _symbol) {}

//no methods or configuration needs to be added for this mock
//no methods or configuration needs to be added for this preset

/**
* @dev Basic mint and transfer
Expand Down
2 changes: 2 additions & 0 deletions contracts/token/ERC721/presets/ERC721PresetMaxQuantity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ contract ERC721PresetMaxQuantity is ERC721MaxQuantity {
ERC721MaxQuantity(_maxQuantity)
{}

//no methods or configuration needs to be added for this preset

/**
* @dev Mint and transfer considering max quantity
*/
Expand Down
3 changes: 3 additions & 0 deletions contracts/token/ERC721/presets/ERC721PresetPreMint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ contract ERC721PresetPreMint is ERC721PreMintable {
ERC721(_name, _symbol)
ERC721PreMintable(_recipients)
{}

//no methods or configuration needs to be added for this preset
//minting is disabled for this preset
}
7 changes: 5 additions & 2 deletions contracts/token/ERC721/presets/ERC721PresetTransferFees.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ contract ERC721PresetTransferFees is ERC721, ERC721TransferFees {

Counters.Counter private _tokenIds;

//in this mock only the contract owner can add a fee
//in this preset only the contract owner can add a fee
address owner;

modifier onlyContractOwner {
require(msg.sender == owner, 'Restricted method access to only the contract owner');
require(
msg.sender == owner,
'Restricted method access to only the contract owner'
);
_;
}

Expand Down
1 change: 0 additions & 1 deletion contracts/token/ERC721/presets/ERC721PresetVanilla.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import "@openzeppelin/contracts/utils/Counters.sol";
//implementation of ERC721
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";


contract ERC721PresetVanilla is ERC721 {
using Counters for Counters.Counter;

Expand Down
1 change: 0 additions & 1 deletion contracts/token/ERC721/presets/ERC721PresetZeppelin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
//see: https://docs.openzeppelin.com/contracts/4.x/api/token/erc721#ERC721Burnable
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";


contract ERC721PresetZeppelin is ERC721URIStorage, ERC721Burnable {
using Counters for Counters.Counter;

Expand Down

0 comments on commit 470a3f4

Please sign in to comment.