NFT minting and selling

Hi!

This is my first crypto project and I could really use some code reviews. I am trying to build a NFT minting and selling platform for a local artist.

Aside from the base ERC-1155 functionality, I only added:

  • a price mapping to keep track of NFT prices for each id
  • a buy method which i expect users to call from the website (using Metamask)

Before publishing this and going live I wanted to check if this approach is suitable? Are there other best practices for selling NFTs I could follow?

Thank you in advance!

This is my contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Burnable.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";

contract MyContract is ERC1155, Ownable, Pausable, ERC1155Burnable, IERC1155Receiver {
    constructor() ERC1155('http://localhost:3000/') {}
    mapping(uint256 => uint256) public prices;

    function setURI(string memory newuri) public onlyOwner {
        _setURI(newuri);
    }

    function onERC1155Received(
        address operator,
        address from,
        uint256 id,
        uint256 value,
        bytes calldata data
    )
    external
    override
    view
    returns(bytes4)
    {
        return IERC1155Receiver(this).onERC1155Received.selector;
    }

    function onERC1155BatchReceived(
        address operator,
        address from,
        uint256[] calldata ids,
        uint256[] calldata values,
        bytes calldata data
    )
    external
    override
    view
    returns(bytes4)
    {
        return IERC1155Receiver(this).onERC1155BatchReceived.selector;
    }

    function pause() public onlyOwner {
        _pause();
    }

    function unpause() public onlyOwner {
        _unpause();
    }

    function mint(address account, uint256 id, uint256 amount, uint256 price, bytes memory data)
        public
        onlyOwner
    {   
        _mint(account, id, amount, data);
        prices[id] = price;
    }

    function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, uint256 price, bytes memory data)
        public
        onlyOwner
    {
        _mintBatch(to, ids, amounts, data);
        for (uint i=0; i<ids.length; i++) {
            prices[ids[i]] = price;
        }
    }

    function _beforeTokenTransfer(address operator, address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data)
        internal
        whenNotPaused
        override
    {
        super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
    }

    function buy(uint256 id, bytes memory data) external payable {
        require(msg.value >= prices[id], "Insufficient funds");
        this.safeTransferFrom(address(this), msg.sender, id, 1, data);
    }
}
1 Like