Created
July 11, 2024 12:20
-
-
Save tetelie/100af4952eb0a201d16436a1156bbd72 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-Licence-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract Token{ | |
// Déclaration des variables du token | |
string private _name; | |
string private _symbol; | |
uint8 private _decimals; | |
uint256 private _totalSupply; | |
mapping(address => uint256) private _balances; | |
mapping(address => mapping(address => uint256)) private _allowances; | |
// Évènements pour les transferts et les autorisations | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
event Approval(address indexed owner, address indexed spender, uint256 value); | |
// Constructeur du contrat | |
constructor() | |
{ | |
_name = "TokenName"; | |
_symbol = "TokenSymbol"; | |
_decimals = 18; | |
_totalSupply = 1000000 * (10 ** uint256(_decimals)); | |
_balances[msg.sender] = _totalSupply; | |
emit Transfer(address(0), msg.sender, _totalSupply); | |
} | |
// Fonction pour obtenir le nom du token | |
function name() public view returns (string memory) | |
{ | |
return _name; | |
} | |
// Fonction pour obtenir le symbole du token | |
function symbol() public view returns (string memory) | |
{ | |
return _symbol; | |
} | |
// Fonction pour obtenir le nombre de décimales du token | |
function decimals() public view returns(uint8) | |
{ | |
return _decimals; | |
} | |
// Fonction pour obtenir l'approvisionnement total du token | |
function totalSupply() public view returns (uint256) | |
{ | |
return _totalSupply; | |
} | |
// Fonction pour obtenir le solde d'un compte | |
function balanceOf(address account) public view returns (uint256) | |
{ | |
return _balances[account]; | |
} | |
// Fonction pour transférer des tokens à une autre adresse | |
function transfer(address recipient, uint256 amount) public returns (bool) | |
{ | |
_transfer(msg.sender, recipient, amount); | |
return true; | |
} | |
// Fonction pour approuver une adresse à dépenser des tokens | |
function approve (address spender, uint256 amount) public returns (bool) | |
{ | |
_allowances[msg.sender][spender] = amount; | |
emit Approval(msg.sender, spender, amount); | |
return true; | |
} | |
// Fonction pour transférer des tokens depuis un compte à un autre compte | |
function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) | |
{ | |
_transfer(sender,recipient,amount); | |
_approve(sender, msg.sender, _allowances[sender][msg.sender] - amount); | |
return true; | |
} | |
// Fonction interne pour effectuer le transfert de tokens | |
function _transter(address sender, address recipient, uint256 amount) internal | |
{ | |
require(sender != address(0), "Transfer from the zero address"); | |
require(recipient != address(0), "Transfer to the zero adress"); | |
require(_balances[sender] >= amount, "Insifficient balance"); | |
_balances[sender] -= amount; | |
_balances[recipient] += amount; | |
emit Transfer(sender, recipient, amount); | |
} | |
// Fonction interne pour approuver une adresse à dépenser des tokens | |
function _approve(address owner, address spender, uint256 amount) internal | |
{ | |
require(owner != address(0), "ERC20: Approve from the zero address"); | |
require(spender != address(0), "ERC20: Approve to the zero address"); | |
// Mise à jour de l'approbation | |
_allowances[owner][spender] = amount; | |
emit Approval(owner, spender, amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment