A simple BEP-20 token
Solidity code
This contract represents a basic implementation of a BEP-20 token, which is a widely used standard for fungible tokens.
pragma solidity ^0.8.22; // Should be same as the version set in remix
contract Token {
mapping(address => uint) public balances;
mapping(address => mapping(address => uint)) public allowance;
uint public totalSupply = 1000 * 10 ** 18; // Expressed in wei
string public name = "hugo";
string public symbol = "HUGO";
uint public decimals = 18;
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
constructor() {
// Allocating all tokens to contract creator
balances[msg.sender] = totalSupply;
}
function balanceOf(address owner) public view returns(uint) {
return balances[owner];
}
function transfer(address to, uint value) public returns(bool) {
require(balanceOf(msg.sender) >= value, 'Insufficient balance');
balances[to] += value;
balances[msg.sender] -= value;
emit Transfer(msg.sender, to, value);
return true;
}
function approve(address spender, uint value) public returns(bool) {
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(balanceOf(from) >= value, 'Insufficient balance');
require(allowance[from][msg.sender] >= value, 'allownace too low');
balances[to] += value;
balances[from] -= value;
emit Transfer(from, to, value);
return true;
}
}
Explanation
-
Version Pragma:
pragma solidity ^0.8.22;
: This line specifies the version of the Solidity compiler that should be used to compile the contract. The caret (^) symbol indicates that the contract should be compiled using a Solidity version greater than or equal to 0.8.22.
-
Contract State Variables:
mapping(address => uint) public balances;
: This mapping keeps track of the token balances for each Ethereum address. The balance of each address is represented as an unsigned integer.mapping(address => mapping(address => uint)) public allowance;
: This mapping is used to store allowances, allowing certain addresses to spend tokens on behalf of others.uint public totalSupply = 1000 * 10 ** 18;
: The total supply of the token, which is initially set to 1000 tokens. The value is expressed in wei, the smallest unit of Ether, by multiplying 10^18.string public name = "hugo";
: The name of the token.string public symbol = "HUGO";
: The symbol of the token (e.g., ETH for Ether or BTC for Bitcoin).uint public decimals = 18;
: The number of decimal places the token uses.
-
Events:
event Transfer(address indexed from, address indexed to, uint value);
: This event is emitted when tokens are transferred from one address to another.event Approval(address indexed owner, address indexed spender, uint value);
: This event is emitted when an address approves another address to spend tokens on its behalf.
-
Constructor:
- The constructor is executed only once when the contract is deployed. It initializes the balance of the contract creator (msg.sender) with the total supply of tokens.
-
Public Functions:
balanceOf(address owner) public view returns(uint)
: Returns the balance of a specified address.transfer(address to, uint value) public returns(bool)
: Allows an address to transfer a specified amount of tokens to another address. It checks for a sufficient balance and emits a Transfer event.approve(address spender, uint value) public returns(bool)
: Allows an address to approve another address to spend a certain amount of tokens on its behalf. It updates the allowance mapping and emits an Approval event.transferFrom(address from, address to, uint value) public returns(bool)
: Allows an address with an allowance to transfer tokens from the owner’s account to another address. It checks for sufficient balances and allowance, updates the balances, and emits a Transfer event.
Deployment
See the deployed coin at BSC scan
Txn Hash: 0x8131bcd4527f0477874e83b78f3fc4b280341f1b2130c26e547b14eb31d1f878
Potential Issues
- SPDX license identifier not provided
- invalid opcode: opcode 0x5f not defined
- JSON-RPC error when deploying
- If you had added the network to Metamask manually try using one of the Chainlist RPCs
Related
- Event in solidity
- The msg object
- Indexed attribute in solidity event