Skip to content

Commit

Permalink
Merge branch 'feat/token-hub' into feat/gnocket
Browse files Browse the repository at this point in the history
  • Loading branch information
matijamarjanovic committed Jan 17, 2025
2 parents 4a0621b + dabf342 commit 5838545
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
39 changes: 17 additions & 22 deletions examples/gno.land/r/matijamarjanovic/tokenhub/tokenhub.gno
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ func RegisterGRC1155Token(mtGetter grc1155.MultiTokenGetter, tokenID string) err
}

func GetMyBalancesAll() string {
return getBalances(std.GetOrigCaller())
return getBalances(string(std.PrevRealm().Addr()))
}

func GetMyBalances() string {
return getNonZeroBalances(std.GetOrigCaller())
return getNonZeroBalances(string(std.PrevRealm().Addr()))
}

func GetGRC20(key string) grc20.TokenGetter {
Expand Down Expand Up @@ -138,22 +138,6 @@ func MustGetGRC1155(key string) GRC1155TokenInfo {
return info
}

func GetBalancesAll(input string) string {
addr := getAddressForUsername(input)
if addr == "" {
panic("invalid address or username: " + input)
}
return getBalances(addr)
}

func GetBalances(input string) string {
addr := getAddressForUsername(input)
if addr == "" {
panic("invalid address or username: " + input)
}
return getNonZeroBalances(addr)
}

func GetAllNFTs() string {
var sb strings.Builder
registeredNFTs.Iterate("", "", func(key string, value interface{}) bool {
Expand Down Expand Up @@ -188,6 +172,12 @@ func GetAllTokens() string {
GetAllMultiTokens())
}

func GetAllTokensNoNFTs() string {
return ufmt.Sprintf("%s%s",
GetAllGRC20Tokens(),
GetAllMultiTokens())
}

func Render(path string) string {
var sb strings.Builder
sb.WriteString(test)
Expand Down Expand Up @@ -389,10 +379,13 @@ func Render(path string) string {
return sb.String()
}

func getBalances(addr std.Address) string {
func getBalances(input string) string {

addr := getAddressForUsername(input)
if addr == "" {
panic("invalid address")
panic("invalid address or username: " + input)
}

balances := avl.NewTree()
var sb strings.Builder

Expand Down Expand Up @@ -449,10 +442,12 @@ func getBalances(addr std.Address) string {
return sb.String()
}

func getNonZeroBalances(addr std.Address) string {
func getNonZeroBalances(input string) string {
addr := getAddressForUsername(input)
if addr == "" {
panic("invalid address")
panic("invalid address or username: " + input)
}

balances := avl.NewTree()
var sb strings.Builder

Expand Down
20 changes: 6 additions & 14 deletions examples/gno.land/r/matijamarjanovic/tokenhub/tokenhub_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ func TestTokenRegistration(t *testing.T) {
func TestNFTRegistration(t *testing.T) {
std.TestSetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home"))

addr := std.Address("g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y")

nft := grc721.NewBasicNFT("Test NFT", "TNFT")
nft.Mint(addr, grc721.TokenID("1"))
nft.Mint(std.CurrentRealm().Addr(), grc721.TokenID("1"))
err := RegisterNFT(nft.Getter(), "test_nft", "1")
urequire.NoError(t, err, "Should register NFT without error")

Expand Down Expand Up @@ -69,22 +67,20 @@ func TestGRC1155Registration(t *testing.T) {
func TestBalanceRetrieval(t *testing.T) {
std.TestSetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home"))

addr := std.Address("g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y")

token, ledger := grc20.NewToken("Test Token", "TEST", 6)
RegisterToken(token.Getter(), "test_tokenn")
ledger.Mint(addr, 1000)
ledger.Mint(std.CurrentRealm().Addr(), 1000)

mt := grc1155.NewBasicGRC1155Token("test-uri")
RegisterGRC1155Token(mt.Getter(), "11")
mt.SafeMint(addr, grc1155.TokenID("11"), 5)
mt.SafeMint(std.CurrentRealm().Addr(), grc1155.TokenID("11"), 5)

balances := GetBalancesAll(string(addr))
balances := GetMyBalancesAll()
uassert.True(t, strings.Contains(balances, "Token:gno.land/r/matijamarjanovic/tokenhub.test_tokenn:1000"), "Should show correct GRC20 balance")
uassert.True(t, strings.Contains(balances, "NFT:gno.land/r/matijamarjanovic/home.test_nft.1:1"), "Should show correct NFT balance") //already minted in test register
uassert.True(t, strings.Contains(balances, "NFT:gno.land/r/matijamarjanovic/home.test_nft.1"), "Should show correct NFT balance") //already minted in test register
uassert.True(t, strings.Contains(balances, "MultiToken:gno.land/r/matijamarjanovic/home.11:5"), "Should show multi-token balance")

nonZeroBalances := GetBalances(string(addr))
nonZeroBalances := GetMyBalances()
uassert.True(t, strings.Contains(nonZeroBalances, "Token:gno.land/r/matijamarjanovic/tokenhub.test_tokenn:1000"), "Should show non-zero GRC20 balance")
}

Expand All @@ -104,10 +100,6 @@ func TestErrorCases(t *testing.T) {

err = RegisterGRC1155Token(mt.Getter(), "1")
uassert.True(t, err != nil, "Should not allow duplicate registration")

uassert.PanicsWithMessage(t, "invalid address or username: invalid", func() {
GetBalances("invalid")
})
}

func TestTokenListingFunctions(t *testing.T) {
Expand Down

0 comments on commit 5838545

Please sign in to comment.