From 46024542b2311230c1782501b1cc2237745bbf6e Mon Sep 17 00:00:00 2001 From: Austin Kline Date: Mon, 8 Jan 2024 16:38:25 -0800 Subject: [PATCH] add tests for attempting to send nfts and fts --- coverage.lcov | 423 +++++++++--------- .../lost-and-found/get_redeemable_types.cdc | 5 + tests/LostAndFound_tests.cdc | 77 +++- tests/test_helpers.cdc | 2 +- .../example-nft/try_send_example_nft.cdc | 3 +- 5 files changed, 286 insertions(+), 224 deletions(-) create mode 100644 scripts/lost-and-found/get_redeemable_types.cdc diff --git a/coverage.lcov b/coverage.lcov index 2c88c08..73688e6 100644 --- a/coverage.lcov +++ b/coverage.lcov @@ -1,41 +1,41 @@ TN: SF:A.0000000000000005.FeeEstimator -DA:20,1 -DA:21,1 -DA:25,1 -DA:26,1 -DA:31,1 -DA:34,1 +DA:20,8 +DA:21,8 +DA:25,8 +DA:26,8 +DA:31,8 +DA:34,8 DA:39,0 -DA:45,1 -DA:46,1 -DA:47,1 -DA:49,1 -DA:50,1 -DA:51,1 -DA:52,1 -DA:53,1 -DA:57,1 -DA:58,1 +DA:45,8 +DA:46,8 +DA:47,8 +DA:49,8 +DA:50,8 +DA:51,8 +DA:52,8 +DA:53,8 +DA:57,20 +DA:58,20 LF:17 LH:16 end_of_record TN: SF:A.0000000000000005.LostAndFound -DA:58,1 -DA:59,1 -DA:63,1 -DA:64,1 -DA:69,1 -DA:72,1 -DA:99,1 -DA:100,1 -DA:101,1 -DA:102,1 -DA:103,1 -DA:104,1 -DA:106,1 -DA:110,0 +DA:58,8 +DA:59,8 +DA:63,8 +DA:64,8 +DA:69,8 +DA:72,8 +DA:99,12 +DA:100,12 +DA:101,12 +DA:102,12 +DA:103,12 +DA:104,12 +DA:106,12 +DA:110,8 DA:114,0 DA:119,0 DA:124,0 @@ -73,62 +73,62 @@ DA:168,0 DA:169,0 DA:170,0 DA:172,0 -DA:179,1 -DA:180,1 -DA:181,1 -DA:187,1 -DA:188,1 -DA:191,1 -DA:192,1 -DA:207,0 -DA:208,0 -DA:209,0 +DA:179,8 +DA:180,8 +DA:181,8 +DA:187,8 +DA:188,8 +DA:191,8 +DA:192,8 +DA:207,4 +DA:208,4 +DA:209,4 DA:213,0 DA:217,0 DA:218,0 DA:219,0 DA:220,0 DA:223,0 -DA:231,0 -DA:232,0 -DA:235,0 -DA:236,0 -DA:237,0 -DA:239,0 -DA:240,0 -DA:241,0 -DA:243,0 -DA:244,0 +DA:231,4 +DA:232,4 +DA:235,4 +DA:236,4 +DA:237,4 +DA:239,4 +DA:240,4 +DA:241,4 +DA:243,4 +DA:244,4 DA:248,0 DA:252,0 DA:253,0 DA:257,0 DA:258,0 -DA:272,0 -DA:273,0 -DA:274,0 -DA:275,0 +DA:272,4 +DA:273,4 +DA:274,4 +DA:275,4 DA:279,0 -DA:283,0 -DA:284,0 -DA:285,0 -DA:286,0 -DA:287,0 -DA:290,0 +DA:283,1 +DA:284,1 +DA:285,1 +DA:286,1 +DA:287,1 +DA:290,1 DA:294,0 -DA:298,0 -DA:302,0 -DA:303,0 -DA:304,0 -DA:305,0 -DA:306,0 -DA:307,0 -DA:308,0 -DA:309,0 -DA:312,0 -DA:317,0 -DA:318,0 -DA:319,0 +DA:298,1 +DA:302,8 +DA:303,4 +DA:304,4 +DA:305,4 +DA:306,4 +DA:307,4 +DA:308,4 +DA:309,4 +DA:312,8 +DA:317,4 +DA:318,4 +DA:319,4 DA:330,0 DA:331,0 DA:334,0 @@ -164,37 +164,37 @@ DA:383,0 DA:384,0 DA:389,0 DA:397,1 -DA:401,0 -DA:402,0 -DA:403,0 -DA:404,0 -DA:405,0 -DA:407,0 -DA:408,0 -DA:411,0 -DA:423,0 -DA:424,0 -DA:426,0 -DA:431,0 -DA:432,0 -DA:433,0 -DA:434,0 -DA:437,0 -DA:438,0 -DA:439,0 -DA:440,0 -DA:443,0 -DA:444,0 -DA:445,0 -DA:446,0 -DA:447,0 -DA:448,0 -DA:449,0 -DA:450,0 -DA:452,0 -DA:453,0 -DA:454,0 -DA:458,1 +DA:401,4 +DA:402,4 +DA:403,4 +DA:404,4 +DA:405,4 +DA:407,4 +DA:408,4 +DA:411,4 +DA:423,4 +DA:424,4 +DA:426,4 +DA:431,4 +DA:432,4 +DA:433,4 +DA:434,4 +DA:437,4 +DA:438,4 +DA:439,4 +DA:440,4 +DA:443,4 +DA:444,4 +DA:445,4 +DA:446,4 +DA:447,4 +DA:448,4 +DA:449,4 +DA:450,4 +DA:452,4 +DA:453,4 +DA:454,4 +DA:458,9 DA:465,0 DA:466,0 DA:467,0 @@ -262,7 +262,7 @@ DA:601,0 DA:606,0 DA:607,0 DA:608,0 -DA:612,1 +DA:612,13 DA:616,0 DA:617,0 DA:618,0 @@ -288,44 +288,44 @@ DA:653,0 DA:654,0 DA:655,0 DA:656,0 -DA:667,1 -DA:668,1 -DA:669,1 -DA:670,1 -DA:671,1 -DA:672,1 -DA:673,1 -DA:675,0 -DA:676,0 +DA:667,8 +DA:668,8 +DA:669,8 +DA:670,8 +DA:671,8 +DA:672,7 +DA:673,7 +DA:675,1 +DA:676,1 DA:677,0 -DA:681,1 -DA:682,1 -DA:683,1 -DA:684,1 -DA:685,1 -DA:686,1 -DA:688,1 -DA:689,1 -DA:690,1 -DA:694,0 -DA:695,0 -DA:696,0 +DA:681,8 +DA:682,8 +DA:683,8 +DA:684,8 +DA:685,8 +DA:686,8 +DA:688,8 +DA:689,8 +DA:690,8 +DA:694,1 +DA:695,1 +DA:696,1 DA:697,0 -DA:700,0 -DA:712,0 -DA:713,0 -DA:716,0 -DA:717,0 -DA:728,0 -DA:729,0 -DA:730,0 -DA:731,0 +DA:700,1 +DA:712,4 +DA:713,4 +DA:716,4 +DA:717,4 +DA:728,6 +DA:729,1 +DA:730,1 +DA:731,5 DA:732,0 DA:733,0 -DA:734,0 -DA:735,0 -DA:736,0 -DA:738,0 +DA:734,5 +DA:735,1 +DA:736,1 +DA:738,4 DA:743,0 DA:747,1 DA:749,1 @@ -336,19 +336,19 @@ DA:754,1 DA:755,1 DA:756,1 LF:313 -LH:47 +LH:132 end_of_record TN: SF:A.0000000000000006.ExampleNFT -DA:48,1 -DA:49,1 -DA:50,1 -DA:51,1 -DA:52,1 +DA:48,6 +DA:49,6 +DA:50,6 +DA:51,6 +DA:52,6 DA:56,0 DA:60,0 -DA:73,0 -DA:75,0 +DA:73,4 +DA:75,4 DA:85,0 DA:86,0 DA:87,0 @@ -364,17 +364,17 @@ DA:131,0 DA:136,0 DA:138,0 DA:148,0 -DA:160,2 -DA:161,2 -DA:166,1 -DA:168,1 -DA:170,1 -DA:176,2 -DA:178,2 -DA:181,2 -DA:183,2 -DA:185,2 -DA:190,1 +DA:160,4 +DA:161,4 +DA:166,2 +DA:168,2 +DA:170,2 +DA:176,5 +DA:178,5 +DA:181,5 +DA:183,5 +DA:185,5 +DA:190,3 DA:196,0 DA:200,0 DA:202,0 @@ -385,70 +385,73 @@ DA:211,0 DA:212,0 DA:216,0 DA:217,0 -DA:223,1 -DA:239,1 -DA:240,1 -DA:251,1 -DA:252,1 -DA:254,1 -DA:263,1 -DA:276,0 -DA:277,0 -DA:279,0 -DA:280,0 -DA:281,0 -DA:282,0 -DA:283,0 -DA:284,0 -DA:287,0 -DA:290,0 +DA:223,3 +DA:239,2 +DA:240,2 +DA:248,4 +DA:249,4 +DA:257,4 +DA:268,2 +DA:269,2 +DA:271,2 +DA:280,2 +DA:293,0 +DA:294,0 +DA:296,0 +DA:297,0 +DA:298,0 DA:299,0 -DA:309,0 -DA:311,0 -DA:319,0 -DA:323,0 -DA:329,0 +DA:300,0 +DA:301,0 +DA:304,0 +DA:307,0 +DA:316,0 +DA:326,0 +DA:328,0 +DA:336,0 DA:340,0 -DA:349,0 -DA:357,1 -DA:360,1 -DA:361,1 -DA:362,1 -DA:363,1 -DA:366,1 -DA:367,1 -DA:370,1 -DA:376,1 +DA:346,0 +DA:357,0 +DA:366,0 +DA:374,1 DA:377,1 DA:378,1 +DA:379,1 DA:380,1 -LF:82 -LH:35 +DA:383,1 +DA:384,1 +DA:387,1 +DA:393,1 +DA:394,1 +DA:395,1 +DA:397,1 +LF:85 +LH:40 end_of_record TN: SF:A.0000000000000007.ExampleToken -DA:62,1 +DA:62,4 DA:76,0 DA:77,0 DA:78,0 -DA:91,0 -DA:92,0 -DA:93,0 -DA:94,0 -DA:95,0 -DA:99,0 -DA:111,0 -DA:121,0 -DA:122,0 +DA:91,1 +DA:92,1 +DA:93,1 +DA:94,1 +DA:95,1 +DA:99,1 +DA:111,1 +DA:121,2 +DA:122,2 DA:130,0 DA:131,0 -DA:151,0 -DA:152,0 -DA:154,0 -DA:155,0 -DA:156,0 -DA:157,0 -DA:161,0 +DA:151,2 +DA:152,2 +DA:154,2 +DA:155,2 +DA:156,2 +DA:157,2 +DA:161,2 DA:179,0 DA:180,0 DA:181,0 @@ -462,5 +465,5 @@ DA:210,1 DA:211,1 DA:215,1 LF:34 -LH:9 +LH:25 end_of_record diff --git a/scripts/lost-and-found/get_redeemable_types.cdc b/scripts/lost-and-found/get_redeemable_types.cdc new file mode 100644 index 0000000..58b7170 --- /dev/null +++ b/scripts/lost-and-found/get_redeemable_types.cdc @@ -0,0 +1,5 @@ +import "LostAndFound" + +pub fun main(addr: Address): [Type] { + return LostAndFound.getRedeemableTypes(addr) +} \ No newline at end of file diff --git a/tests/LostAndFound_tests.cdc b/tests/LostAndFound_tests.cdc index 5a2901e..dfcc744 100644 --- a/tests/LostAndFound_tests.cdc +++ b/tests/LostAndFound_tests.cdc @@ -2,6 +2,8 @@ import Test import "test_helpers.cdc" import "LostAndFound" +import "ExampleNFT" +import "ExampleToken" pub fun setup() { deployAll() @@ -23,26 +25,69 @@ pub fun testEstimateDeposit() { pub fun testDepositNft() { let acct = getNewAccount() - mintAndSendNft(acct: acct) + mintAndSendNft(acct) let event = Test.eventsOfType(Type()).removeLast() as! LostAndFound.TicketDeposited Test.assertEqual(event.redeemer, acct.address) Test.assertEqual(exampleNftIdentifier(), event.type.identifier) - // estimate depositing another to see that the fee is reduced (no shelf initialization) + // estimate depositing another to see that the fee is reduced (we have initialized the shelf and bin for this user and resource type pairing) + setupExampleNft(acct: acct) let id = mintExampleNfts(acct, 1)[0] let estimate = scriptExecutor("lost-and-found/estimate_deposit_nft.cdc", [acct.address, id, exampleNftStoragePath])! as! UFix64 - Test.assert(estimate >= 0.00001, message: "fee is lower than expected") - Test.assert(estimate < 0.00002, message: "fee is higher than expected") + Test.assert(estimate == 0.0, message: "fee is higher than expected") +} + +pub fun testGetRedeemableTypes() { + let acct = getNewAccount() + mintAndSendNft(acct) + + let types = scriptExecutor("lost-and-found/get_redeemable_types.cdc", [acct.address])! as! [Type] + let identifiers: [String] = [] + for t in types { + identifiers.append(t.identifier) + } + + Test.assert(identifiers.contains(exampleNftIdentifier()), message: "example nft type not found") +} + +pub fun testTrySendNftResource_ValidCapability() { + let acct = getNewAccount() + setupExampleNft(acct: acct) + + trySendNft(acct) + + let event = Test.eventsOfType(Type()).removeLast() as! ExampleNFT.Deposit + Test.assertEqual(acct.address, event.to!) +} + +pub fun testTrySendNftResource_InvalidCapability() { + let acct = getNewAccount() + trySendNft(acct) + + let event = Test.eventsOfType(Type()).removeLast() as! LostAndFound.TicketDeposited + Test.assertEqual(event.redeemer, acct.address) + Test.assertEqual(exampleNftIdentifier(), event.type.identifier) +} + +pub fun testTrySendFtResource_ValidCapability() { + let acct = getNewAccount() + setupExampleToken(acct: acct) + + trySendFt(acct, 1.0) + let event = Test.eventsOfType(Type()).removeLast() as! ExampleToken.TokensDeposited + Test.assertEqual(acct.address, event.to!) +} + +pub fun testTrySendFtResource_InvalidCapability() { + let acct = getNewAccount() + + trySendFt(acct, 1.0) + let event = Test.eventsOfType(Type()).removeLast() as! LostAndFound.TicketDeposited + Test.assertEqual(event.redeemer, acct.address) + Test.assertEqual(exampleTokenIdentifier(), event.type.identifier) } -// TODO: getRedeemableTypes -// TODO: deposit invalid payment type -// TODO: try send resource (NFT) - valid collection public capability -// TODO: try send resource (NFT) - invalid collection public capability -// TODO: try send resource (NFT) - valid receiver capability -// TODO: try send resource (NFT) - invalid receiver capability -// TODO: try send resource (FT) - valid receiver capability // TODO: send non nft/ft resource // TODO: getAddress // TODO: redeemAll - nft @@ -52,6 +97,14 @@ pub fun testDepositNft() { // TODO: borrowAllTicketsByType - ft // TODO: create depositor -pub fun mintAndSendNft(acct: Test.Account) { +pub fun mintAndSendNft(_ acct: Test.Account) { txExecutor("example-nft/mint_and_deposit_example_nft.cdc", [exampleNftAccount], [acct.address]) +} + +pub fun trySendNft(_ acct: Test.Account) { + txExecutor("example-nft/try_send_example_nft.cdc", [exampleNftAccount], [acct.address]) +} + +pub fun trySendFt(_ acct: Test.Account, _ amount: UFix64) { + txExecutor("example-token/try_send_example_token.cdc", [exampleTokenAccount], [acct.address, amount]) } \ No newline at end of file diff --git a/tests/test_helpers.cdc b/tests/test_helpers.cdc index 7c6b01a..5fa874a 100644 --- a/tests/test_helpers.cdc +++ b/tests/test_helpers.cdc @@ -138,7 +138,7 @@ pub fun mintExampleTokens(_ acct: Test.Account, _ amount: UFix64) { } pub fun mintFlow(_ receiver: Test.Account, _ amount: UFix64) { - let code = Test.readFile("../transactions/flow/mint_flow.cdc ") + let code = loadCode("flow/mint_flow.cdc", "transactions") let tx = Test.Transaction( code: code, authorizers: [Test.serviceAccount().address], diff --git a/transactions/example-nft/try_send_example_nft.cdc b/transactions/example-nft/try_send_example_nft.cdc index 8bd7b20..16973c0 100644 --- a/transactions/example-nft/try_send_example_nft.cdc +++ b/transactions/example-nft/try_send_example_nft.cdc @@ -34,7 +34,8 @@ transaction(recipient: Address) { execute { let exampleNFTReceiver = getAccount(recipient).getCapability<&{NonFungibleToken.CollectionPublic}>(ExampleNFT.CollectionPublicPath) - let token <- self.minter.mintAndReturnNFT(name: "testname", description: "descr", thumbnail: "image.html", royalties: []) as! @ExampleNFT.NFT + + let token <- self.minter.mint(name: "testname", description: "descr", thumbnail: "image.html") let display = token.resolveView(Type()) as! MetadataViews.Display? let memo = "test memo"