Skip to content

feat(examples): make /r/gnoland/coins better #4306

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

Merged
merged 28 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
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
82 changes: 82 additions & 0 deletions examples/gno.land/p/leon/coinsort/coinsort.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Package coinsort provides helpers to sort a slice of std.Coins using the
// classic sort.Sort API (without relying on sort.Slice).
//
// Usage examples:
//
// coins := banker.GetCoins("g1....")
//
// // Ascending by balance
// coinsort.SortByBalance(coins)
//
// // Alphabetically by denom
// coinsort.SortByDenomAlphabetically(coins)
//
// // Custom order – largest balance first
// coinsort.SortBy(coins, func(a, b std.Coin) bool {
// return a.Amount > b.Amount // descending
// })
package coinsort

import (
"sort"
"std"
)

type ByAmount struct{ std.Coins }

func (b ByAmount) Len() int { return len(b.Coins) }
func (b ByAmount) Swap(i, j int) { b.Coins[i], b.Coins[j] = b.Coins[j], b.Coins[i] }
func (b ByAmount) Less(i, j int) bool { return b.Coins[i].Amount < b.Coins[j].Amount }

type ByDenom struct{ std.Coins }

func (b ByDenom) Len() int { return len(b.Coins) }
func (b ByDenom) Swap(i, j int) { b.Coins[i], b.Coins[j] = b.Coins[j], b.Coins[i] }
func (b ByDenom) Less(i, j int) bool { return b.Coins[i].Denom < b.Coins[j].Denom }

// SortByBalance sorts c in ascending order by Amount.
//
// coinsort.SortByBalance(myCoins)
func SortByBalance(c std.Coins) {
sort.Sort(ByAmount{c})
}

// SortByDenomAlphabetically sorts c in ascending lexicographic order by Denom.
//
// coinsort.SortByDenomAlphabetically(myCoins)
func SortByDenomAlphabetically(c std.Coins) {
sort.Sort(ByDenom{c})
}

// LessFunc defines the comparison function for SortBy. It must return true if
// 'a' should come before 'b'.

type LessFunc func(a, b std.Coin) bool

// customSorter adapts a LessFunc to sort.Interface so we can keep using
// sort.Sort (rather than sort.Slice).

type customSorter struct {
coins std.Coins
less LessFunc
}

func (cs customSorter) Len() int { return len(cs.coins) }
func (cs customSorter) Swap(i, j int) { cs.coins[i], cs.coins[j] = cs.coins[j], cs.coins[i] }
func (cs customSorter) Less(i, j int) bool {
return cs.less(cs.coins[i], cs.coins[j])
}

// SortBy sorts c in place using the provided LessFunc.
//
// Example – descending by Amount:
//
// coinsort.SortBy(coins, func(a, b std.Coin) bool {
// return a.Amount > b.Amount
// })
func SortBy(c std.Coins, less LessFunc) {
if less == nil {
return // nothing to do; keep original order
}
sort.Sort(customSorter{coins: c, less: less})
}
97 changes: 97 additions & 0 deletions examples/gno.land/p/leon/coinsort/coinsort_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package coinsort

import (
"std"
"testing"
)

func TestSortByBalance(t *testing.T) {
coins := std.Coins{
std.Coin{Denom: "b", Amount: 50},
std.Coin{Denom: "c", Amount: 10},
std.Coin{Denom: "a", Amount: 100},
}

expected := std.Coins{
std.Coin{Denom: "c", Amount: 10},
std.Coin{Denom: "b", Amount: 50},
std.Coin{Denom: "a", Amount: 100},
}

SortByBalance(coins)

for i := range coins {
if coins[i] != expected[i] {
t.Errorf("SortByBalance failed at index %d: got %+v, want %+v", i, coins[i], expected[i])
}
}
}

