Skip to content

Commit

Permalink
chore: update 006-grc20
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <[email protected]>
  • Loading branch information
moul committed Sep 13, 2022
1 parent 8ae3d46 commit d5394ed
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 29 deletions.
6 changes: 0 additions & 6 deletions 006-grc20/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
-include ../single.mk

# ''gnodev build' does not work yet w/ this demo.
build:
@exit 0

# install gno.land/p/dom if not yet existing
publish: publish-dom publish-default
publish-dom:
$(call publish_pkg,gno.land/p/dom,"${GNO_ROOT}/examples/gno.land/p/dom")
21 changes: 18 additions & 3 deletions 006-grc20/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
# [r/moul_basics_import_v1](https://test2.gno.land/r/moul_basics_import_v1)
# [r/moul_basics_grc20_v1](https://test2.gno.land/r/moul_basics_grc20_v1)
_`README.md` generated by "make integration"._

## Examples

```console
foo@bar:~$ gnokey maketx call "MYWALLET" --gas-fee "1ugnot" \
> --broadcast "true" --chainid "test2" --remote "test2.gno.land:36657" \
> --gas-wanted "500000" --pkgpath gno.land/r/moul_basics_import_v1 \
> --func Render --args
> --gas-wanted "500000" --pkgpath gno.land/r/moul_basics_grc20_v1 \
> --func MyBalance
(100000000 uint64)
OK!
GAS WANTED: 500000
GAS USED: 289953
```

## `gnodev test`

```console
foo@bar:~$ gnodev test . --verbose
=== RUN Test
--- PASS: Test (0.00s)
ok ./. 3.07s
```

## How to publish locally

```sh
gnokey maketx addpkg "MYWALLET" --deposit "1ugnot" --gas-fee "1ugnot" --gas-wanted "5000000" --broadcast "true" --remote "localhost:26657" --chainid "dev" --pkgpath "gno.land/r/moul_basics_grc20_v1" --pkgdir "."
```

30 changes: 23 additions & 7 deletions 006-grc20/contract.gno
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package demo

import (
"gno.land/p/avl"
"gno.land/p/dom"
"std"

"gno.land/p/grc/grc20"
)

func Render(path string) string {
thread := dom.Plot{Name: "Hello!"}
thread.AddPost("Foo", "foo foo foo")
thread.AddPost("Bar", "bar bar bar")
return thread.String()
// FooToken is exported. Other contracts can interact with it.
var FooToken grc20.IGRC20

func init() {
// generate minter and mint some tokens to test1.
const test1 = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
minter := grc20.NewAdminToken("Foo Token", "FOO", 4)
minter.Mint(test1, 100000000)

// publicly expose an unprivileged implementation of IGRC20.
FooToken = minter.GRC20()
}

func MyBalance() uint64 {
caller := std.GetOrigCaller()
balance, err := FooToken.BalanceOf(caller)
if err != nil {
panic(err)
}
return balance
}
55 changes: 44 additions & 11 deletions 006-grc20/contract_test.gno
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
package demo

import (
"std"
"strings"
"testing"

"gno.land/p/testutils"
)

func Test(t *testing.T) {
got := Render("")
expected := `
# [plot] Hello!
## Foo
foo foo foo
## Bar
bar bar bar
`
if strings.TrimSpace(got) != strings.TrimSpace(expected) {
t.Fatalf("expected %q, got %q.", expected, got)
test1 := std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
test2 := testutils.TestAddress("test2")
std.TestSetOrigCaller(test1)

// check token metadata.
{
got := FooToken.GetName()
expected := "Foo Token"
if strings.TrimSpace(got) != strings.TrimSpace(expected) {
t.Fatalf("expected %q, got %q.", expected, got)
}
}

// check balance of test1.
{
got, _ := FooToken.BalanceOf(test1)
expected := 100000000
if got != expected {
t.Fatalf("expected %d, got %d.", expected, got)
}
}

// transfer tokens to test2.
_ = FooToken.Transfer(test2, 1000)

// check balance of test1.
{
got, _ := FooToken.BalanceOf(test1)
expected := 99999000
if got != expected {
t.Fatalf("expected %d, got %d.", expected, got)
}
}

// check balance of test2.
{
got, _ := FooToken.BalanceOf(test2)
expected := 1000
if got != expected {
t.Fatalf("expected %d, got %d.", expected, got)
}
}
}
3 changes: 2 additions & 1 deletion 006-grc20/integrations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
. ../integrations.lib.sh

pre
call --pkgpath ${GNO_CONTRACT_ENDPOINT} --func "Render" --args ""
#call --pkgpath ${GNO_CONTRACT_ENDPOINT} --func "FooToken.BalanceOf" --args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
call --pkgpath ${GNO_CONTRACT_ENDPOINT} --func "MyBalance"
post
2 changes: 1 addition & 1 deletion 006-grc20/pkgpath.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
r/moul_basics_import_v1
r/moul_basics_grc20_v1

0 comments on commit d5394ed

Please sign in to comment.