-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathERC20.rb
76 lines (53 loc) · 2.14 KB
/
ERC20.rb
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
pragma :rubidity, "1.0.0"
contract :ERC20, abstract: true do
event :Transfer, { from: :address, to: :address, amount: :uint256 }
event :Approval, { owner: :address, spender: :address, amount: :uint256 }
string :public, :name
string :public, :symbol
uint8 :public, :decimals
uint256 :public, :totalSupply
mapping ({ address: :uint256 }), :public, :balanceOf
mapping ({ address: mapping(address: :uint256) }), :public, :allowance
constructor(name: :string, symbol: :string, decimals: :uint8) {
s.name = name
s.symbol = symbol
s.decimals = decimals
}
function :approve, { spender: :address, amount: :uint256 }, :public, :virtual, returns: :bool do
s.allowance[msg.sender][spender] = amount
emit :Approval, owner: msg.sender, spender: spender, amount: amount
return true
end
function :transfer, { to: :address, amount: :uint256 }, :public, :virtual, returns: :bool do
require(s.balanceOf[msg.sender] >= amount, "Insufficient balance")
s.balanceOf[msg.sender] -= amount
s.balanceOf[to] += amount
emit :Transfer, from: msg.sender, to: to, amount: amount
return true
end
function :transferFrom, {
from: :address,
to: :address,
amount: :uint256
}, :public, :virtual, returns: :bool do
allowed = s.allowance[from][msg.sender]
require(s.balanceOf[from] >= amount, "Insufficient balance")
require(allowed >= amount, "Insufficient allowance")
s.allowance[from][msg.sender] = allowed - amount
s.balanceOf[from] -= amount
s.balanceOf[to] += amount
emit :Transfer, from: from, to: to, amount: amount
return true
end
function :_mint, { to: :address, amount: :uint256 }, :internal, :virtual do
s.totalSupply += amount
s.balanceOf[to] += amount
emit :Transfer, from: address(0), to: to, amount: amount
end
function :_burn, { from: :address, amount: :uint256 }, :internal, :virtual do
require(s.balanceOf[from] >= amount, "Insufficient balance")
s.balanceOf[from] -= amount
s.totalSupply -= amount
emit :Transfer, from: from, to: address(0), amount: amount
end
end