Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2513888

Browse files
committedOct 17, 2024·
chore: fixup
Signed-off-by: moul <[email protected]>
1 parent e2d414e commit 2513888

File tree

11 files changed

+177
-176
lines changed

11 files changed

+177
-176
lines changed
 

‎examples/gno.land/p/demo/grc/grc20/bankers.gno

-84
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package grc20
2+
3+
// XXX: func Mock(t *Token)

‎examples/gno.land/p/demo/grc/grc20/mock_banker.gno

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package grc20
2+
3+
import (
4+
"std"
5+
)
6+
7+
func PrevRealmTeller(b *Token) GRC20 {
8+
if b == nil {
9+
panic("Token cannot be nil")
10+
}
11+
12+
return &fnTeller{
13+
accountFn: func() std.Address {
14+
caller := std.PrevRealm().Addr()
15+
return caller
16+
},
17+
Token: b,
18+
}
19+
}
20+
21+
func RealmTeller(b *Token) GRC20 {
22+
if b == nil {
23+
panic("Token cannot be nil")
24+
}
25+
26+
return &fnTeller{
27+
accountFn: func() std.Address {
28+
caller := std.CurrentRealm().Addr()
29+
return caller
30+
},
31+
Token: b,
32+
}
33+
}
34+
35+
func ReadonlyTeller(b *Token) GRC20 {
36+
if b == nil {
37+
panic("Token cannot be nil")
38+
}
39+
40+
return &fnTeller{
41+
accountFn: nil,
42+
Token: b,
43+
}
44+
}
45+
46+
func SubAccountTeller(b *Token, slug string) GRC20 {
47+
if b == nil {
48+
panic("Token cannot be nil")
49+
}
50+
51+
caller := std.CurrentRealm().Addr()
52+
account := accountSlugAddr(caller, slug)
53+
54+
return &fnTeller{
55+
accountFn: func() std.Address {
56+
return account
57+
},
58+
Token: b,
59+
}
60+
}
61+
62+
func (ft *fnTeller) Transfer(to std.Address, amount uint64) error {
63+
if ft.accountFn == nil {
64+
return ErrReadonly
65+
}
66+
caller := ft.accountFn()
67+
return ft.Token.ledger.Transfer(caller, to, amount)
68+
}
69+
70+
func (ft *fnTeller) Approve(spender std.Address, amount uint64) error {
71+
if ft.accountFn == nil {
72+
return ErrReadonly
73+
}
74+
caller := ft.accountFn()
75+
return ft.Token.ledger.Approve(caller, spender, amount)
76+
}
77+
78+
func (ft *fnTeller) TransferFrom(owner, to std.Address, amount uint64) error {
79+
if ft.accountFn == nil {
80+
return ErrReadonly
81+
}
82+
spender := ft.accountFn()
83+
return ft.Token.ledger.TransferFrom(owner, spender, to, amount)
84+
}
85+
86+
// accountSlugAddr returns the address derived from the specified address and slug.
87+
func accountSlugAddr(addr std.Address, slug string) std.Address {
88+
// XXX: use a new `std.XXX` call for this.
89+
if slug == "" {
90+
return addr
91+
}
92+
key := addr.String() + "/" + slug
93+
return std.DerivePkgAddr(key) // temporarily using this helper
94+
}

‎examples/gno.land/p/demo/grc/grc20/bank.gno ‎examples/gno.land/p/demo/grc/grc20/token.gno

+56-55
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"gno.land/p/demo/ufmt"
88
)
99

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) {
1414
if name == "" {
1515
panic("name should not be empty")
1616
}
@@ -19,60 +19,60 @@ func NewBank(name, symbol string, decimals uint) (*Bank, *AdminBanker) {
1919
}
2020
// XXX additional checks (length, characters, limits, etc)
2121

22-
bank := &Bank{
22+
ledger := &AdminLedger{}
23+
token := &Token{
2324
name: name,
2425
symbol: symbol,
2526
decimals: decimals,
27+
ledger: ledger,
2628
}
27-
adm := &AdminBanker{}
28-
bank.adm = adm
29-
return bank, adm
29+
return token, ledger
3030
}
3131

3232
// 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 }
3434

3535
// 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 }
3737

3838
// 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 }
4040

4141
// 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 }
4343

4444
// 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() }
4646

4747
// 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)
5050
}
5151

5252
// 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)
5555
}
5656

57-
func (b *Bank) RenderHome() string {
57+
func (tok *Token) RenderHome() string {
5858
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())
6363
return str
6464
}
6565

6666
// 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 {
6868
if !owner.IsValid() {
6969
return ErrInvalidAddress
7070
}
7171
if !spender.IsValid() {
7272
return ErrInvalidAddress
7373
}
7474

75-
currentAllowance := b.allowance(owner, spender)
75+
currentAllowance := led.allowance(owner, spender)
7676
if currentAllowance < amount {
7777
return ErrInsufficientAllowance
7878
}
@@ -81,16 +81,16 @@ func (b *AdminBanker) SpendAllowance(owner, spender std.Address, amount uint64)
8181
newAllowance := currentAllowance - amount
8282

8383
if newAllowance == 0 {
84-
b.allowances.Remove(key)
84+
led.allowances.Remove(key)
8585
} else {
86-
b.allowances.Set(key, newAllowance)
86+
led.allowances.Set(key, newAllowance)
8787
}
8888

8989
return nil
9090
}
9191

9292
// 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 {
9494
if !from.IsValid() {
9595
return ErrInvalidAddress
9696
}
@@ -101,18 +101,22 @@ func (b *AdminBanker) Transfer(from, to std.Address, amount uint64) error {
101101
return ErrCannotTransferToSelf
102102
}
103103

104-
toBalance := b.balanceOf(to)
105-
fromBalance := b.balanceOf(from)
104+
var (
105+
toBalance = led.balanceOf(to)
106+
fromBalance = led.balanceOf(from)
107+
)
106108

107109
if fromBalance < amount {
108110
return ErrInsufficientBalance
109111
}
110112

111-
newToBalance := toBalance + amount
112-
newFromBalance := fromBalance - amount
113+
var (
114+
newToBalance = toBalance + amount
115+
newFromBalance = fromBalance - amount
116+
)
113117

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)
116120

117121
std.Emit(
118122
TransferEvent,
@@ -126,27 +130,24 @@ func (b *AdminBanker) Transfer(from, to std.Address, amount uint64) error {
126130

127131
// TransferFrom transfers tokens from the specified owner to the specified to address.
128132
// 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 {
131135
return ErrInsufficientBalance
132136
}
133-
if err := b.SpendAllowance(owner, spender, amount); err != nil {
137+
if err := led.SpendAllowance(owner, spender, amount); err != nil {
134138
return err
135139
}
136140
// 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)
138142
}
139143

140144
// 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() {
146147
return ErrInvalidAddress
147148
}
148149

149-
b.allowances.Set(allowanceKey(owner, spender), amount)
150+
led.allowances.Set(allowanceKey(owner, spender), amount)
150151

151152
std.Emit(
152153
ApprovalEvent,
@@ -159,18 +160,18 @@ func (b *AdminBanker) Approve(owner, spender std.Address, amount uint64) error {
159160
}
160161

161162
// 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 {
163164
if !address.IsValid() {
164165
return ErrInvalidAddress
165166
}
166167

167168
// XXX check for overflow
168169

169-
a.totalSupply += amount
170-
currentBalance := a.balanceOf(address)
170+
led.totalSupply += amount
171+
currentBalance := led.balanceOf(address)
171172
newBalance := currentBalance + amount
172173

173-
a.balances.Set(string(address), newBalance)
174+
led.balances.Set(string(address), newBalance)
174175

175176
std.Emit(
176177
TransferEvent,
@@ -183,21 +184,21 @@ func (a *AdminBanker) Mint(address std.Address, amount uint64) error {
183184
}
184185

185186
// 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 {
187188
if !address.IsValid() {
188189
return ErrInvalidAddress
189190
}
190191
// XXX check for overflow
191192

192-
currentBalance := a.balanceOf(address)
193+
currentBalance := led.balanceOf(address)
193194
if currentBalance < amount {
194195
return ErrInsufficientBalance
195196
}
196197

197-
a.totalSupply -= amount
198+
led.totalSupply -= amount
198199
newBalance := currentBalance - amount
199200

200-
a.balances.Set(string(address), newBalance)
201+
led.balances.Set(string(address), newBalance)
201202

202203
std.Emit(
203204
TransferEvent,
@@ -210,17 +211,17 @@ func (a *AdminBanker) Burn(address std.Address, amount uint64) error {
210211
}
211212

212213
// 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())
215216
if !found {
216217
return 0
217218
}
218219
return balance.(uint64)
219220
}
220221

221222
// 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))
224225
if !found {
225226
return 0
226227
}

‎examples/gno.land/p/demo/grc/grc20/types.gno

+12-22
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414
ErrInvalidAddress = errors.New("invalid address")
1515
ErrCannotTransferToSelf = errors.New("cannot send transfer to self")
1616
ErrReadonly = errors.New("banker is readonly")
17-
ErrRestrictedBankOwner = errors.New("restricted to bank owner")
17+
ErrRestrictedTokenOwner = errors.New("restricted to bank owner")
1818
)
1919

2020
const (
@@ -24,17 +24,17 @@ const (
2424
ApprovalEvent = "Approval"
2525
)
2626

27-
// Bank represents a token's bank, including its name, symbol,
27+
// Token represents a token's bank, including its name, symbol,
2828
// decimals, and administrative functions.
29-
type Bank struct {
29+
type Token struct {
3030
name string
3131
symbol string
3232
decimals uint
33-
adm *AdminBanker
33+
ledger *AdminLedger
3434
}
3535

36-
// AdminBanker is a struct that holds administrative functions for the token.
37-
type AdminBanker struct {
36+
// AdminLedger is a struct that holds administrative functions for the token.
37+
type AdminLedger struct {
3838
totalSupply uint64
3939
balances avl.Tree // std.Address -> uint64
4040
allowances avl.Tree // owner.(std.Address)+":"+spender.(std.Address)) -> uint64
@@ -82,24 +82,14 @@ type GRC20 interface {
8282
TransferFrom(from, to std.Address, amount uint64) error
8383
}
8484

85-
type fnBanker struct {
85+
type fnTeller struct {
8686
accountFn func() std.Address
87-
*Bank
87+
*Token
8888
}
8989

90-
var _ GRC20 = (*fnBanker)(nil)
90+
var _ GRC20 = (*fnTeller)(nil)
9191

92-
// adminBanker is a struct that holds administrative functions for the token.
93-
type adminBanker struct {
94-
bank *Bank
95-
}
96-
97-
// accountSlugAddr returns the address derived from the specified address and slug.
98-
func accountSlugAddr(addr std.Address, slug string) std.Address {
99-
// XXX: use a new `std.XXX` call for this.
100-
if slug == "" {
101-
return addr
102-
}
103-
key := addr.String() + "/" + slug
104-
return std.DerivePkgAddr(key) // temporarily using this helper
92+
// ledger is a struct that holds administrative functions for the token.
93+
type ledger struct {
94+
token *Token
10595
}

‎examples/gno.land/r/demo/atomicswap/atomicswap.gno

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewCustomCoinSwap(recipient std.Address, hashlock string, timelock time.Tim
5858

5959
// NewCustomGRC20Swap creates a new atomic swap contract for grc20 tokens.
6060
// It is not callable with `gnokey maketx call`, but can be imported by another contract or `gnokey maketx run`.
61-
func NewCustomGRC20Swap(recipient std.Address, hashlock string, timelock time.Time, bank *grc20.Bank) (int, *Swap) {
61+
func NewCustomGRC20Swap(recipient std.Address, hashlock string, timelock time.Time, bank *grc20.Token) (int, *Swap) {
6262
sender := std.PrevRealm().Addr()
6363
curAddr := std.CurrentRealm().Addr()
6464
// userBanker := grc20.PrevRealmBanker(bank)

‎examples/gno.land/r/demo/grc20reg/grc20reg.gno

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"gno.land/p/demo/ufmt"
1010
)
1111

12-
var registry = avl.NewTree() // rlmPath[.slug] -> *grc20.Bank
12+
var registry = avl.NewTree() // rlmPath[.slug] -> *grc20.Token
1313

14-
func Register(bank *grc20.Bank, slug string) {
14+
func Register(bank *grc20.Token, slug string) {
1515
rlmPath := std.PrevRealm().PkgPath()
1616
key := fqname.Construct(rlmPath, slug)
1717
registry.Set(key, bank)
@@ -22,15 +22,15 @@ func Register(bank *grc20.Bank, slug string) {
2222
)
2323
}
2424

25-
func Get(key string) *grc20.Bank {
25+
func Get(key string) *grc20.Token {
2626
bank, ok := registry.Get(key)
2727
if !ok {
2828
return nil
2929
}
30-
return bank.(*grc20.Bank)
30+
return bank.(*grc20.Token)
3131
}
3232

33-
func MustGet(key string) *grc20.Bank {
33+
func MustGet(key string) *grc20.Token {
3434
bank := Get(key)
3535
if bank == nil {
3636
panic("unknown bank: " + key)
@@ -46,7 +46,7 @@ func Render(path string) string {
4646
count := 0
4747
registry.Iterate("", "", func(key string, bankI interface{}) bool {
4848
count++
49-
bank := bankI.(*grc20.Bank)
49+
bank := bankI.(*grc20.Token)
5050
rlmPath, slug := fqname.Parse(key)
5151
rlmLink := fqname.RenderLink(rlmPath, slug)
5252
infoLink := "/r/demo/grc20reg:" + key

‎examples/gno.land/r/demo/minidex/minidex.gno

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
// Order represents an order in the DEX.
1414
type Order struct {
1515
trader std.Address
16-
tokenFrom *grc20.Bank
17-
tokenTo *grc20.Bank
16+
tokenFrom *grc20.Token
17+
tokenTo *grc20.Token
1818
amount uint64
1919
isBuy bool
2020
}
@@ -34,7 +34,7 @@ func NewDEX() *DEX {
3434

3535
// PlaceOrder places a new order and matches orders.
3636
// Returns the amount of matched orders, if any.
37-
func (dex *DEX) PlaceOrder(tokenFrom, tokenTo *grc20.Bank, amount uint64, isBuy bool) int {
37+
func (dex *DEX) PlaceOrder(tokenFrom, tokenTo *grc20.Token, amount uint64, isBuy bool) int {
3838
trader := std.PrevRealm().Addr()
3939
curAddr := std.CurrentRealm().Addr()
4040
userBanker := grc20.AccountBanker(tokenFrom, "")
@@ -73,7 +73,7 @@ func (dex *DEX) PlaceOrder(tokenFrom, tokenTo *grc20.Bank, amount uint64, isBuy
7373
}
7474

7575
// matchPairOrders matches orders for a given pair.
76-
func (dex *DEX) matchPairOrders(tokenFrom, tokenTo *grc20.Bank) int {
76+
func (dex *DEX) matchPairOrders(tokenFrom, tokenTo *grc20.Token) int {
7777
pair := createPairString(tokenFrom, tokenTo)
7878
ordersTree, ok := dex.pairs.Get(pair)
7979
if !ok {
@@ -184,7 +184,7 @@ func (o Order) String() string {
184184
}
185185

186186
// createPairString creates a pair string based on two tokens, sorting by the token names to ensure consistency.
187-
func createPairString(token1, token2 *grc20.Bank) string {
187+
func createPairString(token1, token2 *grc20.Token) string {
188188
tokens := []string{token1.GetName(), token2.GetName()}
189189
sort.Strings(tokens)
190190
return tokens[0] + "/" + tokens[1]

0 commit comments

Comments
 (0)
Please sign in to comment.