-
Notifications
You must be signed in to change notification settings - Fork 423
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
Changes from 16 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
f0a5182
changes
leohhhn cb7f3f4
chore: fixup
leohhhn 1a1e027
chore: fixup
leohhhn a90403d
update pages, add converter page
leohhhn 452e1bd
save
leohhhn 688ad12
changes
leohhhn ab9c1eb
chore: fixup
leohhhn a46da7b
add sortcoins
leohhhn 61c0b2d
chore: fixup
leohhhn 98dda69
update gno.mod
leohhhn d985f16
rm comment
leohhhn 5c6ce48
chore: fixup
leohhhn 31a3a02
update tests
leohhhn 9b32aca
add converter pkg, make better
leohhhn a19e4d9
remove unnecessary code
leohhhn bd684f1
Merge branch 'master' into better-coins
leohhhn 0bb5773
fmt
leohhhn 3387a61
fix tests
leohhhn 19e88d6
chore: fixup
leohhhn a04165a
Merge branch 'master' into better-coins
leohhhn dee7b2e
fix tests, rename package
leohhhn 7fec5be
update coinsort
leohhhn 05d7e22
rm coins
leohhhn 8c192da
Merge branch 'master' into better-coins
leohhhn 6c71ea9
Merge branch 'master' into better-coins
leohhhn f9abb75
automatically convert cosmos with balances/{addr}
leohhhn 7450487
chore: fixup
leohhhn 07db1ec
Merge branch 'master' into better-coins
leohhhn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/leon/coinsort |
leohhhn marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/leon/cosmtogno |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.