Last active
January 10, 2022 14:14
-
-
Save devdattakhoche/47982124a1b88c049d8b905351c1c224 to your computer and use it in GitHub Desktop.
This is general template for creating your own custom erc20 token.
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: MIT | |
pragma solidity >=0.5.0 <0.8.0; | |
interface ERC20{ | |
function balanceOf(address _owner) external view returns (uint256 balance); | |
function transfer(address _to, uint256 _value) external returns (bool success); | |
function transferFrom(address _from, address _to, uint256 _value) external returns (bool success); | |
function approve(address _spender , uint256 _value) external returns (bool success); | |
function allowance(address _owner, address _spender) external view returns (uint256 remaining); | |
event Transfer(address indexed _from, address indexed _to, uint256 _value); | |
event Approval(address indexed _owner, address indexed _spender, uint256 _value); | |
} | |
contract YourCustomToken is ERC20{ | |
uint256 public totalSupply; //Total coins in circulation | |
string public name; //fancy name: eg Dev's Bucks | |
uint8 public decimals; //How many decimals to show. | |
string public symbol; // Symbol : DBK (for Dev's Bucks) | |
mapping (address => uint256) public balances; | |
mapping (address => mapping (address => uint256)) public allowed; | |
constructor(uint256 _initialAmount, string memory _tokenName, uint8 _decimalUnits, string memory _tokenSymbol) { | |
balances[msg.sender] = _initialAmount; // Give the creator all initial tokens | |
totalSupply = _initialAmount; // Update total supply | |
name = _tokenName; // Set the name for display purposes | |
decimals = _decimalUnits; // Amount of decimals for display purposes | |
symbol = _tokenSymbol; // Set the symbol for display purposes | |
} | |
function balanceOf(address _owner) public override view returns (uint256 balance) { | |
return balances[_owner]; | |
} | |
function transfer(address _to, uint256 _value) public override returns (bool success) { | |
require(balances[msg.sender] >= _value, "token balance is lower than the value requested"); | |
balances[msg.sender] = balances[msg.sender] - _value; | |
balances[_to] = balances[_to] + _value; | |
emit Transfer(msg.sender, _to, _value); | |
return true; | |
} | |
function approve(address _spender, uint256 _value) public override returns (bool success) { | |
allowed[msg.sender][_spender] = _value; | |
emit Approval(msg.sender, _spender, _value); //solhint-disable-line indent, no-unused-vars | |
return true; | |
} | |
function allowance(address _owner, address _spender) public override view returns (uint256 remaining) { | |
return allowed[_owner][_spender]; | |
} | |
function transferFrom(address _from, address _to, uint256 _value) public override returns (bool success) { | |
uint256 allowance = allowed[_from][msg.sender]; | |
require(balances[_from] >= _value && allowance >= _value, "token balance or allowance is lower than amount requested"); | |
balances[_to] = balances[_to] + _value; | |
balances[_from] = balances[_to] - _value; | |
allowed[_from][msg.sender] = allowed[_from][msg.sender] - _value; | |
emit Transfer(_from, _to, _value); //solhint-disable-line indent, no-unused-vars | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment