github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/grc/grc1155/basic_grc1155_token_test.gno (about)

     1  package grc1155
     2  
     3  import (
     4  	"fmt"
     5  	"std"
     6  	"testing"
     7  
     8  	"gno.land/p/demo/users"
     9  )
    10  
    11  const dummyURI = "ipfs://xyz"
    12  
    13  func TestNewBasicGRC1155Token(t *testing.T) {
    14  	dummy := NewBasicGRC1155Token(dummyURI)
    15  	if dummy == nil {
    16  		return t.Errorf("should not be nil")
    17  	}
    18  }
    19  
    20  func TestUri(t *testing.T) {
    21  	dummy := NewBasicGRC1155Token(dummyURI)
    22  	if dummy == nil {
    23  		t.Errorf("should not be nil")
    24  	}
    25  
    26  	uri := dummy.Uri()
    27  	if uri != dummyURI {
    28  		t.Errorf("expected: (%s), got: (%s)", dummyURI, uri)
    29  	}
    30  }
    31  
    32  func TestBalanceOf(t *testing.T) {
    33  	dummy := NewBasicGRC1155Token(dummyURI)
    34  	if dummy == nil {
    35  		t.Errorf("should not be nil")
    36  	}
    37  
    38  	addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
    39  	addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
    40  
    41  	tid1 := TokenID("1")
    42  	tid2 := TokenID("2")
    43  
    44  	balanceZeroAddressOfToken1, err := dummy.BalanceOf(zeroAddress, tid1)
    45  	if err == nil {
    46  		t.Errorf("should result in error")
    47  	}
    48  	balanceAddr1OfToken1, err := dummy.BalanceOf(addr1, tid1)
    49  	if err != nil {
    50  		t.Errorf("should not result in error")
    51  	}
    52  	if balanceAddr1OfToken1 != 0 {
    53  		t.Errorf("expected: (%d), got: (%d)", 0, balanceAddr1OfToken1)
    54  	}
    55  
    56  	dummy.mintBatch(addr1, []TokenID{tid1, tid2}, []uint64{10, 100})
    57  	dummy.mintBatch(addr2, []TokenID{tid1}, []uint64{20})
    58  
    59  	balanceAddr1OfToken1, err = dummy.BalanceOf(addr1, tid1)
    60  	if err != nil {
    61  		t.Errorf("should not result in error")
    62  	}
    63  	balanceAddr1OfToken2, err := dummy.BalanceOf(addr1, tid2)
    64  	if err != nil {
    65  		t.Errorf("should not result in error")
    66  	}
    67  	balanceAddr2OfToken1, err := dummy.BalanceOf(addr2, tid1)
    68  	if err != nil {
    69  		t.Errorf("should not result in error")
    70  	}
    71  
    72  	if balanceAddr1OfToken1 != 10 {
    73  		t.Errorf("expected: (%d), got: (%d)", 10, balanceAddr1OfToken1)
    74  	}
    75  	if balanceAddr1OfToken2 != 100 {
    76  		t.Errorf("expected: (%d), got: (%d)", 100, balanceAddr1OfToken2)
    77  	}
    78  	if balanceAddr2OfToken1 != 20 {
    79  		t.Errorf("expected: (%d), got: (%d)", 20, balanceAddr2OfToken1)
    80  	}
    81  }
    82  
    83  func TestBalanceOfBatch(t *testing.T) {
    84  	dummy := NewBasicGRC1155Token(dummyURI)
    85  	if dummy == nil {
    86  		t.Errorf("should not be nil")
    87  	}
    88  
    89  	addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
    90  	addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
    91  
    92  	tid1 := TokenID("1")
    93  	tid2 := TokenID("2")
    94  
    95  	balanceBatch, err := dummy.BalanceOfBatch([]std.Address{addr1, addr2}, []TokenID{tid1, tid2})
    96  	if err != nil {
    97  		t.Errorf("should not result in error")
    98  	}
    99  
   100  	if balanceBatch[0] != 0 {
   101  		t.Errorf("expected: (%d), got: (%d)", 0, balanceBatch[0])
   102  	}
   103  	if balanceBatch[1] != 0 {
   104  		t.Errorf("expected: (%d), got: (%d)", 0, balanceBatch[1])
   105  	}
   106  
   107  	dummy.mintBatch(addr1, []TokenID{tid1}, []uint64{10})
   108  	dummy.mintBatch(addr2, []TokenID{tid2}, []uint64{20})
   109  
   110  	balanceBatch, err = dummy.BalanceOfBatch([]std.Address{addr1, addr2}, []TokenID{tid1, tid2})
   111  	if err != nil {
   112  		t.Errorf("should not result in error")
   113  	}
   114  
   115  	if balanceBatch[0] != 10 {
   116  		t.Errorf("expected: (%d), got: (%d)", 10, balanceBatch[0])
   117  	}
   118  	if balanceBatch[1] != 20 {
   119  		t.Errorf("expected: (%d), got: (%d)", 20, balanceBatch[1])
   120  	}
   121  }
   122  
   123  func TestIsApprovedForAll(t *testing.T) {
   124  	dummy := NewBasicGRC1155Token(dummyURI)
   125  	if dummy == nil {
   126  		t.Errorf("should not be nil")
   127  	}
   128  
   129  	addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
   130  	addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
   131  
   132  	isApprovedForAll := dummy.IsApprovedForAll(addr1, addr2)
   133  	if isApprovedForAll != false {
   134  		t.Errorf("expected: (%v), got: (%v)", false, isApprovedForAll)
   135  	}
   136  }
   137  
   138  func TestSetApprovalForAll(t *testing.T) {
   139  	dummy := NewBasicGRC1155Token(dummyURI)
   140  	if dummy == nil {
   141  		t.Errorf("should not be nil")
   142  	}
   143  
   144  	caller := std.GetOrigCaller()
   145  	addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
   146  
   147  	isApprovedForAll := dummy.IsApprovedForAll(caller, addr)
   148  	if isApprovedForAll != false {
   149  		t.Errorf("expected: (%v), got: (%v)", false, isApprovedForAll)
   150  	}
   151  
   152  	err := dummy.SetApprovalForAll(addr, true)
   153  	if err != nil {
   154  		t.Errorf("should not result in error")
   155  	}
   156  
   157  	isApprovedForAll = dummy.IsApprovedForAll(caller, addr)
   158  	if isApprovedForAll != true {
   159  		t.Errorf("expected: (%v), got: (%v)", true, isApprovedForAll)
   160  	}
   161  
   162  	err = dummy.SetApprovalForAll(addr, false)
   163  	if err != nil {
   164  		t.Errorf("should not result in error")
   165  	}
   166  
   167  	isApprovedForAll = dummy.IsApprovedForAll(caller, addr)
   168  	if isApprovedForAll != false {
   169  		t.Errorf("expected: (%v), got: (%v)", false, isApprovedForAll)
   170  	}
   171  }
   172  
   173  func TestSafeTransferFrom(t *testing.T) {
   174  	dummy := NewBasicGRC1155Token(dummyURI)
   175  	if dummy == nil {
   176  		t.Errorf("should not be nil")
   177  	}
   178  
   179  	caller := std.GetOrigCaller()
   180  	addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
   181  
   182  	tid := TokenID("1")
   183  
   184  	dummy.mintBatch(caller, []TokenID{tid}, []uint64{100})
   185  
   186  	err := dummy.SafeTransferFrom(caller, zeroAddress, tid, 10)
   187  	if err == nil {
   188  		t.Errorf("should result in error")
   189  	}
   190  
   191  	err = dummy.SafeTransferFrom(caller, addr, tid, 160)
   192  	if err == nil {
   193  		t.Errorf("should result in error")
   194  	}
   195  
   196  	err = dummy.SafeTransferFrom(caller, addr, tid, 60)
   197  	if err != nil {
   198  		t.Errorf("should not result in error")
   199  	}
   200  
   201  	// Check balance of caller after transfer
   202  	balanceOfCaller, err := dummy.BalanceOf(caller, tid)
   203  	if err != nil {
   204  		t.Errorf("should not result in error")
   205  	}
   206  	if balanceOfCaller != 40 {
   207  		t.Errorf("expected: (%d), got: (%d)", 40, balanceOfCaller)
   208  	}
   209  
   210  	// Check balance of addr after transfer
   211  	balanceOfAddr, err := dummy.BalanceOf(addr, tid)
   212  	if err != nil {
   213  		t.Errorf("should not result in error")
   214  	}
   215  	if balanceOfAddr != 60 {
   216  		t.Errorf("expected: (%d), got: (%d)", 60, balanceOfAddr)
   217  	}
   218  }
   219  
   220  func TestSafeBatchTransferFrom(t *testing.T) {
   221  	dummy := NewBasicGRC1155Token(dummyURI)
   222  	if dummy == nil {
   223  		t.Errorf("should not be nil")
   224  	}
   225  
   226  	caller := std.GetOrigCaller()
   227  	addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
   228  
   229  	tid1 := TokenID("1")
   230  	tid2 := TokenID("2")
   231  
   232  	dummy.mintBatch(caller, []TokenID{tid1, tid2}, []uint64{10, 100})
   233  
   234  	err := dummy.SafeBatchTransferFrom(caller, zeroAddress, []TokenID{tid1, tid2}, []uint64{4, 60})
   235  	if err == nil {
   236  		t.Errorf("should result in error")
   237  	}
   238  	err = dummy.SafeBatchTransferFrom(caller, addr, []TokenID{tid1, tid2}, []uint64{40, 60})
   239  	if err == nil {
   240  		t.Errorf("should result in error")
   241  	}
   242  	err = dummy.SafeBatchTransferFrom(caller, addr, []TokenID{tid1}, []uint64{40, 60})
   243  	if err == nil {
   244  		t.Errorf("should result in error")
   245  	}
   246  	err = dummy.SafeBatchTransferFrom(caller, addr, []TokenID{tid1, tid2}, []uint64{4, 60})
   247  	if err != nil {
   248  		t.Errorf("should not result in error")
   249  	}
   250  
   251  	balanceBatch, err := dummy.BalanceOfBatch([]std.Address{caller, addr, caller, addr}, []TokenID{tid1, tid1, tid2, tid2})
   252  	if err != nil {
   253  		t.Errorf("should not result in error")
   254  	}
   255  
   256  	// Check token1's balance of caller after batch transfer
   257  	if balanceBatch[0] != 6 {
   258  		t.Errorf("expected: (%d), got: (%d)", 6, balanceBatch[0])
   259  	}
   260  
   261  	// Check token1's balance of addr after batch transfer
   262  	if balanceBatch[1] != 4 {
   263  		t.Errorf("expected: (%d), got: (%d)", 4, balanceBatch[1])
   264  	}
   265  
   266  	// Check token2's balance of caller after batch transfer
   267  	if balanceBatch[2] != 40 {
   268  		t.Errorf("expected: (%d), got: (%d)", 40, balanceBatch[2])
   269  	}
   270  
   271  	// Check token2's balance of addr after batch transfer
   272  	if balanceBatch[3] != 60 {
   273  		t.Errorf("expected: (%d), got: (%d)", 60, balanceBatch[3])
   274  	}
   275  }
   276  
   277  func TestSafeMint(t *testing.T) {
   278  	dummy := NewBasicGRC1155Token(dummyURI)
   279  	if dummy == nil {
   280  		t.Errorf("should not be nil")
   281  	}
   282  
   283  	addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
   284  	addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
   285  
   286  	tid1 := TokenID("1")
   287  	tid2 := TokenID("2")
   288  
   289  	err := dummy.SafeMint(zeroAddress, tid1, 100)
   290  	if err == nil {
   291  		t.Errorf("should result in error")
   292  	}
   293  	err = dummy.SafeMint(addr1, tid1, 100)
   294  	if err != nil {
   295  		t.Errorf("should not result in error")
   296  	}
   297  	err = dummy.SafeMint(addr1, tid2, 200)
   298  	if err != nil {
   299  		t.Errorf("should not result in error")
   300  	}
   301  	err = dummy.SafeMint(addr2, tid1, 50)
   302  	if err != nil {
   303  		t.Errorf("should not result in error")
   304  	}
   305  
   306  	balanceBatch, err := dummy.BalanceOfBatch([]std.Address{addr1, addr2, addr1}, []TokenID{tid1, tid1, tid2})
   307  	if err != nil {
   308  		t.Errorf("should not result in error")
   309  	}
   310  	// Check token1's balance of addr1 after mint
   311  	if balanceBatch[0] != 100 {
   312  		t.Errorf("expected: (%d), got: (%d)", 100, balanceBatch[0])
   313  	}
   314  	// Check token1's balance of addr2 after mint
   315  	if balanceBatch[1] != 50 {
   316  		t.Errorf("expected: (%d), got: (%d)", 50, balanceBatch[1])
   317  	}
   318  	// Check token2's balance of addr1 after mint
   319  	if balanceBatch[2] != 200 {
   320  		t.Errorf("expected: (%d), got: (%d)", 200, balanceBatch[2])
   321  	}
   322  }
   323  
   324  func TestSafeBatchMint(t *testing.T) {
   325  	dummy := NewBasicGRC1155Token(dummyURI)
   326  	if dummy == nil {
   327  		t.Errorf("should not be nil")
   328  	}
   329  
   330  	addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
   331  	addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
   332  
   333  	tid1 := TokenID("1")
   334  	tid2 := TokenID("2")
   335  
   336  	err := dummy.SafeBatchMint(zeroAddress, []TokenID{tid1, tid2}, []uint64{100, 200})
   337  	if err == nil {
   338  		t.Errorf("should result in error")
   339  	}
   340  	err = dummy.SafeBatchMint(addr1, []TokenID{tid1, tid2}, []uint64{100, 200})
   341  	if err != nil {
   342  		t.Errorf("should not result in error")
   343  	}
   344  	err = dummy.SafeBatchMint(addr2, []TokenID{tid1, tid2}, []uint64{300, 400})
   345  	if err != nil {
   346  		t.Errorf("should not result in error")
   347  	}
   348  
   349  	balanceBatch, err := dummy.BalanceOfBatch([]std.Address{addr1, addr2, addr1, addr2}, []TokenID{tid1, tid1, tid2, tid2})
   350  	if err != nil {
   351  		t.Errorf("should not result in error")
   352  	}
   353  	// Check token1's balance of addr1 after batch mint
   354  	if balanceBatch[0] != 100 {
   355  		t.Errorf("expected: (%d), got: (%d)", 100, balanceBatch[0])
   356  	}
   357  	// Check token1's balance of addr2 after batch mint
   358  	if balanceBatch[1] != 300 {
   359  		t.Errorf("expected: (%d), got: (%d)", 300, balanceBatch[1])
   360  	}
   361  	// Check token2's balance of addr1 after batch mint
   362  	if balanceBatch[2] != 200 {
   363  		t.Errorf("expected: (%d), got: (%d)", 200, balanceBatch[2])
   364  	}
   365  	// Check token2's balance of addr2 after batch mint
   366  	if balanceBatch[3] != 400 {
   367  		t.Errorf("expected: (%d), got: (%d)", 400, balanceBatch[3])
   368  	}
   369  }
   370  
   371  func TestBurn(t *testing.T) {
   372  	dummy := NewBasicGRC1155Token(dummyURI)
   373  	if dummy == nil {
   374  		t.Errorf("should not be nil")
   375  	}
   376  
   377  	addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
   378  
   379  	tid1 := TokenID("1")
   380  	tid2 := TokenID("2")
   381  
   382  	dummy.mintBatch(addr, []TokenID{tid1, tid2}, []uint64{100, 200})
   383  	err := dummy.Burn(zeroAddress, tid1, 60)
   384  	if err == nil {
   385  		t.Errorf("should result in error")
   386  	}
   387  	err = dummy.Burn(addr, tid1, 160)
   388  	if err == nil {
   389  		t.Errorf("should result in error")
   390  	}
   391  	err = dummy.Burn(addr, tid1, 60)
   392  	if err != nil {
   393  		t.Errorf("should not result in error")
   394  	}
   395  	err = dummy.Burn(addr, tid2, 60)
   396  	if err != nil {
   397  		t.Errorf("should not result in error")
   398  	}
   399  	balanceBatch, err := dummy.BalanceOfBatch([]std.Address{addr, addr}, []TokenID{tid1, tid2})
   400  	if err != nil {
   401  		t.Errorf("should not result in error")
   402  	}
   403  
   404  	// Check token1's balance of addr after burn
   405  	if balanceBatch[0] != 40 {
   406  		t.Errorf("expected: (%d), got: (%d)", 40, balanceBatch[0])
   407  	}
   408  	// Check token2's balance of addr after burn
   409  	if balanceBatch[1] != 140 {
   410  		t.Errorf("expected: (%d), got: (%d)", 140, balanceBatch[1])
   411  	}
   412  }
   413  
   414  func TestBatchBurn(t *testing.T) {
   415  	dummy := NewBasicGRC1155Token(dummyURI)
   416  	if dummy == nil {
   417  		t.Errorf("should not be nil")
   418  	}
   419  
   420  	addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm")
   421  
   422  	tid1 := TokenID("1")
   423  	tid2 := TokenID("2")
   424  
   425  	dummy.mintBatch(addr, []TokenID{tid1, tid2}, []uint64{100, 200})
   426  	err := dummy.BatchBurn(zeroAddress, []TokenID{tid1, tid2}, []uint64{60, 60})
   427  	if err == nil {
   428  		t.Errorf("should result in error")
   429  	}
   430  	err = dummy.BatchBurn(addr, []TokenID{tid1, tid2}, []uint64{160, 60})
   431  	if err == nil {
   432  		t.Errorf("should result in error")
   433  	}
   434  	err = dummy.BatchBurn(addr, []TokenID{tid1, tid2}, []uint64{60, 60})
   435  	if err != nil {
   436  		t.Errorf("should not result in error")
   437  	}
   438  	balanceBatch, err := dummy.BalanceOfBatch([]std.Address{addr, addr}, []TokenID{tid1, tid2})
   439  	if err != nil {
   440  		t.Errorf("should not result in error")
   441  	}
   442  
   443  	// Check token1's balance of addr after batch burn
   444  	if balanceBatch[0] != 40 {
   445  		t.Errorf("expected: (%d), got: (%d)", 40, balanceBatch[0])
   446  	}
   447  	// Check token2's balance of addr after batch burn
   448  	if balanceBatch[1] != 140 {
   449  		t.Errorf("expected: (%d), got: (%d)", 140, balanceBatch[1])
   450  	}
   451  }