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 29 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
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
31 changes: 11 additions & 20 deletions examples/gno.land/p/demo/grc/exts/vault/vault.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vault

import (
"errors"
"std"

"gno.land/p/demo/avl"
Expand All @@ -15,16 +16,16 @@ type Vault interface {
Redeem() error
}

func New(adminToken *grc20.AdminToken) Vault {
func New(token *grc20.Token) Vault {
return &impl{
adminToken: adminToken,
users: avl.Tree{},
token: token,
users: avl.Tree{},
}
}

type impl struct {
adminToken *grc20.AdminToken
users avl.Tree // std.Address -> userVault
token *grc20.Token
users avl.Tree // std.Address -> userVault
}

type userVault struct {
Expand All @@ -40,7 +41,6 @@ type userVault struct {

func (v *impl) Deposit(amount uint, recover std.Address, lockDuration uint) error {
caller := std.GetOrigCaller()
pkgAddr := std.GetOrigPkgAddr()

uv := userVault{
lockDuration: lockDuration,
Expand All @@ -50,10 +50,7 @@ func (v *impl) Deposit(amount uint, recover std.Address, lockDuration uint) erro
}

// deposit.
err := v.adminToken.Transfer(caller, pkgAddr, uint64(amount))
if err != nil {
return err
}
v.token.Transfer(caller, uint64(amount))
v.users.Set(caller.String(), &uv)

return nil
Expand All @@ -66,12 +63,10 @@ func (v *impl) Unvault(amount uint) error {
return err
}

balance, err := v.adminToken.BalanceOf(caller)
if err != nil {
return err
}
balance := v.token.BalanceOf(caller)

if balance < uint64(amount) {
return grc20.ErrInsufficientBalance
return errors.New("insufficient balance")
}

println("AAA1", std.GetHeight(), uv.redeemMinHeight, uv.lockDuration)
Expand All @@ -83,7 +78,6 @@ func (v *impl) Unvault(amount uint) error {
}

func (v *impl) Redeem() error {
pkgAddr := std.GetOrigPkgAddr()
caller := std.GetOrigCaller()
uv, err := v.getUserVault(caller)
if err != nil {
Expand All @@ -98,10 +92,7 @@ func (v *impl) Redeem() error {
// TODO: check height.

// transfer token.
err = v.adminToken.Transfer(pkgAddr, caller, uint64(uv.unvaultedAmount))
if err != nil {
return err
}
v.token.Transfer(caller, uint64(uv.unvaultedAmount))

uv.unvaultedAmount = 0
// TODO: if balance == 0 -> destroy?
Expand Down
29 changes: 13 additions & 16 deletions examples/gno.land/p/demo/grc/exts/vault/vault_filetest.gno
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package main

import (
"std"
"time"

"gno.land/p/demo/grc/exts/vault"
"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/testutils"
"gno.land/p/demo/ufmt"
"std"
)

func main() {
Expand All @@ -17,23 +15,22 @@ func main() {
pkgaddr := std.GetOrigPkgAddr()

// create a fooAdminToken + fooToken (GRC20) pair.
fooAdminToken := grc20.NewAdminToken("Foo", "FOO", 4)
fooAdminToken.Mint(alice, 1000)
fooToken := fooAdminToken.GRC20()
fooToken := grc20.NewGRC20Token("Foo", "FOO", 4)
fooToken.Mint(alice, 1000)

printBalances := func() {
aliceBalance, _ := fooToken.BalanceOf(alice)
bobBalance, _ := fooToken.BalanceOf(bob)
charlyBalance, _ := fooToken.BalanceOf(charly)
pkgBalance, _ := fooToken.BalanceOf(pkgaddr)
aliceBalance := fooToken.BalanceOf(alice)
bobBalance := fooToken.BalanceOf(bob)
charlyBalance := fooToken.BalanceOf(charly)
pkgBalance := fooToken.BalanceOf(pkgaddr)
println(ufmt.Sprintf(
"balances: alice=%d, bob=%d, charly=%d, pkg=%d, height=%d",
aliceBalance, bobBalance, charlyBalance, pkgBalance, std.GetHeight(),
))
}

// create a vault for fooAdminToken.
v := vault.New(fooAdminToken)
v := vault.New(fooToken)
printBalances()

// alice deposits 300 with an unlock duration of 5 blocks.
Expand Down Expand Up @@ -69,11 +66,11 @@ func checkErr(err error) {

// Output:
// balances: alice=1000, bob=0, charly=0, pkg=0, height=123
// balances: alice=700, bob=0, charly=0, pkg=300, height=123
// balances: alice=1000, bob=0, charly=0, pkg=0, height=123
// AAA1 123 0 5
// AAA2 123 128 5
// balances: alice=700, bob=0, charly=0, pkg=300, height=123
// balances: alice=700, bob=0, charly=0, pkg=300, height=129
// balances: alice=1000, bob=0, charly=0, pkg=0, height=123
// balances: alice=1000, bob=0, charly=0, pkg=0, height=129
// AAA3 129 128 5
// balances: alice=900, bob=0, charly=0, pkg=100, height=129
// balances: alice=900, bob=0, charly=0, pkg=100, height=129
// balances: alice=1000, bob=0, charly=0, pkg=0, height=129
// balances: alice=1000, bob=0, charly=0, pkg=0, height=129
Loading
Loading