-
Notifications
You must be signed in to change notification settings - Fork 2
/
berachain.sol
98 lines (81 loc) · 3.96 KB
/
berachain.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// ERC20 standard interface
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract BerachainToken is IERC20 {
string public name = "BerachainToken";
string public symbol = "BRC";
uint8 public decimals = 18;
uint256 private _totalSupply;
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
address public owner;
modifier onlyOwner() {
require(msg.sender == owner, "Only the contract owner can perform this action");
_;
}
constructor(uint256 initialSupply) {
owner = msg.sender; // Contract deployer becomes the owner
_totalSupply = initialSupply * (10 ** uint256(decimals)); // Initial supply with decimals
_balances[owner] = _totalSupply; // Assign the total supply to the owner
emit Transfer(address(0), owner, _totalSupply); // Emit the initial transfer event
}
// Return the total supply of tokens
function totalSupply() public view override returns (uint256) {
return _totalSupply;
}
// Return the balance of a specific account
function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
}
// Transfer tokens to a recipient
function transfer(address recipient, uint256 amount) public override returns (bool) {
require(_balances[msg.sender] >= amount, "Insufficient balance");
_balances[msg.sender] -= amount;
_balances[recipient] += amount;
emit Transfer(msg.sender, recipient, amount);
return true;
}
// Check how much a spender is allowed to spend on behalf of an owner
function allowance(address owner, address spender) public view override returns (uint256) {
return _allowances[owner][spender];
}
// Approve a spender to spend tokens on behalf of the owner
function approve(address spender, uint256 amount) public override returns (bool) {
_allowances[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
// Transfer tokens on behalf of the owner using an approved allowance
function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
require(_balances[sender] >= amount, "Insufficient balance");
require(_allowances[sender][msg.sender] >= amount, "Allowance exceeded");
_balances[sender] -= amount;
_balances[recipient] += amount;
_allowances[sender][msg.sender] -= amount;
emit Transfer(sender, recipient, amount);
return true;
}
// Mint new tokens (only the owner can mint new tokens)
function mint(uint256 amount) public onlyOwner {
_totalSupply += amount * (10 ** uint256(decimals));
_balances[owner] += amount * (10 ** uint256(decimals));
emit Transfer(address(0), owner, amount * (10 ** uint256(decimals)));
}
// Burn tokens (destroy tokens from the owner's balance)
function burn(uint256 amount) public onlyOwner {
require(_balances[owner] >= amount, "Insufficient tokens to burn");
_totalSupply -= amount * (10 ** uint256(decimals));
_balances[owner] -= amount * (10 ** uint256(decimals));
emit Transfer(owner, address(0), amount * (10 ** uint256(decimals)));
}
}