Skip to content

Commit

Permalink
chore: update tori with new grc20 apis
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <norman@samourai.coop>
n0izn0iz committed Dec 17, 2024
1 parent 508e831 commit 58c1011
Showing 2 changed files with 62 additions and 70 deletions.
23 changes: 13 additions & 10 deletions examples/gno.land/r/teritori/tori/messages.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tori

import (
"std"
"strconv"
"strings"

@@ -10,17 +11,19 @@ import (
"gno.land/p/teritori/jsonutil"
)

// TODO: move this file in a generic package to administrate grc20s via daos

type ExecutableMessageMintTori struct {
dao_interfaces.ExecutableMessage

Recipient users.AddressOrName
Recipient std.Address
Amount uint64
}

var _ dao_interfaces.ExecutableMessage = &ExecutableMessageMintTori{}

func (msg ExecutableMessageMintTori) Type() string {
return "gno.land/r/teritori/tori.Mint"
return "gno.land/r/teritori/tori.MintTori"
}

func (msg *ExecutableMessageMintTori) String() string {
@@ -37,13 +40,13 @@ func (msg *ExecutableMessageMintTori) String() string {

func (msg *ExecutableMessageMintTori) FromJSON(ast *json.Node) {
obj := ast.MustObject()
msg.Recipient = jsonutil.MustAddressOrName(obj["recipient"])
msg.Recipient = jsonutil.MustAddress(obj["recipient"])
msg.Amount = jsonutil.MustUint64(obj["amount"])
}

func (msg *ExecutableMessageMintTori) ToJSON() *json.Node {
return json.ObjectNode("", map[string]*json.Node{
"recipient": jsonutil.AddressOrNameNode(msg.Recipient),
"recipient": jsonutil.AddressNode(msg.Recipient),
"amount": jsonutil.Uint64Node(msg.Amount),
})
}
@@ -60,7 +63,7 @@ func NewMintToriHandler() *MintToriHandler {

func (h *MintToriHandler) Execute(imsg dao_interfaces.ExecutableMessage) {
msg := imsg.(*ExecutableMessageMintTori)
Mint(msg.Recipient, msg.Amount)
Mint(users.AddressOrName(msg.Recipient), msg.Amount)
}

func (h MintToriHandler) Type() string {
@@ -81,7 +84,7 @@ type ExecutableMessageBurnTori struct {
var _ dao_interfaces.ExecutableMessage = &ExecutableMessageBurnTori{}

func (msg ExecutableMessageBurnTori) Type() string {
return "gno.land/r/teritori/tori.Burn"
return "gno.land/r/teritori/tori.BurnTori"
}

func (msg *ExecutableMessageBurnTori) String() string {
@@ -135,7 +138,7 @@ func (h *BurnToriHandler) Instantiate() dao_interfaces.ExecutableMessage {
type ExecutableMessageChangeAdmin struct {
dao_interfaces.ExecutableMessage

NewAdmin users.AddressOrName
NewAdmin std.Address
}

var _ dao_interfaces.ExecutableMessage = &ExecutableMessageChangeAdmin{}
@@ -154,12 +157,12 @@ func (msg *ExecutableMessageChangeAdmin) String() string {

func (msg *ExecutableMessageChangeAdmin) FromJSON(ast *json.Node) {
obj := ast.MustObject()
msg.NewAdmin = jsonutil.MustAddressOrName(obj["newAdmin"])
msg.NewAdmin = jsonutil.MustAddress(obj["newAdmin"])
}

func (msg *ExecutableMessageChangeAdmin) ToJSON() *json.Node {
return json.ObjectNode("", map[string]*json.Node{
"newAdmin": jsonutil.AddressOrNameNode(msg.NewAdmin),
"newAdmin": jsonutil.AddressNode(msg.NewAdmin),
})
}

@@ -175,7 +178,7 @@ func NewChangeAdminHandler() *ChangeAdminHandler {

func (h *ChangeAdminHandler) Execute(imsg dao_interfaces.ExecutableMessage) {
msg := imsg.(*ExecutableMessageChangeAdmin)
ChangeAdmin(msg.NewAdmin)
owner.TransferOwnership(msg.NewAdmin)
}

func (h ChangeAdminHandler) Type() string {
109 changes: 49 additions & 60 deletions examples/gno.land/r/teritori/tori/tori.gno
Original file line number Diff line number Diff line change
@@ -1,111 +1,100 @@
// tori is a copy of foo20 that can be administred by a dao
package tori

import (
"std"
"strings"

"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ownable"
"gno.land/p/demo/ufmt"
"gno.land/p/demo/users"
rusers "gno.land/r/demo/users"
pusers "gno.land/p/demo/users"
"gno.land/r/demo/grc20reg"
"gno.land/r/demo/users"
)

var (
tori *grc20.AdminToken
userTori grc20.IGRC20
admin std.Address = std.DerivePkgAddr("gno.land/r/teritori/dao_realm")
Token, privateLedger = grc20.NewToken("Tori", "TORI", 4)
UserTeller = Token.CallerTeller()
owner = ownable.NewWithAddress(std.DerivePkgAddr("gno.land/r/teritori/dao_realm"))
)

func init() {
tori = grc20.NewAdminToken("Tori", "TORI", 6)
userTori = tori.GRC20()
privateLedger.Mint(owner.Owner(), 1_000_000*10_000)
getter := func() *grc20.Token { return Token }
grc20reg.Register(getter, "")
}

// method proxies as public functions.
//

// getters.

func TotalSupply() uint64 {
return tori.TotalSupply()
return UserTeller.TotalSupply()
}

func BalanceOf(owner users.AddressOrName) uint64 {
balance, err := tori.BalanceOf(rusers.Resolve(owner))
if err != nil {
panic(err)
}
return balance
func BalanceOf(owner pusers.AddressOrName) uint64 {
ownerAddr := users.Resolve(owner)
return UserTeller.BalanceOf(ownerAddr)
}

func Allowance(owner, spender users.AddressOrName) uint64 {
allowance, err := tori.Allowance(rusers.Resolve(owner), rusers.Resolve(spender))
if err != nil {
panic(err)
}
return allowance
func Allowance(owner, spender pusers.AddressOrName) uint64 {
ownerAddr := users.Resolve(owner)
spenderAddr := users.Resolve(spender)
return UserTeller.Allowance(ownerAddr, spenderAddr)
}

// setters.

func Transfer(to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
tori.Transfer(caller, rusers.Resolve(to), amount)
func Transfer(to pusers.AddressOrName, amount uint64) {
toAddr := users.Resolve(to)
checkErr(UserTeller.Transfer(toAddr, amount))
}

func Approve(spender users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
tori.Approve(caller, rusers.Resolve(spender), amount)
func Approve(spender pusers.AddressOrName, amount uint64) {
spenderAddr := users.Resolve(spender)
checkErr(UserTeller.Approve(spenderAddr, amount))
}

func TransferFrom(from, to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
tori.TransferFrom(caller, rusers.Resolve(from), rusers.Resolve(to), amount)
func TransferFrom(from, to pusers.AddressOrName, amount uint64) {
fromAddr := users.Resolve(from)
toAddr := users.Resolve(to)
checkErr(UserTeller.TransferFrom(fromAddr, toAddr, amount))
}

// administration.

func ChangeAdmin(newAdmin users.AddressOrName) {
// Faucet is distributing tori tokens without restriction (unsafe).
// For a real token faucet, you should take care of setting limits are asking payment.
func Faucet() {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
admin = rusers.Resolve(newAdmin)
amount := uint64(1_000 * 10_000) // 1k
checkErr(privateLedger.Mint(caller, amount))
}

func Mint(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
tori.Mint(rusers.Resolve(address), amount)
func Mint(to pusers.AddressOrName, amount uint64) {
owner.AssertCallerIsOwner()
toAddr := users.Resolve(to)
checkErr(privateLedger.Mint(toAddr, amount))
}

func Burn(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
tori.Burn(rusers.Resolve(address), amount)
func Burn(from pusers.AddressOrName, amount uint64) {
owner.AssertCallerIsOwner()
fromAddr := users.Resolve(from)
checkErr(privateLedger.Burn(fromAddr, amount))
}

// render.
//

func Render(path string) string {
parts := strings.Split(path, "/")
c := len(parts)

switch {
case path == "":
return tori.RenderHome()

return Token.RenderHome()
case c == 2 && parts[0] == "balance":
owner := users.AddressOrName(parts[1])
balance, _ := tori.BalanceOf(rusers.Resolve(owner))
owner := pusers.AddressOrName(parts[1])
ownerAddr := users.Resolve(owner)
balance := UserTeller.BalanceOf(ownerAddr)
return ufmt.Sprintf("%d\n", balance)

default:
return "404\n"
}
}

func assertIsAdmin(address std.Address) {
if address != admin {
panic("restricted access")
func checkErr(err error) {
if err != nil {
panic(err)
}
}

0 comments on commit 58c1011

Please sign in to comment.