Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

contract.gno

package grc20

import (
	"chain/runtime/unsafe"

	tokens "gno.land/p/nt/grc20/v0"
)

// test1 receives the initial mint at construction.
var test1 = address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")

// Token is exported. Other contracts can read it (balances, metadata).
// privateLedger stays unexported: it grants unrestricted mint/burn/transfer.
var (
	Token         *tokens.Token
	privateLedger *tokens.PrivateLedger
)

func init(cur realm) {
	// generate the token and mint some tokens to test1.
	Token, privateLedger = tokens.NewToken("Foo Token", "FOO", 4, 0, cur)
	privateLedger.Mint(test1, 100000000)
}

func MyBalance() int64 {
	return Token.BalanceOf(unsafe.OriginCaller())
}

contract_test.gno

package grc20

import (
	"strings"
	"testing"

	"gno.land/p/nt/testutils/v0"
)

func Test(t *testing.T) {
	test2 := testutils.TestAddress("test2")

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

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

	// transfer tokens from test1 to test2.
	if err := privateLedger.Transfer(test1, test2, 1000); err != nil {
		t.Fatal(err)
	}

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

	// check balance of test2.
	{
		got := Token.BalanceOf(test2)
		expected := int64(1000)
		if got != expected {
			t.Fatalf("expected %d, got %d.", expected, got)
		}
	}
}

gnomod.toml

module = "gno.land/r/nym-moultest123/demo/grc20/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186406
  max_deposit = "5000000ugnot"