-
Notifications
You must be signed in to change notification settings - Fork 4
/
erc20.rb
77 lines (57 loc) · 1.96 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
77
class ERC20 < Contract # abstract: true
event :Transfer, from: Address,
to: Address,
amount: UInt
event :Approval, owner: Address,
spender: Address,
amount: UInt
storage name: String,
symbol: String,
decimals: UInt,
totalSupply: UInt,
balanceOf: mapping( Address, UInt),
allowance: mapping( Address, mapping( Address, UInt))
sig [String, String, UInt]
def constructor(name:, symbol:, decimals:)
@name = name
@symbol = symbol
@decimals = decimals
end
sig [Address, UInt], returns: Bool
def approve( spender:, amount: )
@allowance[msg.sender][spender] = amount
log Approval, owner: msg.sender, spender: spender, amount: amount
true
end
sig [Address, UInt], returns: Bool
def transfer( to:, amount: )
assert @balanceOf[msg.sender] >= amount, "Insufficient balance"
@balanceOf[msg.sender] -= amount
@balanceOf[to] += amount
log Transfer, from: msg.sender, to: to, amount: amount
true
end
sig [Address, Address, UInt], returns: Bool
def transferFrom( from:, to:, amount: )
allowed = @allowance[from][msg.sender]
assert @balanceOf[from] >= amount, "Insufficient balance"
assert allowed >= amount, "Insufficient allowance"
@allowance[from][msg.sender] = allowed - amount
@balanceOf[from] -= amount
@balanceOf[to] += amount
log Transfer, from: from, to: to, amount: amount
true
end
sig [Address, UInt]
def _mint( to:, amount: )
@totalSupply += amount
@balanceOf[to] += amount
log Transfer, from: address(0), to: to, amount: amount
end
sig [Address, UInt]
def _burn( from:, amount: )
@balanceOf[from] -= amount
@totalSupply -= amount
log Transfer, from: from, to: address(0), amount: amount
end
end # class ERC20