Skip to content

Instantly share code, notes, and snippets.

@Khanisic
Created August 14, 2022 17:53
Show Gist options
  • Save Khanisic/2a1dda0b801dabcb89eecb6f7d7f01b6 to your computer and use it in GitHub Desktop.
Save Khanisic/2a1dda0b801dabcb89eecb6f7d7f01b6 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.16+commit.07a7930e.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 );
_collectionCount.increment();
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 );
}
// get tokens minted to my charity
// get my tokens
// get all token minted
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment