From 9faec3d0f1ae95e222a928436bbff198464c5ba9 Mon Sep 17 00:00:00 2001 From: irreverentsimplicity Date: Fri, 27 Oct 2023 07:57:43 -0400 Subject: [PATCH] SetTokenURI --- .../gno.land/p/demo/grc/grc721/basic_nft.gno | 20 +++++++++++ .../p/demo/grc/grc721/basic_nft_test.gno | 35 +++++++++++++++++++ .../gno.land/p/demo/grc/grc721/igrc721.gno | 2 ++ 3 files changed, 57 insertions(+) diff --git a/examples/gno.land/p/demo/grc/grc721/basic_nft.gno b/examples/gno.land/p/demo/grc/grc721/basic_nft.gno index b707527c6a4..412751405b2 100644 --- a/examples/gno.land/p/demo/grc/grc721/basic_nft.gno +++ b/examples/gno.land/p/demo/grc/grc721/basic_nft.gno @@ -69,6 +69,26 @@ func (s *basicNFT) TokenURI(tid TokenID) (string, error) { return uri.(string), nil } +func (s *basicNFT) SetTokenURI(tid TokenID, tURI TokenURI) (bool, error) { + // check for invalid TokenID + owner, found := s.owners.Get(string(tid)) + if !found { + return false, ErrInvalidTokenId + } + + // check for the right owner + owner, err := s.OwnerOf(tid) + if err != nil { + return false, ErrCallerIsNotOwner + } + s.tokenURIs.Set(string(tid), string(tURI)) + storedURI, added := s.tokenURIs.Get(string(tid)) + if !added { + return false, ErrTokenURINotSet + } + return true, nil +} + // IsApprovedForAll returns true if operator is approved for all by the owner. // Otherwise, returns false func (s *basicNFT) IsApprovedForAll(owner, operator std.Address) bool { diff --git a/examples/gno.land/p/demo/grc/grc721/basic_nft_test.gno b/examples/gno.land/p/demo/grc/grc721/basic_nft_test.gno index 7ad378af2f7..aa2c92c466d 100644 --- a/examples/gno.land/p/demo/grc/grc721/basic_nft_test.gno +++ b/examples/gno.land/p/demo/grc/grc721/basic_nft_test.gno @@ -135,6 +135,41 @@ func TestOwnerOf(t *testing.T) { } } +func TestSetTokenURI(t *testing.T) { + dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol) + if dummy == nil { + t.Errorf("should not be nil") + } + + addr1 := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + tokenURI := "http://example.com/token" + + dummy.mint(addr1.Resolve(), TokenID("1")) + dummy.SetTokenURI(TokenID("1"), TokenURI(tokenURI)) + + // Test case: Invalid token ID + _, err := dummy.SetTokenURI(TokenID("2"), TokenURI(tokenURI)) + if err != ErrInvalidTokenId { + t.Errorf("Expected error %v, got %v", ErrInvalidTokenId, err) + } + + // Test case: Caller is not owner + owner, err := dummy.OwnerOf(TokenID("1")) + if err != nil { + t.Errorf("should not result in error") + } + if owner != addr1.Resolve() { + t.Errorf("expected: (%s), got: (%s)", addr1.Resolve().String(), owner.String()) + } + + // Test case: Retrieving TokenURI + dummyTokenURI, err := dummy.TokenURI(TokenID("1")) + if dummyTokenURI != tokenURI { + t.Errorf("Expected URI %v, got %v", tokenURI, dummyTokenURI) + } + +} + func TestIsApprovedForAll(t *testing.T) { dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol) if dummy == nil { diff --git a/examples/gno.land/p/demo/grc/grc721/igrc721.gno b/examples/gno.land/p/demo/grc/grc721/igrc721.gno index d60308e11a1..779e9a5b557 100644 --- a/examples/gno.land/p/demo/grc/grc721/igrc721.gno +++ b/examples/gno.land/p/demo/grc/grc721/igrc721.gno @@ -5,6 +5,7 @@ import "std" type IGRC721 interface { BalanceOf(owner std.Address) (uint64, error) OwnerOf(tid TokenID) (std.Address, error) + SetTokenURI(tid TokenID, tURI TokenURI) (bool, error) SafeTransferFrom(from, to std.Address, tid TokenID) error TransferFrom(from, to std.Address, tid TokenID) error Approve(approved std.Address, tid TokenID) error @@ -14,6 +15,7 @@ type IGRC721 interface { } type TokenID string +type TokenURI string type TransferEvent struct { From std.Address