Contract Source Code:
File 1 of 1 : GemToken
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract GemToken {
string public name = "Gem";
string public symbol = "GEM";
uint8 public decimals = 18;
uint256 public totalSupply;
address public owner;
mapping(address => uint256) private balances;
mapping(address => mapping(address => uint256)) private allowances;
// Events
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
// Constructor to set the initial supply and assign ownership
constructor(uint256 initialSupply) {
owner = msg.sender;
totalSupply = initialSupply * (10 ** decimals);
balances[msg.sender] = totalSupply;
emit Transfer(address(0), msg.sender, totalSupply);
}
// Modifier to restrict functions to the owner
modifier onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
// Transfer ownership
function transferOwnership(address newOwner) external onlyOwner {
require(newOwner != address(0), "New owner cannot be the zero address");
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
// Check balance of a specific address
function balanceOf(address account) external view returns (uint256) {
return balances[account];
}
// Transfer tokens to a specific address
function transfer(address to, uint256 amount) external returns (bool) {
require(to != address(0), "Cannot transfer to the zero address");
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
balances[to] += amount;
emit Transfer(msg.sender, to, amount);
return true;
}
// Approve an address to spend tokens on behalf of the caller
function approve(address spender, uint256 amount) external returns (bool) {
require(spender != address(0), "Cannot approve the zero address");
allowances[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
// Check the allowance of a spender for a specific owner
function allowance(address ownerAddress, address spender) external view returns (uint256) {
return allowances[ownerAddress][spender];
}
// Transfer tokens from one address to another using allowance
function transferFrom(address from, address to, uint256 amount) external returns (bool) {
require(to != address(0), "Cannot transfer to the zero address");
require(balances[from] >= amount, "Insufficient balance");
require(allowances[from][msg.sender] >= amount, "Allowance exceeded");
balances[from] -= amount;
allowances[from][msg.sender] -= amount;
balances[to] += amount;
emit Transfer(from, to, amount);
return true;
}
// Mint new tokens (only callable by the owner)
function mint(address to, uint256 amount) external onlyOwner {
require(to != address(0), "Cannot mint to the zero address");
totalSupply += amount;
balances[to] += amount;
emit Transfer(address(0), to, amount);
}
// Burn tokens (only callable by the owner)
function burn(address from, uint256 amount) external onlyOwner {
require(balances[from] >= amount, "Insufficient balance to burn");
totalSupply -= amount;
balances[from] -= amount;
emit Transfer(from, address(0), amount);
}
}