Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Update creating-grc20.md #1771

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions docs/how-to-guides/creating-grc20.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,65 +56,79 @@ In this code preview, we have:

The following section will be about introducing Public functions to expose functionality imported from the [grc20 package](https://github.com/gnolang/gno/tree/master/examples/gno.land/p/demo/grc/grc20).

Add this imports :

```go
import (
....
"strings"

"gno.land/p/demo/ufmt"
"gno.land/r/demo/users"

pusers "gno.land/p/demo/users"
)
```

[embedmd]:# (../assets/how-to-guides/creating-grc20/mytoken-2.gno go)
```go
func TotalSupply() uint64 {
return mytoken.TotalSupply()
}

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

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

func Transfer(to users.AddressOrName, amount uint64) {
func Transfer(to pusers.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := mytoken.Transfer(caller, to.Resolve(), amount)
err := mytoken.Transfer(caller, users.Resolve(to), amount)
if err != nil {
panic(err)
}
}

func Approve(spender users.AddressOrName, amount uint64) {
func Approve(spender pusers.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := mytoken.Approve(caller, spender.Resolve(), amount)
err := mytoken.Approve(caller, users.Resolve(spender), amount)
if err != nil {
panic(err)
}
}

func TransferFrom(from, to users.AddressOrName, amount uint64) {
func TransferFrom(from, to pusers.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := mytoken.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
err := mytoken.TransferFrom(caller, users.Resolve(from), users.Resolve(to), amount)
if err != nil {
panic(err)
}
}

func Mint(address users.AddressOrName, amount uint64) {
func Mint(address pusers.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
err := mytoken.Mint(address.Resolve(), amount)
err := mytoken.Mint(users.Resolve(address), amount)
if err != nil {
panic(err)
}
}

func Burn(address users.AddressOrName, amount uint64) {
func Burn(address pusers.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
err := mytoken.Burn(address.Resolve(), amount)
err := mytoken.Burn(users.Resolve(address), amount)
if err != nil {
panic(err)
}
Expand All @@ -128,8 +142,8 @@ func Render(path string) string {
case path == "":
return mytoken.RenderHome()
case c == 2 && parts[0] == "balance":
owner := users.AddressOrName(parts[1])
balance, _ := mytoken.BalanceOf(owner.Resolve())
owner := pusers.AddressOrName(parts[1])
balance, _ := mytoken.BalanceOf(users.Resolve(owner))
return ufmt.Sprintf("%d\n", balance)
default:
return "404\n"
Expand Down
Loading