Skip to content

Commit

Permalink
feat(grc20,foo20): lazily resolve username in grc20 (and foo20) (#145)
Browse files Browse the repository at this point in the history
* chore(grc20): lazily resolve username

Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>

* chore: function renaming

Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>

* chore: switch to string instead of std.Address for input types on grc20 and foo20

Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>

* initial commit showing direction

* chore: fixup

Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>

* chore: fixup

Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>

Co-authored-by: Jae Kwon <jae@tendermint.com>
  • Loading branch information
moul and jaekwon authored May 9, 2022
1 parent 0df05b7 commit 081442c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 26 deletions.
12 changes: 6 additions & 6 deletions examples/gno.land/p/grc/grc20/impl/impl.gno
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (t *Token) GetSymbol() string { return t.symbol }
func (t *Token) GetDecimals() uint { return t.decimals }
func (t *Token) TotalSupply() uint64 { return t.totalSupply }

func (t *Token) BalanceOf(address std.Address) uint64 {
return t.balanceOf(address)
func (t *Token) BalanceOf(owner std.Address) uint64 {
return t.balanceOf(owner)
}

func (t *Token) Transfer(owner, to std.Address, amount uint64) {
Expand All @@ -74,12 +74,12 @@ func (t *Token) TransferFrom(spender, from, to std.Address, amount uint64) {
// Administration helpers implementation.
//

func (t *Token) Mint(address std.Address, amount uint64) {
t.mint(address, amount)
func (t *Token) Mint(to std.Address, amount uint64) {
t.mint(to, amount)
}

func (t *Token) Burn(address std.Address, amount uint64) {
t.burn(address, amount)
func (t *Token) Burn(from std.Address, amount uint64) {
t.burn(from, amount)
}

// private helpers
Expand Down
43 changes: 26 additions & 17 deletions examples/gno.land/r/foo20/foo.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,64 @@ import (

grc20 "gno.land/p/grc/grc20/impl"
"gno.land/p/ufmt"
"gno.land/r/users"
)

var foo *grc20.Token
var admin std.Address = "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj" // TODO: helper to change admin

func init() {
foo = grc20.NewToken("Foo", "FOO", 4)
foo.Mint("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj", 1000000*10000) // @administrator (1M)
foo.Mint("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq", 10000*10000) // @manfred (10k)
foo.Mint(admin, 1000000*10000) // @administrator (1M)
foo.Mint("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq", 10000*10000) // @manfred (10k)
}

// method proxies as public functions.
//

// getters.

func TotalSupply() uint64 { return foo.TotalSupply() }
func BalanceOf(address std.Address) uint64 { return foo.BalanceOf(address) }
func Allowance(owner, spender std.Address) uint64 { return foo.Allowance(owner, spender) }
func TotalSupply() uint64 {
return foo.TotalSupply()
}

func BalanceOf(owner users.AddressOrName) uint64 {
return foo.BalanceOf(owner.Resolve())
}

func Allowance(owner, spender users.AddressOrName) uint64 {
return foo.Allowance(owner.Resolve(), spender.Resolve())
}

// setters.

func Transfer(to std.Address, amount uint64) {
func Transfer(to users.AddressOrName, amount uint64) {
caller := std.GetCallerAt(2)
foo.Transfer(caller, to, amount)
foo.Transfer(caller, to.Resolve(), amount)
}

func Approve(spender std.Address, amount uint64) {
func Approve(spender users.AddressOrName, amount uint64) {
caller := std.GetCallerAt(2)
foo.Approve(caller, spender, amount)
foo.Approve(caller, spender.Resolve(), amount)
}

func TransferFrom(from, to std.Address, amount uint64) {
func TransferFrom(from, to users.AddressOrName, amount uint64) {
caller := std.GetCallerAt(2)
foo.TransferFrom(caller, from, to, amount)
foo.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
}

// administration.

func Mint(address std.Address, amount uint64) {
func Mint(address users.AddressOrName, amount uint64) {
caller := std.GetCallerAt(2)
assertIsAdmin(caller)
foo.Mint(address, amount)
foo.Mint(address.Resolve(), amount)
}

func Burn(address std.Address, amount uint64) {
func Burn(address users.AddressOrName, amount uint64) {
caller := std.GetCallerAt(2)
assertIsAdmin(caller)
foo.Burn(address, amount)
foo.Burn(address.Resolve(), amount)
}

// render.
Expand All @@ -68,8 +77,8 @@ func Render(path string) string {
case path == "":
return foo.RenderHome()
case c == 2 && parts[0] == "balance":
addr := std.Address(parts[1])
balance := foo.BalanceOf(addr)
owner := users.AddressOrName(parts[1])
balance := foo.BalanceOf(owner.Resolve())
return ufmt.Sprintf("%d\n", balance)
default:
return "404\n"
Expand Down
26 changes: 26 additions & 0 deletions examples/gno.land/r/users/types.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package users

import "std"

type AddressOrName string

func (aon AddressOrName) IsName() bool {
return aon != "" && aon[0] == '@'
}

func (aon AddressOrName) GetName() (string, bool) {
if len(aon) >= 2 && aon[0] == '@' {
return string(aon[1:]), true
}
return "", false
}

func (aon AddressOrName) Resolve() std.Address {
name, isName := aon.GetName()
if isName {
user := GetUserByName(name)
return user.address
} else {
return std.Address(aon) // TODO check validity
}
}
9 changes: 9 additions & 0 deletions examples/gno.land/r/users/users.gno
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ func GetUserByAddress(addr std.Address) *User {
return userI.(*User)
}

// unlike GetUserByName, input must be "@" prefixed for names.
func GetUserByAddressOrName(input AddressOrName) *User {
name, isName := input.GetName()
if isName {
return GetUserByName(name)
}
return GetUserByAddress(std.Address(input))
}

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

Expand Down
7 changes: 4 additions & 3 deletions tests/files2/zrealm_foo200.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"std"

"gno.land/r/foo20"
"gno.land/r/users"
)

var admin = std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
var manfred = std.Address("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq")
var unknown = std.Address("g1u0000000000000000000000000000000000000")
var admin = users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
var manfred = users.AddressOrName("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq")
var unknown = users.AddressOrName("g1u0000000000000000000000000000000000000")

func main() {
// testing read-only public methods
Expand Down

0 comments on commit 081442c

Please sign in to comment.