Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
move onlyOwner on minting into canMint/balanceOf logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderatallah committed Oct 16, 2019
1 parent b82b22f commit 52bcaf3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 39 deletions.
31 changes: 12 additions & 19 deletions contracts/MyFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ contract MyFactory is IFactory, Ownable {
address public proxyRegistryAddress;
address public nftAddress;
string constant internal baseMetadataURI = "https://opensea-creatures-api.herokuapp.com/api/";
uint256 constant UINT256_MAX = ~uint256(0);

/**
* Enforce the existence of only 100 items per option/token ID
* Optionally set this to a small integer to enforce limited existence per option/token ID
* (Otherwise rely on sell orders on OpenSea, which can only be made by the factory owner.)
*/
uint256 SUPPLY_PER_TOKEN_ID = 100;
uint256 constant SUPPLY_PER_TOKEN_ID = UINT256_MAX;

/**
* Three different options for minting MyCollectibles (basic, premium, and gold).
Expand All @@ -30,18 +32,6 @@ contract MyFactory is IFactory, Ownable {
uint256 constant NUM_OPTIONS = 3;
mapping (uint256 => uint256) public optionToTokenID;

/**
* @dev Require msg.sender to be the owner proxy or owner.
*/
modifier onlyOwner() {
ProxyRegistry proxyRegistry = ProxyRegistry(proxyRegistryAddress);
require(
owner() == msg.sender || address(proxyRegistry.proxies(owner())) == msg.sender,
"MyFactory#onlyOwner: NOT_OWNER_OR_OWNER_PROXY"
);
_;
}

constructor(address _proxyRegistryAddress, address _nftAddress) public {
proxyRegistryAddress = _proxyRegistryAddress;
nftAddress = _nftAddress;
Expand Down Expand Up @@ -72,7 +62,7 @@ contract MyFactory is IFactory, Ownable {
}

function canMint(uint256 _optionId, uint256 _amount) external view returns (bool) {
return _canMint(Option(_optionId), _amount);
return _canMint(msg.sender, Option(_optionId), _amount);
}

function mint(uint256 _optionId, address _toAddress, uint256 _amount, bytes calldata _data) external {
Expand All @@ -95,8 +85,8 @@ contract MyFactory is IFactory, Ownable {
address _toAddress,
uint256 _amount,
bytes memory _data
) internal onlyOwner {
require(_canMint(_option, _amount), "MyFactory#_mint: CANNOT_MINT_MORE");
) internal {
require(_canMint(msg.sender, _option, _amount), "MyFactory#_mint: CANNOT_MINT_MORE");
uint256 optionId = uint256(_option);
MyCollectible nftContract = MyCollectible(nftAddress);
uint256 id = optionToTokenID[optionId];
Expand All @@ -117,7 +107,9 @@ contract MyFactory is IFactory, Ownable {
address _owner,
uint256 _optionId
) public view returns (uint256) {
if (_owner != owner()) {
ProxyRegistry proxyRegistry = ProxyRegistry(proxyRegistryAddress);
if (owner() != _owner && address(proxyRegistry.proxies(owner())) != _owner) {
// Only the factory owner or owner's proxy can have supply
return 0;
}
uint256 id = optionToTokenID[_optionId];
Expand Down Expand Up @@ -166,10 +158,11 @@ contract MyFactory is IFactory, Ownable {
}

function _canMint(
address _fromAddress,
Option _option,
uint256 _amount
) internal view returns (bool) {
uint256 optionId = uint256(_option);
return _amount > 0 && balanceOf(owner(), optionId) >= _amount;
return _amount > 0 && balanceOf(_fromAddress, optionId) >= _amount;
}
}
22 changes: 2 additions & 20 deletions contracts/MyLootBox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ contract MyLootBox is ILootBox, Ownable, Pausable, ReentrancyGuard, MyFactory {
mapping (uint256 => uint256[]) public classToTokenIds;
mapping (uint256 => bool) public classIsPreminted;
uint256 nonce = 0;
uint256 constant UINT256_MAX = ~uint256(0);
uint256 constant INVERSE_BASIS_POINT = 10000;

/**
Expand Down Expand Up @@ -157,13 +156,13 @@ contract MyLootBox is ILootBox, Ownable, Pausable, ReentrancyGuard, MyFactory {
address _toAddress,
uint256 _amount,
bytes memory /* _data */
) internal onlyOwner whenNotPaused nonReentrant {
) internal whenNotPaused nonReentrant {
// Load settings for this box option
uint256 optionId = uint256(_option);
OptionSettings memory settings = optionToSettings[optionId];

require(settings.quantityPerOpen > 0, "MyLootBox#_mint: OPTION_NOT_ALLOWED");
require(_canMint(_option, _amount), "MyLootBox#_mint: CANNOT_MINT");
require(_canMint(msg.sender, _option, _amount), "MyLootBox#_mint: CANNOT_MINT");

// Iterate over the quantity of boxes specified
for (uint256 i = 0; i < _amount; i++) {
Expand All @@ -179,23 +178,6 @@ contract MyLootBox is ILootBox, Ownable, Pausable, ReentrancyGuard, MyFactory {
emit LootBoxOpened(optionId, _toAddress, _amount, totalMinted);
}

/**
* When _owner is the contract owner, this will return how many
* times a particular Option can still be opened.
* NOTE: called by `canMint`
*/
function balanceOf(
address _owner,
uint256 /* _optionId */
) public view returns (uint256) {
if (_owner != owner()) {
// No one except the contract owner can sell any lootboxes
return 0;
}
// Opens are set via off-chain sell orders, so return a large number
return UINT256_MAX;
}

function withdraw() public onlyOwner {
msg.sender.transfer(address(this).balance);
}
Expand Down

0 comments on commit 52bcaf3

Please sign in to comment.