@@ -7,10 +7,10 @@ import (
7
7
"gno.land/p/demo/ufmt"
8
8
)
9
9
10
- // NewBank creates a new Bank .
11
- // It returns a pointer to the Bank and a pointer to the AdminBanker .
12
- // Expected usage: Bank , admin := NewBank ("Dummy", "DUMMY", 4)
13
- func NewBank (name , symbol string , decimals uint ) (* Bank , * AdminBanker ) {
10
+ // NewToken creates a new Token .
11
+ // It returns a pointer to the Token and a pointer to the Ledger .
12
+ // Expected usage: Token , admin := NewToken ("Dummy", "DUMMY", 4)
13
+ func NewToken (name , symbol string , decimals uint ) (* Token , * AdminLedger ) {
14
14
if name == "" {
15
15
panic ("name should not be empty" )
16
16
}
@@ -19,60 +19,60 @@ func NewBank(name, symbol string, decimals uint) (*Bank, *AdminBanker) {
19
19
}
20
20
// XXX additional checks (length, characters, limits, etc)
21
21
22
- bank := & Bank {
22
+ ledger := & AdminLedger {}
23
+ token := & Token {
23
24
name : name ,
24
25
symbol : symbol ,
25
26
decimals : decimals ,
27
+ ledger : ledger ,
26
28
}
27
- adm := & AdminBanker {}
28
- bank .adm = adm
29
- return bank , adm
29
+ return token , ledger
30
30
}
31
31
32
32
// GetName returns the name of the token.
33
- func (b Bank ) GetName () string { return b .name }
33
+ func (tok Token ) GetName () string { return tok .name }
34
34
35
35
// GetSymbol returns the symbol of the token.
36
- func (b Bank ) GetSymbol () string { return b .symbol }
36
+ func (tok Token ) GetSymbol () string { return tok .symbol }
37
37
38
38
// GetDecimals returns the number of decimals used to get the token's precision.
39
- func (b Bank ) GetDecimals () uint { return b .decimals }
39
+ func (tok Token ) GetDecimals () uint { return tok .decimals }
40
40
41
41
// TotalSupply returns the total supply of the token.
42
- func (b Bank ) TotalSupply () uint64 { return b . adm .totalSupply }
42
+ func (tok Token ) TotalSupply () uint64 { return tok . ledger .totalSupply }
43
43
44
44
// KnownAccounts returns the number of known accounts in the bank.
45
- func (b Bank ) KnownAccounts () int { return b . adm .balances .Size () }
45
+ func (tok Token ) KnownAccounts () int { return tok . ledger .balances .Size () }
46
46
47
47
// BalanceOf returns the balance of the specified address.
48
- func (b Bank ) BalanceOf (address std.Address ) uint64 {
49
- return b . adm .balanceOf (address )
48
+ func (tok Token ) BalanceOf (address std.Address ) uint64 {
49
+ return tok . ledger .balanceOf (address )
50
50
}
51
51
52
52
// Allowance returns the allowance of the specified owner and spender.
53
- func (b Bank ) Allowance (owner , spender std.Address ) uint64 {
54
- return b . adm .allowance (owner , spender )
53
+ func (tok Token ) Allowance (owner , spender std.Address ) uint64 {
54
+ return tok . ledger .allowance (owner , spender )
55
55
}
56
56
57
- func (b * Bank ) RenderHome () string {
57
+ func (tok * Token ) RenderHome () string {
58
58
str := ""
59
- str += ufmt .Sprintf ("# %s ($%s)\n \n " , b .name , b .symbol )
60
- str += ufmt .Sprintf ("* **Decimals**: %d\n " , b .decimals )
61
- str += ufmt .Sprintf ("* **Total supply**: %d\n " , b . adm .totalSupply )
62
- str += ufmt .Sprintf ("* **Known accounts**: %d\n " , b .KnownAccounts ())
59
+ str += ufmt .Sprintf ("# %s ($%s)\n \n " , tok .name , tok .symbol )
60
+ str += ufmt .Sprintf ("* **Decimals**: %d\n " , tok .decimals )
61
+ str += ufmt .Sprintf ("* **Total supply**: %d\n " , tok . ledger .totalSupply )
62
+ str += ufmt .Sprintf ("* **Known accounts**: %d\n " , tok .KnownAccounts ())
63
63
return str
64
64
}
65
65
66
66
// SpendAllowance decreases the allowance of the specified owner and spender.
67
- func (b * AdminBanker ) SpendAllowance (owner , spender std.Address , amount uint64 ) error {
67
+ func (led * AdminLedger ) SpendAllowance (owner , spender std.Address , amount uint64 ) error {
68
68
if ! owner .IsValid () {
69
69
return ErrInvalidAddress
70
70
}
71
71
if ! spender .IsValid () {
72
72
return ErrInvalidAddress
73
73
}
74
74
75
- currentAllowance := b .allowance (owner , spender )
75
+ currentAllowance := led .allowance (owner , spender )
76
76
if currentAllowance < amount {
77
77
return ErrInsufficientAllowance
78
78
}
@@ -81,16 +81,16 @@ func (b *AdminBanker) SpendAllowance(owner, spender std.Address, amount uint64)
81
81
newAllowance := currentAllowance - amount
82
82
83
83
if newAllowance == 0 {
84
- b .allowances .Remove (key )
84
+ led .allowances .Remove (key )
85
85
} else {
86
- b .allowances .Set (key , newAllowance )
86
+ led .allowances .Set (key , newAllowance )
87
87
}
88
88
89
89
return nil
90
90
}
91
91
92
92
// Transfer transfers tokens from the specified from address to the specified to address.
93
- func (b * AdminBanker ) Transfer (from , to std.Address , amount uint64 ) error {
93
+ func (led * AdminLedger ) Transfer (from , to std.Address , amount uint64 ) error {
94
94
if ! from .IsValid () {
95
95
return ErrInvalidAddress
96
96
}
@@ -101,18 +101,22 @@ func (b *AdminBanker) Transfer(from, to std.Address, amount uint64) error {
101
101
return ErrCannotTransferToSelf
102
102
}
103
103
104
- toBalance := b .balanceOf (to )
105
- fromBalance := b .balanceOf (from )
104
+ var (
105
+ toBalance = led .balanceOf (to )
106
+ fromBalance = led .balanceOf (from )
107
+ )
106
108
107
109
if fromBalance < amount {
108
110
return ErrInsufficientBalance
109
111
}
110
112
111
- newToBalance := toBalance + amount
112
- newFromBalance := fromBalance - amount
113
+ var (
114
+ newToBalance = toBalance + amount
115
+ newFromBalance = fromBalance - amount
116
+ )
113
117
114
- b .balances .Set (string (to ), newToBalance )
115
- b .balances .Set (string (from ), newFromBalance )
118
+ led .balances .Set (string (to ), newToBalance )
119
+ led .balances .Set (string (from ), newFromBalance )
116
120
117
121
std .Emit (
118
122
TransferEvent ,
@@ -126,27 +130,24 @@ func (b *AdminBanker) Transfer(from, to std.Address, amount uint64) error {
126
130
127
131
// TransferFrom transfers tokens from the specified owner to the specified to address.
128
132
// It first checks if the owner has sufficient balance and then decreases the allowance.
129
- func (b * AdminBanker ) TransferFrom (owner , spender , to std.Address , amount uint64 ) error {
130
- if b .balanceOf (owner ) < amount {
133
+ func (led * AdminLedger ) TransferFrom (owner , spender , to std.Address , amount uint64 ) error {
134
+ if led .balanceOf (owner ) < amount {
131
135
return ErrInsufficientBalance
132
136
}
133
- if err := b .SpendAllowance (owner , spender , amount ); err != nil {
137
+ if err := led .SpendAllowance (owner , spender , amount ); err != nil {
134
138
return err
135
139
}
136
140
// XXX: since we don't "panic", we should take care of rollbacking spendAllowance if transfer fails.
137
- return b .Transfer (owner , to , amount )
141
+ return led .Transfer (owner , to , amount )
138
142
}
139
143
140
144
// Approve sets the allowance of the specified owner and spender.
141
- func (b * AdminBanker ) Approve (owner , spender std.Address , amount uint64 ) error {
142
- if ! owner .IsValid () {
143
- return ErrInvalidAddress
144
- }
145
- if ! spender .IsValid () {
145
+ func (led * AdminLedger ) Approve (owner , spender std.Address , amount uint64 ) error {
146
+ if ! owner .IsValid () || ! spender .IsValid () {
146
147
return ErrInvalidAddress
147
148
}
148
149
149
- b .allowances .Set (allowanceKey (owner , spender ), amount )
150
+ led .allowances .Set (allowanceKey (owner , spender ), amount )
150
151
151
152
std .Emit (
152
153
ApprovalEvent ,
@@ -159,18 +160,18 @@ func (b *AdminBanker) Approve(owner, spender std.Address, amount uint64) error {
159
160
}
160
161
161
162
// Mint increases the total supply of the token and adds the specified amount to the specified address.
162
- func (a * AdminBanker ) Mint (address std.Address , amount uint64 ) error {
163
+ func (led * AdminLedger ) Mint (address std.Address , amount uint64 ) error {
163
164
if ! address .IsValid () {
164
165
return ErrInvalidAddress
165
166
}
166
167
167
168
// XXX check for overflow
168
169
169
- a .totalSupply += amount
170
- currentBalance := a .balanceOf (address )
170
+ led .totalSupply += amount
171
+ currentBalance := led .balanceOf (address )
171
172
newBalance := currentBalance + amount
172
173
173
- a .balances .Set (string (address ), newBalance )
174
+ led .balances .Set (string (address ), newBalance )
174
175
175
176
std .Emit (
176
177
TransferEvent ,
@@ -183,21 +184,21 @@ func (a *AdminBanker) Mint(address std.Address, amount uint64) error {
183
184
}
184
185
185
186
// Burn decreases the total supply of the token and subtracts the specified amount from the specified address.
186
- func (a * AdminBanker ) Burn (address std.Address , amount uint64 ) error {
187
+ func (led * AdminLedger ) Burn (address std.Address , amount uint64 ) error {
187
188
if ! address .IsValid () {
188
189
return ErrInvalidAddress
189
190
}
190
191
// XXX check for overflow
191
192
192
- currentBalance := a .balanceOf (address )
193
+ currentBalance := led .balanceOf (address )
193
194
if currentBalance < amount {
194
195
return ErrInsufficientBalance
195
196
}
196
197
197
- a .totalSupply -= amount
198
+ led .totalSupply -= amount
198
199
newBalance := currentBalance - amount
199
200
200
- a .balances .Set (string (address ), newBalance )
201
+ led .balances .Set (string (address ), newBalance )
201
202
202
203
std .Emit (
203
204
TransferEvent ,
@@ -210,17 +211,17 @@ func (a *AdminBanker) Burn(address std.Address, amount uint64) error {
210
211
}
211
212
212
213
// balanceOf returns the balance of the specified address.
213
- func (b AdminBanker ) balanceOf (address std.Address ) uint64 {
214
- balance , found := b .balances .Get (address .String ())
214
+ func (led AdminLedger ) balanceOf (address std.Address ) uint64 {
215
+ balance , found := led .balances .Get (address .String ())
215
216
if ! found {
216
217
return 0
217
218
}
218
219
return balance .(uint64 )
219
220
}
220
221
221
222
// allowance returns the allowance of the specified owner and spender.
222
- func (b AdminBanker ) allowance (owner , spender std.Address ) uint64 {
223
- allowance , found := b .allowances .Get (allowanceKey (owner , spender ))
223
+ func (led AdminLedger ) allowance (owner , spender std.Address ) uint64 {
224
+ allowance , found := led .allowances .Get (allowanceKey (owner , spender ))
224
225
if ! found {
225
226
return 0
226
227
}
0 commit comments