Skip to content

Instantly share code, notes, and snippets.

@JossDuff
Last active October 17, 2021 16:00
Show Gist options
  • Save JossDuff/27feb26e1f095681e4003d7b8509ee6f to your computer and use it in GitHub Desktop.
Save JossDuff/27feb26e1f095681e4003d7b8509ee6f to your computer and use it in GitHub Desktop.
erc20 token jossToken designed to interact with my personal website jossduff.com
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
/// @title
/// @author Joss Duff
/// @notice this is a personal project to set up a token for interacting with
/// and displaying messages on my website jossduff.com. jossToken isn't intended
/// to hold any monetary value. This is for fun.
contract JossToken is ERC20 {
constructor() ERC20("JossToken", "JOSS") public {
_mint(msg.sender, 1000000e18);
}
/// each message is an object with a address as the author and the actual message text
struct Message {
address author;
string text;
}
/// event for front end to update and display a newly made message
/// @param author is the address of the wallet writing the message (the wallet
/// calling makeMessage)
/// @param text is the actual text of the message
event MakeMessage(address author, string text);
// array of message objects
Message[] public messages;
//creates message object if the user has 1 jossToken in their wallet.
///@param _newMessage is the text to be displayed
function makeMessage(string memory _newMessage) public {
// burns a users jossToken from their wallet. Cannot create a message if the
// user doesn't have a jossToken
_burn(msg.sender, 1e18);
messages.push(Message(msg.sender, _newMessage)); //creates a message and adds it to message array
emit MakeMessage(msg.sender, _newMessage); //updates the front end to display the newly created message
}
///@dev returns the address and text of the message at the given index
///@param _messageIndex is the index of the message
function getMessages(uint256 _messageIndex) public view returns(address, string memory){
return (messages[_messageIndex].author, messages[_messageIndex].text);
}
//when the user presses a button the the frontend, they are given one jossToken for free. each address can only receive ONE free joss
//This results in a potentially infinite supply, which is fine because jossToken isn't intended to have value. It is just a test project
//keeps track of which addresses have used the freeJoss function yet
mapping(address => bool) private isUsed; //bool starts as false by default
///@dev mints the caller 1 jossToken if the caller has not yet used this function. Then updates the isUsed mapping to indicate that the caller has received their free jossToken
function freeJoss() public {
require(isUsed[msg.sender] == false); //makes sure the address hasn't used this function before
_mint(msg.sender, 1e18); //mints a new jossToken to callers address
isUsed[msg.sender] = true; //updates the mapping to indicate that the caller has received their free jossToken
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment