Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Latest commit

 

History

History
138 lines (91 loc) · 4.3 KB

File metadata and controls

138 lines (91 loc) · 4.3 KB

realm.gno

The realm realm provides a guide on how to import packages in a test environment (gno) and the production environment (gnokey addpkg) along with precautions to note.

First, let's write a test package:

// welcome.gno

package welcome


var MsgOfPkg = "this is a public variable"
var msgOfPkg = "this is a private variable"

func GetPrivVar() string {
	return msgOfPkg
}

func Gnome(name string) string {
	return "greetings " + name + ", welcome to the gnoland"
}

The welcome package contains the declaration of two variables and functions. These two variables have separate functionality for public and private usage. All functions within the package are designated as public.

The msgOfPkg is a private function, thus it is inaccessible for direct external access. We, therefore, use the GetPriveVar() function for indirect external access.

Next, we'll import the package above in both the test environment and the production environment.

Importing in the test environment

In the test environment, we will copy the file, as gno imports the package with the physical file path.

Importing in a production environment

In the production environment, we will run the maketx addpkg command to actually deploy the package to the network.

Realm Code

// realm.gno

package realm

import (
	"gno.land/p/demo/gor_welcome_v1"
)

func Render(name string) string {
	if name == "" {
		return "# Welcome Anonymous User"
	} 

	return "# " + welcome.Gnome(name)
}

func GetPublicVar() string {
	return welcome.MsgOfPkg
}

func getPrivateVar() string {
	return welcome.GetPrivVar()
}

The realm realm imports the pre-deployed welcome package and declares 3 functions. The Render() function and the GetPublicVar() function are public, while the getPrivateVar() is private.

Test Code

// realm_test.gno

package realm

import "testing"


func TestWelcomeAnon(t *testing.T) {
	expected := "# Welcome Anonymous User"
	got := Render("")
	if got != expected {
		t.Fatalf("expected %q, got %q.", expected, got)
	}
}

func TestWelcomeUser(t *testing.T) {
	expected := "# greetings n3wbie, welcome to the gnoland"
	got := Render("n3wbie")
	if got != expected {
		t.Fatalf("expected %q, got %q.", expected, got)
	}
}

func TestGetPublicVar(t *testing.T) {
	expected := "this is public variable"
	got := GetPublicVar()
	if got != expected {
		t.Fatalf("expected %q, got %q.", expected, got)
	}	
}

func TestGetPrivateVar(t *testing.T) {
	expected := "this is private variable"
	got := getPrivateVar()
	if got != expected {
		t.Fatalf("expected %q, got %q.", expected, got)
	}	
}

Result

We can confirm that the test has been successfully passed in the test environment using gno.

The package gets added successfully in the production environment.

Calling Render() and GetPublicVar() using the gnokey maketx call also works as expected.

However, unlike the results in gno, we run into an issue when calling the getPrivateVar() function using the gnokey maketx call command in the production environment.

On the other hand, calling the getPrivateVar() function using the gnokey query vm/qeval command works successfully.

Note: As of testnet3, we have run into unexpected results when using access modifiers with gno, maketx, and query. We suspect the cause to be one of the following: GnoVM, Gnokey Query, or gno. For now, we can get around this error by changing the lowercase to the uppercase to publicly access the functions. We will update this section once we determine the cause of this phenomenon.