Last active
January 1, 2023 08:08
-
-
Save stephenhodgkiss/71e4d2bc16c83de2138728f52b33302b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: Apache-2.0 | |
// Author: Stephen Hodgkiss | |
// https://stephenhodgkiss.com | |
pragma solidity ^0.8.0; | |
/** | |
* @title ERC20 Standard Token | |
* @dev This is a standard ERC20 token with support for custom on-chain data. | |
*/ | |
contract ERC20CustomData { | |
// Token info | |
string public name; | |
string public symbol; | |
uint8 public decimals; | |
uint256 public totalSupply; | |
// Custom on-chain data | |
mapping (address => uint) public balances; | |
mapping (address => mapping (address => uint)) public allowed; | |
mapping (address => mapping (string => string)) public customData; | |
// Events | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
event Approval(address indexed owner, address indexed spender, uint256 value); | |
event CustomDataUpdate(address indexed owner, string key, string value); | |
// Constructor | |
constructor (string memory _name, string memory _symbol, uint8 _decimals, uint256 _totalSupply) { | |
name = _name; | |
symbol = _symbol; | |
decimals = _decimals; | |
totalSupply = _totalSupply; | |
balances[msg.sender] = totalSupply; | |
} | |
/** | |
* @dev Function to transfer tokens | |
* @param _to Address to which tokens are transferred | |
* @param _value Amount of tokens to transfer | |
*/ | |
function transfer(address _to, uint256 _value) public { | |
require(balances[msg.sender] >= _value); | |
require(_to != address(0)); | |
balances[msg.sender] -= _value; | |
balances[_to] += _value; | |
emit Transfer(msg.sender, _to, _value); | |
} | |
/** | |
* @dev Function to approve tokens for transfer | |
* @param _spender Address to which tokens are approved | |
* @param _value Amount of tokens to approve | |
*/ | |
function approve(address _spender, uint256 _value) public { | |
require(_spender != address(0)); | |
allowed[msg.sender][_spender] = _value; | |
emit Approval(msg.sender, _spender, _value); | |
} | |
/** | |
* @dev Function to transfer tokens from one address to another | |
* @param _from Address from which tokens are transferred | |
* @param _to Address to which tokens are transferred | |
* @param _value Amount of tokens to transfer | |
*/ | |
function transferFrom(address _from, address _to, uint256 _value) public { | |
require(_from != address(0)); | |
require(_to != address(0)); | |
require(allowed[_from][msg.sender] >= _value); | |
allowed[_from][msg.sender] -= _value; | |
balances[_from] -= _value; | |
balances[_to] += _value; | |
emit Transfer(_from, _to, _value); | |
} | |
/** | |
* @dev Function to update custom data on the chain | |
* @param _owner Address of the owner of the data | |
* @param _key Key for the data | |
* @param _value Value for the data | |
*/ | |
function updateCustomData(address _owner, string memory _key, string memory _value) public { | |
require(_owner != address(0)); | |
customData[_owner][_key] = _value; | |
emit CustomDataUpdate(_owner, _key, _value); | |
} | |
/** | |
* @dev Function to get custom data from the chain | |
* @param _owner Address of the owner of the data | |
* @param _key Key for the data | |
* @return Value for the data | |
*/ | |
function getCustomData(address _owner, string memory _key) public view returns (string memory) { | |
return customData[_owner][_key]; | |
} | |
/** | |
* @dev Function to get balance of an account | |
* @param _owner Address of the account | |
* @return Balance of the account | |
*/ | |
function getBalance(address _owner) public view returns (uint) { | |
return balances[_owner]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment