Skip to content

Commit

Permalink
fix nft registration, now each minted can be registered
Browse files Browse the repository at this point in the history
  • Loading branch information
matijamarjanovic committed Jan 17, 2025
1 parent 87aac01 commit 8ab9a26
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
20 changes: 14 additions & 6 deletions examples/gno.land/r/matijamarjanovic/tokenhub/tokenhub.gno
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ type GRC1155TokenInfo struct {
}

var (
registeredNFTs = avl.NewTree() // rlmPath[.slug] -> NFTGetter
registeredNFTs = avl.NewTree() // rlmPath[.slug] -> NFTInfo
registeredMTs = avl.NewTree() // rlmPath[.slug] -> GRC1155TokenInfo

ErrNFTAlreadyRegistered = errors.New("NFT already registered")
ErrNFTNotFound = errors.New("NFT not found")
ErrMTAlreadyRegistered = errors.New("Multi-token already registered")
ErrMTNotFound = errors.New("Multi-token not found")
ErrMTInfoNotFound = errors.New("Multi-token info not found")
ErrNFTtokIDNotExists = errors.New("NFT token ID does not exists")
ErrNFTNotMetadata = errors.New("NFT must implement IGRC721CollectionMetadata")

pageSize = 2
test = ""
Expand All @@ -44,16 +46,23 @@ func RegisterToken(tokenGetter grc20.TokenGetter, slugs ...string) {
grc20reg.Register(tokenGetter, slug)
}

// TokenId is mandatory for NFTs
func RegisterNFT(nftGetter grc721.NFTGetter, tokenId string) error {
// registering a collection of NFTs
func RegisterNFT(nftGetter grc721.NFTGetter, collection string, tokenId string) error {

nft := nftGetter()
_, ok := nft.(grc721.IGRC721CollectionMetadata)
if !ok {
return errors.New("NFT must implement IGRC721CollectionMetadata")
return ErrNFTNotMetadata
}

nftOwner, err := nft.OwnerOf(grc721.TokenID(tokenId))
if string(nftOwner) == "" || err != nil {
return ErrNFTtokIDNotExists
}

rlmPath := std.PrevRealm().PkgPath()
key := fqname.Construct(rlmPath, tokenId)
k := ufmt.Sprintf("%s.%s", collection, tokenId)
key := fqname.Construct(rlmPath, k)

if registeredNFTs.Has(key) {
return ErrNFTAlreadyRegistered
Expand Down Expand Up @@ -419,7 +428,6 @@ func getBalances(addr std.Address) string {
balance := value.(uint64)
symbol := strings.TrimPrefix(key, "nft:")
sb.WriteString(ufmt.Sprintf("NFT:%s:%d,", symbol, balance))
sb.WriteString(ufmt.Sprintf("Token:%s:%d,", symbol, balance))
case strings.HasPrefix(key, "token:"):
balance := value.(uint64)
symbol := strings.TrimPrefix(key, "token:")
Expand Down
28 changes: 14 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,11 +30,14 @@ 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")
err := RegisterNFT(nft.Getter(), "test_nft")
nft.Mint(addr, grc721.TokenID("1"))
err := RegisterNFT(nft.Getter(), "test_nft", "1")
urequire.NoError(t, err, "Should register NFT without error")

getter := GetGRC721("gno.land/r/matijamarjanovic/home.test_nft")
getter := GetGRC721("gno.land/r/matijamarjanovic/home.test_nft.1")
urequire.True(t, getter != nil, "Should retrieve registered NFT")

retrievedNFT := getter()
Expand All @@ -43,7 +46,7 @@ func TestNFTRegistration(t *testing.T) {
uassert.Equal(t, "Test NFT", metadata.Name(), "NFT name should match")
uassert.Equal(t, "TNFT", metadata.Symbol(), "NFT symbol should match")

err = RegisterNFT(nft.Getter(), "test_nft")
err = RegisterNFT(nft.Getter(), "test_nft", "1")
uassert.True(t, err != nil, "Should not allow duplicate registration")
}

Expand Down Expand Up @@ -72,17 +75,13 @@ func TestBalanceRetrieval(t *testing.T) {
RegisterToken(token.Getter(), "test_tokenn")
ledger.Mint(addr, 1000)

nft := grc721.NewBasicNFT("Test NFT", "TNFT")
RegisterNFT(nft.Getter(), "test_nftt")
nft.Mint(addr, grc721.TokenID("str"))

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

balances := GetBalancesAll(string(addr))
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_nftt:1"), "Should show correct NFT 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, "MultiToken:gno.land/r/matijamarjanovic/home.11:5"), "Should show multi-token balance")

nonZeroBalances := GetBalances(string(addr))
Expand All @@ -93,10 +92,10 @@ func TestErrorCases(t *testing.T) {
std.TestSetRealm(std.NewCodeRealm("gno.land/r/matijamarjanovic/home"))

nft := grc721.NewBasicNFT("Test NFT", "TNFT")
err := RegisterNFT(nft.Getter(), "test_nft")
err := RegisterNFT(nft.Getter(), "test_nft", "1")
uassert.True(t, err != nil, "Should not allow duplicate registration")

err = RegisterNFT(nft.Getter(), "test_nft")
err = RegisterNFT(nft.Getter(), "test_nft", "1")
uassert.True(t, err != nil, "Should not allow duplicate registration")

mt := grc1155.NewBasicGRC1155Token("test-uri")
Expand All @@ -117,14 +116,15 @@ func TestTokenListingFunctions(t *testing.T) {
grc20Token, _ := grc20.NewToken("Test Token", "TEST", 6)
RegisterToken(grc20Token.Getter(), "listing_token")

nftToken := grc721.NewBasicNFT("Test NFT", "TNFT")
RegisterNFT(nftToken.Getter(), "listing_nft")
nftToken := grc721.NewBasicNFT("Listing NFT", "LNFT")
nftToken.Mint(std.Address("g1ej0qca5ptsw9kfr64ey8jvfy9eacga6mpj2z0y"), grc721.TokenID("1"))
RegisterNFT(nftToken.Getter(), "listing_nft", "1")

multiToken := grc1155.NewBasicGRC1155Token("test-uri")
RegisterGRC1155Token(multiToken.Getter(), "listing_mt")

nftList := GetAllNFTs()
uassert.True(t, strings.Contains(nftList, "NFT:gno.land/r/matijamarjanovic/home.listing_nft"),
uassert.True(t, strings.Contains(nftList, "NFT:gno.land/r/matijamarjanovic/home.listing_nft.1"),
"GetAllNFTs should list registered NFT")

grc20List := GetAllGRC20Tokens()
Expand All @@ -136,7 +136,7 @@ func TestTokenListingFunctions(t *testing.T) {
"GetAllMultiTokens should list registered multi-token")

completeList := GetAllTokens()
uassert.True(t, strings.Contains(completeList, "NFT:gno.land/r/matijamarjanovic/home.listing_nft"),
uassert.True(t, strings.Contains(completeList, "NFT:gno.land/r/matijamarjanovic/home.listing_nft.1"),
"GetAllTokens should list NFTs")
uassert.True(t, strings.Contains(completeList, "Token:gno.land/r/matijamarjanovic/tokenhub.listing_token"),
"GetAllTokens should list GRC20 tokens")
Expand Down

0 comments on commit 8ab9a26

Please sign in to comment.