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

refactor: GRC20 #2314

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
68589d3
rm grc20 folder
leohhhn Jun 10, 2024
0e3bcf7
add interface
leohhhn Jun 10, 2024
6513052
add initial implementation
leohhhn Jun 10, 2024
cde3839
add update helper
leohhhn Jun 10, 2024
eefe8a1
implement functions
leohhhn Jun 10, 2024
9f5830c
comments & transfer
leohhhn Jun 10, 2024
283b82c
mint & tests
leohhhn Jun 10, 2024
3cb8085
rename to .gno, fix valid transfer
leohhhn Jun 10, 2024
9ca19cc
add more tests
leohhhn Jun 10, 2024
2344a39
remove leftover
leohhhn Jun 10, 2024
17d2978
play around with pointer tests
leohhhn Jun 10, 2024
6cc7ee2
modify tests
leohhhn Jun 11, 2024
d653cd2
gno mod tidy, fix imports
leohhhn Jun 11, 2024
b98a9ce
add docs to grc20, modify foo20
leohhhn Jun 11, 2024
9780137
fix errors, add faucet management
leohhhn Jun 11, 2024
60d5197
fix rendering
leohhhn Jun 11, 2024
d17c1fc
fix errors & rendering for grc20factory
leohhhn Jun 11, 2024
ae91731
fixing up wugnot
leohhhn Jun 11, 2024
1960279
tests for wugnot
leohhhn Jun 11, 2024
f58010a
add deposit & withdraw tests
leohhhn Jun 11, 2024
0d6514d
fix vault tests
leohhhn Jun 12, 2024
9ac3eda
gno mod tidy
leohhhn Jun 12, 2024
c7a9646
remove old tests
leohhhn Jun 12, 2024
fb7d88b
fix tests
leohhhn Jun 12, 2024
114eed3
fix panic msg
leohhhn Jun 12, 2024
c413fd4
Merge branch 'master' into refac/grc20
leohhhn Jun 12, 2024
b7ee546
Merge branch 'master' into refac/grc20
leohhhn Jun 12, 2024
3e707c8
Merge branch 'master' into refac/grc20
leohhhn Jun 13, 2024
93ddcf8
update eff gno
leohhhn Jun 13, 2024
5204628
update guide & playground link
leohhhn Jun 13, 2024
6437691
Merge branch 'master' into refac/grc20
leohhhn Jun 17, 2024
c48a2ba
Merge branch 'master' into refac/grc20
leohhhn Jun 25, 2024
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
19 changes: 9 additions & 10 deletions docs/assets/how-to-guides/creating-grc20/mytoken-1.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ import (
"strings"

"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ownable"
"gno.land/p/demo/ufmt"
)

var (
mytoken *grc20.AdminToken
admin std.Address
mytoken *grc20.Token // main token object
o *ownable.Ownable // ownable to store admin of token
)

// init is called once at time of deployment
func init() {
// Set deployer of Realm to admin
admin = std.PrevRealm().Addr()
// Instantiate ownable
o = ownable.New()

// Set token name, symbol and number of decimals
mytoken = grc20.NewAdminToken("My Token", "TKN", 4)
// Instantiate token
mytoken = grc20.NewGRC20Token("My Token", "MYT", 4)

// Mint 1 million tokens to admin
mytoken.Mint(admin, 1000000*10000)
// Mint 1M tokens to admin
mytoken.Mint(o.Owner(), uint64(1_000_000*10000))
}

111 changes: 43 additions & 68 deletions docs/assets/how-to-guides/creating-grc20/mytoken-2.gno
Original file line number Diff line number Diff line change
@@ -1,109 +1,84 @@
// TotalSupply returns the total supply of mytoken
// Name returns the name of the token
func Name() string {
return mytoken.Name()
}

// Symbol returns the name of the token
func Symbol() string {
return mytoken.Symbol()
}

// TotalSupply returns the total supply of the token
func TotalSupply() uint64 {
return mytoken.TotalSupply()
}