func TestSortByDenomAlphabetically(t *testing.T) {
coins := std.Coins{
std.Coin{Denom: "ugnot", Amount: 1},
std.Coin{Denom: "btc", Amount: 1},
std.Coin{Denom: "eth", Amount: 1},
}

expected := std.Coins{
std.Coin{Denom: "btc", Amount: 1},
std.Coin{Denom: "eth", Amount: 1},
std.Coin{Denom: "ugnot", Amount: 1},
}

SortByDenomAlphabetically(coins)

for i := range coins {
if coins[i] != expected[i] {
t.Errorf("SortByDenomAlphabetically failed at index %d: got %+v, want %+v", i, coins[i], expected[i])
}
}
}

func TestSortByCustomDescendingAmount(t *testing.T) {
coins := std.Coins{
std.Coin{Denom: "a", Amount: 2},
std.Coin{Denom: "b", Amount: 3},
std.Coin{Denom: "c", Amount: 1},
}

expected := std.Coins{
std.Coin{Denom: "b", Amount: 3},
std.Coin{Denom: "a", Amount: 2},
std.Coin{Denom: "c", Amount: 1},
}

SortBy(coins, func(a, b std.Coin) bool {
return a.Amount > b.Amount // descending
})

for i := range coins {
if coins[i] != expected[i] {
t.Errorf("SortBy custom descending failed at index %d: got %+v, want %+v", i, coins[i], expected[i])
}
}
}

func TestSortByNilFunc(t *testing.T) {
coins := std.Coins{
std.Coin{Denom: "x", Amount: 5},
std.Coin{Denom: "z", Amount: 20},
std.Coin{Denom: "y", Amount: 10},
}

expected := std.Coins{
std.Coin{Denom: "x", Amount: 5},
std.Coin{Denom: "z", Amount: 20},
std.Coin{Denom: "y", Amount: 10},
}

SortBy(coins, nil)

// should stay the same
for i := range coins {
if coins[i] != expected[i] {
t.Errorf("SortBy nil func failed at index %d: got %+v, want %+v", i, coins[i], expected[i])
}
}
}
1 change: 1 addition & 0 deletions examples/gno.land/p/leon/coinsort/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/leon/coinsort
21 changes: 21 additions & 0 deletions examples/gno.land/p/leon/cosmtogno/converter.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cosmtogno

import (
"std"

"gno.land/p/demo/ufmt"
)

// ConvertCosmosToGno takes a Bech32 Cosmos address (prefix "cosmos")
// and returns the same address re-encoded with the gno.land prefix "g".
func ConvertCosmosToGno(addr string) (std.Address, error) {
prefix, decoded, ok := std.DecodeBech32(std.Address(addr))
if !ok {
return "", ufmt.Errorf("bech32 decode failed")
}
if prefix != "cosmos" {
return "", ufmt.Errorf("expected a cosmos address, got prefix %q", prefix)
}

return std.EncodeBech32("g", decoded), nil
}
37 changes: 37 additions & 0 deletions examples/gno.land/p/leon/cosmtogno/converter_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cosmtogno

import (
"std"
"testing"
)

func TestConvertCosmosToGno(t *testing.T) {
decoded := [20]byte{
0x01, 0x02, 0x03, 0x04, 0x05,
0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14,
}

cosmosAddr := std.EncodeBech32("cosmos", decoded)
wantGno := std.EncodeBech32("g", decoded)

got, err := ConvertCosmosToGno(cosmosAddr.String())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != wantGno {
t.Fatalf("got %s, want %s", got, wantGno)
}

// invalid bech32
if _, err := ConvertCosmosToGno("not-bech32"); err == nil {
t.Fatalf("expected error for invalid bech32")
}

// wrong prefix
gAddr := std.EncodeBech32("g", decoded)
if _, err := ConvertCosmosToGno(gAddr.String()); err == nil {
t.Fatalf("expected error for non-cosmos prefix")
}
}
1 change: 1 addition & 0 deletions examples/gno.land/p/leon/cosmtogno/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/leon/cosmtogno
Loading
Loading