Skip to content

Commit

Permalink
chore(grc20): lazily resolve username
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed Apr 28, 2022
1 parent d7ce03d commit dec1a47
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/gnoland/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ func makeGenesisDoc(pvPub crypto.PubKey) *bft.GenesisDoc {
"p/ufmt",
"p/avl",
"p/grc/grc20",
"p/grc/grc20/impl",
"p/grc/grc721",
"p/maths",
"r/foo20",
"r/users",
"p/grc/grc20/impl",
"r/foo20",
"r/boards",
"r/banktest"} {
// open files in directory as MemPackage.
Expand Down
13 changes: 13 additions & 0 deletions examples/gno.land/p/grc/grc20/impl/impl.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"gno.land/p/avl"
"gno.land/p/grc/grc20"
"gno.land/p/ufmt"
"gno.land/r/users"
)

type Token struct {
Expand Down Expand Up @@ -51,22 +52,32 @@ func (t *Token) GetDecimals() uint { return t.decimals }
func (t *Token) TotalSupply() uint64 { return t.totalSupply }

func (t *Token) BalanceOf(address std.Address) uint64 {
address = users.GetAddressByInput(address.String())
return t.balanceOf(address)
}

func (t *Token) Transfer(owner, to std.Address, amount uint64) {
owner = users.GetAddressByInput(owner.String())
to = users.GetAddressByInput(to.String())
t.transfer(owner, to, amount)
}

func (t *Token) Allowance(owner, spender std.Address) uint64 {
owner = users.GetAddressByInput(owner.String())
spender = users.GetAddressByInput(spender.String())
return t.allowance(owner, spender)
}

func (t *Token) Approve(owner, spender std.Address, amount uint64) {
owner = users.GetAddressByInput(owner.String())
spender = users.GetAddressByInput(spender.String())
t.approve(owner, spender, amount)
}

func (t *Token) TransferFrom(spender, from, to std.Address, amount uint64) {
spender = users.GetAddressByInput(spender.String())
from = users.GetAddressByInput(from.String())
to = users.GetAddressByInput(to.String())
t.spendAllowance(from, spender, amount)
t.transfer(from, to, amount)
}
Expand All @@ -75,10 +86,12 @@ func (t *Token) TransferFrom(spender, from, to std.Address, amount uint64) {
//

func (t *Token) Mint(address std.Address, amount uint64) {
address = users.GetAddressByInput(address.String())
t.mint(address, amount)
}

func (t *Token) Burn(address std.Address, amount uint64) {
address = users.GetAddressByInput(address.String())
t.burn(address, amount)
}

Expand Down
14 changes: 14 additions & 0 deletions examples/gno.land/r/users/users.gno
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,20 @@ func GetUserByAddress(addr std.Address) *User {
return userI.(*User)
}

// GetAddressByInput takes a user input and try to resolve it as a user, or just return it, casted as an address.
// This helper aims to make it easy to integrate r/users with any part of your contract that could ask a user address input.
func GetAddressByInput(input string) std.Address {
// TODO: if input matches an address, return it.

user := GetUserByName(input)
if user != nil {
return user.Address()
}

// fallback, return the address itself
return std.Address(input)
}

//----------------------------------------
// Constants

Expand Down

0 comments on commit dec1a47

Please sign in to comment.