Skip to content

Instantly share code, notes, and snippets.

@JhonatanHern
Created April 14, 2023 16:07
Show Gist options
  • Save JhonatanHern/6874e44179a84358f2f1db19cd973335 to your computer and use it in GitHub Desktop.
Save JhonatanHern/6874e44179a84358f2f1db19cd973335 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/utils/SafeERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";
/// @title Open Edition NFT demo
/// @author Jhonatan Hernández
/// @notice You can use this contract for only the most basic simulation
/// @custom:experimental This is an experimental contract.
contract OpenEdition is ERC1155 {
using SafeERC20 for IERC20;
address admin;
uint256 counter = 1;
struct OESettings {
uint256 limit;
uint256 current;
uint256 price;
address paymentToken;
}
mapping(uint256 => OESettings) public settings;
modifier onlyAdmin() {
require(msg.sender == admin);
_;
}
constructor () ERC1155("https://game.example/api/item/{id}.json") {
admin = msg.sender;
}
/// @notice onlyAdmin: only the contract admin can create the OpenEdition setting.
/// @dev Allows the contract admin to create OpenEdition settings.
/// @param _settings A struct of type OESettings that contains the limit, current, price, and payment token of the OpenEdition NFT to create.
function createOE(OESettings calldata _settings) onlyAdmin external {
settings[counter] = _settings;
counter = counter + 1;
}
/// @dev Allows users to purchase an OpenEdition NFT.
/// @param id The ID of the OpenEdition NFT to purchase.
function buy(uint256 id) external {
OESettings storage nftSettings = settings[id];
require(nftSettings.limit > nftSettings.current, "Limit reached");
IERC20(nftSettings.paymentToken).safeTransferFrom(msg.sender, address(this), nftSettings.price);
_mint(msg.sender, id, 1, "");
nftSettings.current += 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment