Skip to content

Instantly share code, notes, and snippets.

@Khanisic
Created August 2, 2022 21:26
Show Gist options
  • Save Khanisic/c98a8e9c3f8fc3f73e17825507a7a942 to your computer and use it in GitHub Desktop.
Save Khanisic/c98a8e9c3f8fc3f73e17825507a7a942 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.15+commit.e14f2714.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.15;
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract MyNFTCharity is ERC721URIStorage{
// address of the owner
address payable owner;
// pause minting
bool public paused = false;
constructor() ERC721 ("My Charity Tokens", "MCT"){
owner = payable(msg.sender);
}
// Counters from library
using Counters for Counters.Counter;
// to count the number of collections
Counters.Counter private _collectionCount;
// to count the number of tokens in the contract
Counters.Counter private _tokenCount;
// access to tht owner
modifier onlyOwner {
require(msg.sender == owner);
_;
}
// to pause minting of NFTs
function pause(bool _state) public onlyOwner {
paused = _state;
}
// struct of NFT token
struct CharityItem{
uint256 tokenId;
uint collectionId;
address payable owner;
uint256 price;
}
// struct of a collection
struct Collection{
uint256 collectionId;
address payable charityAddress;
uint[] tokensMinted;
}
// event that gets called on minting an NFT
event Donated (
uint indexed collectionId,
uint indexed tokenId,
uint indexed price
);
// maps charity token to its tokenId, collectionId, owner and price
mapping ( uint256 => CharityItem ) public idToCharityToken;
// maps collection to its collectionId, charity address, tokensMinted
mapping ( uint256 => Collection ) public idToCollection;
function createCollection ( address payable _charity ) public {
require ( msg.sender == owner );
uint256 currentCollectionId = _collectionCount.current();
uint[] memory tempArr;
idToCollection[currentCollectionId] = Collection(
currentCollectionId,
_charity,
tempArr
);
}
function mintCharityToken(string memory tokenURI, uint _collectionId, uint _price ) external payable {
require ( msg.value == _price );
_tokenCount.increment();
uint256 newTokenId = _tokenCount.current();
_safeMint(msg.sender, newTokenId);
idToCollection[_collectionId].tokensMinted.push(newTokenId);
_setTokenURI(newTokenId, tokenURI);
idToCharityToken[newTokenId] = CharityItem(
newTokenId,
_collectionId,
payable(msg.sender),
_price
);
// transfers money to charity address
payable(idToCollection[_collectionId].charityAddress).transfer(_price);
emit Donated ( _collectionId, newTokenId, _price );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment