Last active
January 21, 2023 08:34
-
-
Save cod3cow/88be673be2bf24bcac1f69559440ebaa to your computer and use it in GitHub Desktop.
Ethereum ERC20 Smart Contract - Beispiel eines Token, der als eigener Coin funktioniert
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
// Dieser Smart Contract erstellt einen Token mit dem Namen "My Token", | |
// dem Symbol "MYT" und 18 Dezimalstellen. Die Gesamtmenge des Token | |
// wird auf 100000000 festgelegt und der Besitzer des Smart Contracts | |
// wird automatisch als Besitzer des Token gesetzt. | |
// Der Smart Contract enthält auch die Funktionen "transfer", "approve" | |
// und "transferFrom", die es ermöglichen, Token an andere Adressen zu | |
// senden, die Übertragung von Token zu genehmigen und Token von einer | |
// Adresse auf eine andere zu übertragen. | |
pragma solidity ^0.8.0; | |
contract MyToken { | |
// Variablen | |
string public name; | |
string public symbol; | |
uint8 public decimals; | |
uint public totalSupply; | |
mapping(address => uint) public balanceOf; | |
address public owner; | |
// Events | |
event Transfer(address indexed from, address indexed to, uint value); | |
// Konstruktor | |
constructor() public { | |
name = "My Token"; | |
symbol = "MYT"; | |
decimals = 18; | |
totalSupply = 100000000; | |
balanceOf[msg.sender] = totalSupply; | |
owner = msg.sender; | |
} | |
// Funktionen | |
function transfer(address to, uint value) public { | |
require(balanceOf[msg.sender] >= value && value > 0); | |
balanceOf[msg.sender] -= value; | |
balanceOf[to] += value; | |
emit Transfer(msg.sender, to, value); | |
} | |
function approve(address spender, uint value) public returns (bool) { | |
require(spender != address(0)); | |
require(value <= balanceOf[msg.sender]); | |
require(!allowance[msg.sender][spender]); | |
allowance[msg.sender][spender] = value; | |
emit Approval(msg.sender, spender, value); | |
return true; | |
} | |
function transferFrom(address from, address to, uint value) public returns (bool) { | |
require(value <= balanceOf[from]); | |
require(value <= allowance[from][msg.sender]); | |
balanceOf[from] -= value; | |
balanceOf[to] += value; | |
allowance[from][msg.sender] -= value; | |
emit Transfer(from, to, value); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment