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

gnomod.toml

module = "gno.land/p/nym-moultest123/txlink/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186404
  max_deposit = "6000000ugnot"

txlink.gno

// Package txlink provides utilities for creating transaction-related links
// compatible with Gnoweb, Gnobro, and other clients within the Gno ecosystem.
//
// This package is optimized for generating lightweight transaction links with
// flexible arguments, allowing users to build dynamic links that integrate
// seamlessly with various Gno clients.
//
// The package offers a way to generate clickable transaction MD links
// for the current "relative realm":
//
//  Using a builder pattern for more structured URLs:
//     txlink.NewLink("MyFunc").
//         AddArgs("k1", "v1", "k2", "v2"). // or multiple at once
//         SetSend("1000000ugnot").
//         URL()
//
// The builder pattern (TxBuilder) provides a fluent interface for constructing
// transaction URLs in the current "relative realm". Like Call, it supports both
// local realm paths and fully qualified paths through the underlying Call
// implementation.
//
// The Call function remains the core implementation, used both directly and
// internally by the builder pattern to generate the final URLs.
//
// This package is a streamlined alternative to helplink, providing similar
// functionality for transaction links without the full feature set of helplink.

package txlink

import (
	"chain/runtime"
	"chain/runtime/unsafe"
	"net/url"
	"strings"
)

var chainDomain = runtime.ChainDomain()

// Realm represents a specific realm for generating tx links.
type Realm string

// TxBuilder provides a fluent interface for building transaction URLs
type TxBuilder struct {
	fn        string   // function name
	args      []string // key-value pairs
	send      string   // optional send amount
	realm_XXX Realm    // realm for the URL
}

// NewLink creates a transaction link builder for the specified function in the current realm.
func NewLink(fn string) *TxBuilder {
	return Realm("").NewLink(fn)
}

// NewLink creates a transaction link builder for the specified function in this realm.
func (r Realm) NewLink(fn string) *TxBuilder {
	if fn == "" {
		return nil
	}
	return &TxBuilder{fn: fn, realm_XXX: r}
}

// addArg adds a key-value argument pair. Returns the builder for chaining.
func (b *TxBuilder) addArg(key, value string) *TxBuilder {
	if b == nil {
		return nil
	}
	if key == "" {
		return b
	}

	// Special case: "." prefix is for reserved keywords.
	if strings.HasPrefix(key, ".") {
		panic("invalid key")
	}

	b.args = append(b.args, key, value)
	return b
}

// AddArgs adds multiple key-value pairs at once. Arguments should be provided
// as pairs: AddArgs("key1", "value1", "key2", "value2").
func (b *TxBuilder) AddArgs(args ...string) *TxBuilder {
	if b == nil {
		return nil
	}
	if len(args)%2 != 0 {
		panic("odd number of arguments")
	}
	// Add key-value pairs
	for i := 0; i < len(args); i += 2 {
		key := args[i]
		value := args[i+1]
		b.addArg(key, value)
	}
	return b
}

// SetSend adds a send amount. (Only one send amount can be specified.)
func (b *TxBuilder) SetSend(amount string) *TxBuilder {
	if b == nil {
		return nil
	}
	if amount == "" {
		return b
	}
	b.send = amount
	return b
}

// URL generates the final URL using the standard $help&func=name format.
func (b *TxBuilder) URL() string {
	if b == nil || b.fn == "" {
		return ""
	}
	args := b.args
	if b.send != "" {
		args = append(args, ".send", b.send)
	}
	return b.realm_XXX.Call(b.fn, args...)
}

// Call returns a URL for the specified function with optional key-value
// arguments, for the current realm.
func Call(fn string, args ...string) string {
	return Realm("").Call(fn, args...)
}

// prefix returns the URL prefix for the realm.
func (r Realm) prefix() string {
	// relative
	if r == "" {
		curPath := unsafe.CurrentRealm().PkgPath()
		return strings.TrimPrefix(curPath, chainDomain)
	}

	// local realm -> /realm
	rlm := string(r)
	if strings.HasPrefix(rlm, chainDomain) {
		return strings.TrimPrefix(rlm, chainDomain)
	}

	// remote realm -> https://remote.land/realm
	return "https://" + string(r)
}

// Call returns a URL for the specified function with optional key-value
// arguments.
func (r Realm) Call(fn string, args ...string) string {
	if len(args) == 0 {
		return r.prefix() + "$help&func=" + fn
	}

	// Create url.Values to properly encode parameters.
	// But manage &func=fn as a special case to keep it as the first argument.
	values := url.Values{}

	// Check if args length is even
	if len(args)%2 != 0 {
		panic("odd number of arguments")
	}
	// Add key-value pairs to values
	for i := 0; i < len(args); i += 2 {
		key := args[i]
		value := args[i+1]
		values.Add(key, value)
	}

	// Build the base URL and append encoded query parameters
	return r.prefix() + "$help&func=" + fn + "&" + values.Encode()
}

txlink_test.gno

package txlink

import (
	"chain/runtime"
	"testing"

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

func TestCall(t *testing.T) {
	cd := runtime.ChainDomain()

	tests := []struct {
		fn        string
		args      []string
		want      string
		realm_XXX Realm
	}{
		{"foo", []string{"bar", "1", "baz", "2"}, "/p/nym-moultest123/txlink/v0$help&func=foo&bar=1&baz=2", ""},
		{"testFunc", []string{"key", "value"}, "/p/nym-moultest123/txlink/v0$help&func=testFunc&key=value", ""},
		{"noArgsFunc", []string{}, "/p/nym-moultest123/txlink/v0$help&func=noArgsFunc", ""},
		{"oddArgsFunc", []string{"key"}, "/p/nym-moultest123/txlink/v0$help&func=oddArgsFunc&error=odd+number+of+arguments", ""},
		{"foo", []string{"bar", "1", "baz", "2"}, "/r/lorem/ipsum$help&func=foo&bar=1&baz=2", Realm(cd + "/r/lorem/ipsum")},
		{"testFunc", []string{"key", "value"}, "/r/lorem/ipsum$help&func=testFunc&key=value", Realm(cd + "/r/lorem/ipsum")},
		{"noArgsFunc", []string{}, "/r/lorem/ipsum$help&func=noArgsFunc", Realm(cd + "/r/lorem/ipsum")},
		{"oddArgsFunc", []string{"key"}, "/r/lorem/ipsum$help&func=oddArgsFunc&error=odd+number+of+arguments", Realm(cd + "/r/lorem/ipsum")},
		{"foo", []string{"bar", "1", "baz", "2"}, "https://gno.world/r/lorem/ipsum$help&func=foo&bar=1&baz=2", "gno.world/r/lorem/ipsum"},
		{"testFunc", []string{"key", "value"}, "https://gno.world/r/lorem/ipsum$help&func=testFunc&key=value", "gno.world/r/lorem/ipsum"},
		{"noArgsFunc", []string{}, "https://gno.world/r/lorem/ipsum$help&func=noArgsFunc", "gno.world/r/lorem/ipsum"},
		{"oddArgsFunc", []string{"key"}, "https://gno.world/r/lorem/ipsum$help&func=oddArgsFunc&error=odd+number+of+arguments", "gno.world/r/lorem/ipsum"},
		{"test", []string{"key", "hello world"}, "/p/nym-moultest123/txlink/v0$help&func=test&key=hello+world", ""},
		{"test", []string{"key", "a&b=c"}, "/p/nym-moultest123/txlink/v0$help&func=test&key=a%26b%3Dc", ""},
		{"test", []string{"key", ""}, "/p/nym-moultest123/txlink/v0$help&func=test&key=", ""},
		{"testSend", []string{"key", "hello world", ".send", "1000000ugnot"}, "/p/nym-moultest123/txlink/v0$help&func=testSend&.send=1000000ugnot&key=hello+world", ""},
	}

	for _, tt := range tests {
		title := string(tt.realm_XXX) + "_" + tt.fn
		t.Run(title, func(t *testing.T) {
			if tt.fn == "oddArgsFunc" {
				defer func() {
					if r := recover(); r != nil {
						if r != "odd number of arguments" {
							t.Errorf("expected panic with message 'odd number of arguments', got: %v", r)
						}
					} else {
						t.Error("expected panic for odd number of arguments, but did not panic")
					}
				}()
			}
			got := tt.realm_XXX.Call(tt.fn, tt.args...)
			urequire.Equal(t, tt.want, got)
		})
	}
}

func TestBuilder(t *testing.T) {
	cases := []struct {
		name     string
		build    func() string
		expected string
	}{
		// Basic functionality tests
		{
			name: "empty_function",
			build: func() string {
				return NewLink("").URL()
			},
			expected: "",
		},
		{
			name: "function_without_args",
			build: func() string {
				return NewLink("MyFunc").URL()
			},
			expected: "/p/nym-moultest123/txlink/v0$help&func=MyFunc",
		},

		// Realm tests
		{
			name: "gnoland_realm",
			build: func() string {
				return Realm("gno.land/r/demo").
					NewLink("MyFunc").
					AddArgs("key", "value").
					URL()
			},
			expected: "/r/demo$help&func=MyFunc&key=value",
		},
		{
			name: "external_realm",
			build: func() string {
				return Realm("gno.world/r/demo").
					NewLink("MyFunc").
					AddArgs("key", "value").
					URL()
			},
			expected: "https://gno.world/r/demo$help&func=MyFunc&key=value",
		},
		{
			name: "empty_realm",
			build: func() string {
				return Realm("").
					NewLink("func").
					AddArgs("key", "value").
					URL()
			},
			expected: "/p/nym-moultest123/txlink/v0$help&func=func&key=value",
		},

		// URL encoding tests
		{
			name: "url_encoding_with_spaces",
			build: func() string {
				return NewLink("test").
					AddArgs("key", "hello world").
					URL()
			},
			expected: "/p/nym-moultest123/txlink/v0$help&func=test&key=hello+world",
		},
		{
			name: "url_encoding_with_special_chars",
			build: func() string {
				return NewLink("test").
					AddArgs("key", "a&b=c").
					URL()
			},
			expected: "/p/nym-moultest123/txlink/v0$help&func=test&key=a%26b%3Dc",
		},
		{
			name: "url_encoding_with_unicode",
			build: func() string {
				return NewLink("func").
					AddArgs("key", "🌟").
					URL()
			},
			expected: "/p/nym-moultest123/txlink/v0$help&func=func&key=%F0%9F%8C%9F",
		},
		{
			name: "url_encoding_with_special_chars_in_key",
			build: func() string {
				return NewLink("func").
					AddArgs("my/key", "value").
					URL()
			},
			expected: "/p/nym-moultest123/txlink/v0$help&func=func&my%2Fkey=value",
		},

		// AddArgs tests
		{
			name: "addargs_with_multiple_pairs",
			build: func() string {
				return NewLink("MyFunc").
					AddArgs("key1", "value1", "key2", "value2").
					URL()
			},
			expected: "/p/nym-moultest123/txlink/v0$help&func=MyFunc&key1=value1&key2=value2",
		},
		{
			name: "addargs_with_odd_number_of_args",
			build: func() string {
				defer func() {
					if r := recover(); r != nil {
						if r != "odd number of arguments" {
							t.Errorf("expected panic with message 'odd number of arguments', got: %v", r)
						}
					} else {
						t.Error("expected panic for odd number of arguments, but did not panic")
					}
				}()
				return NewLink("MyFunc").
					AddArgs("key1", "value1", "orphan").
					URL()
			},
			expected: "",
		},

		// Empty values tests
		{
			name: "empty_key_should_be_ignored",
			build: func() string {
				return NewLink("func").
					AddArgs("", "value").
					URL()
			},
			expected: "/p/nym-moultest123/txlink/v0$help&func=func",
		},
		{
			name: "empty_value_should_be_kept",
			build: func() string {
				return NewLink("func").
					AddArgs("key", "").
					URL()
			},
			expected: "/p/nym-moultest123/txlink/v0$help&func=func&key=",
		},

		// Send tests
		{
			name: "send_via_addsend_method",
			build: func() string {
				return NewLink("MyFunc").
					AddArgs("key", "value").
					SetSend("1000000ugnot").
					URL()
			},
			expected: "/p/nym-moultest123/txlink/v0$help&func=MyFunc&.send=1000000ugnot&key=value",
		},
		{
			name: "send_via_addarg_method_panic",
			build: func() string {
				defer func() {
					if r := recover(); r != nil {
						if r != "invalid key" {
							t.Errorf("expected panic with message 'invalid key', got: %v", r)
						}
					} else {
						t.Errorf("expected panic for .send key, but did not panic")
					}
				}()
				NewLink("MyFunc").AddArgs(".send", "1000000ugnot")
				return "no panic occurred"
			},
			expected: "",
		},
		{
			name: "addsend_should_override_previous_addsend",
			build: func() string {
				return NewLink("MyFunc").
					SetSend("1000000ugnot").
					SetSend("2000000ugnot").
					URL()
			},
			expected: "/p/nym-moultest123/txlink/v0$help&func=MyFunc&.send=2000000ugnot",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got := tc.build()
			urequire.Equal(t, tc.expected, got)
		})
	}
}