Skip to content

Instantly share code, notes, and snippets.

@ChanJuiHuang
Created August 20, 2022 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChanJuiHuang/8dbf7eebc0a66a0ed1b8c00ab9f2856f to your computer and use it in GitHub Desktop.
Save ChanJuiHuang/8dbf7eebc0a66a0ed1b8c00ab9f2856f to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract XXXERC721 is ERC721 {
using Counters for Counters.Counter;
uint256 public mintPrice = 0.005 ether;
address private owner;
Counters.Counter private _tokenIds;
event GiveChange(address _to, uint256 _amount, uint256 _balance);
event Withdraw(address _owner, uint256 _amount);
constructor() ERC721("XXXNFT", "XXXNFT") {
owner = msg.sender;
}
// TODO: 買 NFT
function mintMultiple(address _to, uint256 _amount) public payable {
require((mintPrice * _amount) <= msg.value, "Your payment is not enough");
for (uint256 i = 0; i < _amount; i++) {
uint256 tokenId = _tokenIds.current();
safeMint(_to, tokenId);
_tokenIds.increment();
}
uint256 balance = msg.value - (mintPrice * _amount);
if (balance > 0) {
payable(msg.sender).transfer(balance);
emit GiveChange(_to, _amount, balance);
}
}
function safeMint(address _to, uint256 _amount) private {
_safeMint(_to, _amount);
}
// TODO: 轉傳多個 NFT
function transferNft(uint256[] memory _tokens, address _to) public {
for (uint256 i = 0; i < _tokens.length; i++) {
safeTransferFrom(msg.sender, _to, _tokens[i]);
}
}
// 銷毀 Token
function burn(uint256 _tokenId) public {
_burn(_tokenId);
}
// TODO: 退款
function refund(uint256 _tokenId) external {
require(msg.sender == ownerOf(_tokenId), "You are not the owner");
_burn(_tokenId);
payable(msg.sender).transfer(mintPrice);
}
// TODO: 項目方領錢
function withdraw () external {
require(msg.sender == owner, "You are not the owner~XDDD");
uint256 balance = address(this).balance;
payable(msg.sender).transfer(balance);
emit Withdraw(msg.sender, balance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment