Skip to content

Commit

Permalink
SetTokenURI
Browse files Browse the repository at this point in the history
  • Loading branch information
irreverentsimplicity committed Nov 11, 2023
1 parent 98f384e commit 9faec3d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/gno.land/p/demo/grc/grc721/basic_nft.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions examples/gno.land/p/demo/grc/grc721/basic_nft_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions examples/gno.land/p/demo/grc/grc721/igrc721.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,6 +15,7 @@ type IGRC721 interface {
}

type TokenID string
type TokenURI string

type TransferEvent struct {
From std.Address
Expand Down

0 comments on commit 9faec3d

Please sign in to comment.