// Decimals returns the number of decimals of mytoken
func Decimals() uint {
return mytoken.GetDecimals()
// Decimals returns the decimals of the token
func Decimals() uint8 {
return mytoken.Decimals()
}

// BalanceOf returns the balance mytoken for `account`
func BalanceOf(account std.Address) uint64 {
balance, err := mytoken.BalanceOf(account)
if err != nil {
panic(err)
}

return balance
// BalanceOf returns the balance of a specific username or address
func BalanceOf(owner std.Address) uint64 {
return mytoken.BalanceOf(owner)
}

// Allowance returns the allowance of spender on owner's balance
// Allowance returns the allowance of a spender for the owner's tokens
func Allowance(owner, spender std.Address) uint64 {
allowance, err := mytoken.Allowance(owner, spender)
if err != nil {
panic(err)
}

return allowance
return mytoken.Allowance(owner, spender)
}

// Transfer transfers amount from caller to recipient
func Transfer(recipient std.Address, amount uint64) {
caller := std.PrevRealm().Addr()
if err := mytoken.Transfer(caller, recipient, amount); err != nil {
panic(err)
}
// Setters

// Transfer transfers `value` amount of tokens to address `to`, and MUST fire the Transfer event
// The function SHOULD throw if the message caller’s account balance does not have enough tokens to spend
func Transfer(to std.Address, amount uint64) {
mytoken.Transfer(to, amount)
}

// Approve approves amount of caller's tokens to be spent by spender
// Approve allows `spender` to withdraw from your account multiple times, up to the `value` amount
// If this function is called again it overwrites the current allowance with value
func Approve(spender std.Address, amount uint64) {
caller := std.PrevRealm().Addr()
if err := mytoken.Approve(caller, spender, amount); err != nil {
panic(err)
}
mytoken.Approve(spender, amount)
}

// TransferFrom transfers `amount` of tokens from `from` to `to`
// TransferFrom transfers `value` amount of tokens from address `from` to address `to`, and MUST fire the Transfer event
func TransferFrom(from, to std.Address, amount uint64) {
caller := std.PrevRealm().Addr()

if amount <= 0 {
panic("transfer amount must be greater than zero")
}

if err := mytoken.TransferFrom(caller, from, to, amount); err != nil {
panic(err)
}
mytoken.TransferFrom(from, to, amount)
}

// Mint mints amount of tokens to address. Callable only by admin of token
func Mint(address std.Address, amount uint64) {
assertIsAdmin(std.PrevRealm().Addr())
// Administration

if amount <= 0 {
panic("mint amount must be greater than zero")
// Mint mints `amount` of tokens to `address`, only callable by owner
func Mint(address std.Address, amount uint64) {
if err := o.CallerIsOwner(); err != nil {
panic("only owner can mint tokens")
}

if err := mytoken.Mint(address, amount); err != nil {
panic(err)
}
mytoken.Mint(address, amount)
}

// Burn burns amount of tokens from address. Callable only by admin of token
// Burn burns `amount` of tokens from `address`, only callable by owner
func Burn(address std.Address, amount uint64) {
assertIsAdmin(std.PrevRealm().Addr())

if amount <= 0 {
panic("burn amount must be greater than zero")
if err := o.CallerIsOwner(); err != nil {
panic("only owner can burn tokens")
}

if err := mytoken.Burn(address, amount); err != nil {
panic(err)
}
mytoken.Burn(address, amount)
}

// assertIsAdmin asserts the address is the admin of token
func assertIsAdmin(address std.Address) {
if address != admin {
panic("restricted access")
}
}
// Rendering

// Render renders the state of the realm
func Render(path string) string {
parts := strings.Split(path, "/")
c := len(parts)

switch {
case path == "":
// Default GRC20 render
return mytoken.RenderHome()
case c == 2 && parts[0] == "balance":
// Render balance of specific address
case c == 2 && parts[0] == "balance": // pkgpath:balance/address
owner := std.Address(parts[1])
balance, _ := mytoken.BalanceOf(owner)
return ufmt.Sprintf("%d\n", balance)
return ufmt.Sprintf("%d\n", mytoken.BalanceOf(owner))
default:
return "404\n"
}
Expand Down
5 changes: 2 additions & 3 deletions docs/concepts/effective-gno.md
Original file line number Diff line number Diff line change
Expand Up @@ -701,12 +701,11 @@ best of both worlds, you can wrap a Coins into a GRC20 compatible token.
```go
import "gno.land/p/demo/grc/grc20"

var fooToken grc20.AdminToken = grc20.NewAdminToken("Foo Token", "FOO", 4)
var fooToken grc20.Token = grc20.NewGRC20Token("Foo Token", "FOO", 4)

func MyBalance() uint64 {
caller := std.PrevRealm().Addr()
balance, _ := fooToken.BalanceOf(caller)
return balance
return fooToken.BalanceOf(caller)
}
```

Expand Down
Loading
Loading