Skip to content

Commit 0609918

Browse files
Fix order cancellation logic and tests (#277)
* Fix order cancellation logic and tests * Increment package version
1 parent 80390f5 commit 0609918

File tree

3 files changed

+186
-105
lines changed

3 files changed

+186
-105
lines changed

Assets/SequenceSDK/Marketplace/MarketplaceCheckoutTests.cs

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -615,66 +615,74 @@ private async Task<CollectibleOrder[]> FetchOffers(string collection, Chain chai
615615
public async Task TestCancelOrder()
616616
{
617617
EndToEndTestHarness testHarness = new EndToEndTestHarness();
618-
Address erc20UniversallyMintable = new Address("0x88e57238a23e2619fd42f479d546560b44c698fe");
619618
Address collection = new Address("0x079294e6ffec16234578c672fa3fbfd4b6c48640");
620619
bool cancelled = false;
621620

622-
testHarness.Login(async wallet =>
621+
WaaSEndToEndTestConfig config = WaaSEndToEndTestConfig.GetConfig();
622+
623+
string email = config.PlayFabEmail;
624+
var request = new LoginWithEmailAddressRequest { Email = email, Password = config.PlayFabPassword };
625+
PlayFabClientAPI.LoginWithEmailAddress(request, result =>
623626
{
624-
await SeedWalletAndCreateListing(wallet, collection, erc20UniversallyMintable);
625-
626-
Order myOrder = null;
627-
int retries = 5;
628-
for (int i = 0; i < retries; i++)
627+
testHarness.LoginWithPlayFab(result, email, async wallet =>
629628
{
630-
CollectibleOrder[] collectibleOrders = await FetchListings(collection, _chain,
631-
new CollectiblesFilter(true, "", null, null, new string[] {wallet.GetWalletAddress()}));
632-
Assert.Greater(collectibleOrders.Length, 0);
633-
634-
myOrder = FindOrderCreatedByWallet(wallet.GetWalletAddress(), collectibleOrders);
635-
if (myOrder != null)
629+
Order myOrder = null;
630+
int retries = 5;
631+
for (int i = 0; i < retries; i++)
636632
{
637-
break;
633+
CollectibleOrder[] collectibleOrders = await FetchListings(collection, _chain,
634+
new CollectiblesFilter(true, "", ordersCreatedBy: new string[] {wallet.GetWalletAddress()}));
635+
Assert.Greater(collectibleOrders.Length, 0);
636+
637+
myOrder = FindOrderCreatedByWallet(wallet.GetWalletAddress(), collectibleOrders);
638+
if (myOrder != null)
639+
{
640+
break;
641+
}
642+
else
643+
{
644+
Debug.Log($"No order created by {wallet.GetWalletAddress()} found. Retrying {i + 1}...");
645+
}
638646
}
639-
else
647+
648+
if (myOrder == null)
640649
{
641-
Debug.Log($"No order created by {wallet.GetWalletAddress()} found. Retrying {i + 1}...");
650+
Assert.Fail($"Failed to find order created by wallet after {retries} retries. Try running {nameof(MarketplaceSeeder.SeedMarketplace_Playfab)} and seeding the marketplace with orders from this wallet first");
642651
}
643-
}
644-
645-
if (myOrder == null)
646-
{
647-
Assert.Fail($"Failed to find order created by wallet after {retries} retries");
648-
}
649652

650-
Checkout checkout = new Checkout(wallet, _chain);
651-
Step[] steps = await checkout.GenerateCancelTransaction(collection, myOrder);
652-
await SubmitStepsAsTransaction(steps, wallet);
653-
654-
for (int i = 0; i < retries; i++)
655-
{
656-
CollectibleOrder[] collectibleOrders = await FetchListings(collection, _chain,
657-
new CollectiblesFilter(true, "", null, null, new string[] {wallet.GetWalletAddress()}));
658-
Assert.Greater(collectibleOrders.Length, 0);
653+
Checkout checkout = new Checkout(wallet, _chain);
654+
Step[] steps = await checkout.GenerateCancelTransaction(collection, myOrder);
655+
await SubmitStepsAsTransaction(steps, wallet);
659656

660-
myOrder = FindOrderCreatedByWallet(wallet.GetWalletAddress(), collectibleOrders);
661-
if (myOrder == null)
657+
for (int i = 0; i < retries; i++)
662658
{
663-
break;
659+
CollectibleOrder[] collectibleOrders = await FetchListings(collection, _chain);
660+
Assert.Greater(collectibleOrders.Length, 0);
661+
662+
myOrder = FindOrderCreatedByWallet(wallet.GetWalletAddress(), collectibleOrders);
663+
if (myOrder == null)
664+
{
665+
break;
666+
}
667+
else
668+
{
669+
Debug.Log($"My order {myOrder.orderId} is still found. Retrying {i + 1}...");
670+
}
664671
}
665-
else
672+
673+
if (myOrder != null)
666674
{
667-
Debug.Log($"My order {myOrder.orderId} is still found. Retrying {i + 1}...");
675+
Assert.Fail(
676+
$"Failed to see that order {myOrder.orderId} was cancelled after {retries} retries");
668677
}
669-
}
670-
671-
if (myOrder != null)
672-
{
673-
Assert.Fail($"Failed to see that order {myOrder.orderId} was cancelled after {retries} retries");
674-
}
675678

679+
cancelled = true;
680+
}, (error, method, email, methods) => { Assert.Fail(error); });
681+
}, error =>
682+
{
683+
Assert.Fail(error.ErrorMessage);
676684
cancelled = true;
677-
}, (error, method, email, methods) => { Assert.Fail(error); });
685+
});
678686

679687
while (!cancelled)
680688
{
@@ -687,7 +695,7 @@ private Order FindOrderCreatedByWallet(Address walletAddress, CollectibleOrder[]
687695
foreach (var collectibleOrder in collectibleOrders)
688696
{
689697
Order order = collectibleOrder.order;
690-
if (order.createdBy == walletAddress)
698+
if (order != null && order.createdBy.ToLower() == walletAddress.Value.ToLower())
691699
{
692700
return order;
693701
}

Assets/SequenceSDK/Marketplace/MarketplaceSeeder.cs

Lines changed: 133 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Numerics;
44
using System.Threading.Tasks;
55
using NUnit.Framework;
6+
using PlayFab;
7+
using PlayFab.ClientModels;
68
using Sequence.Contracts;
79
using Sequence.EmbeddedWallet;
810
using Sequence.EmbeddedWallet.Tests;
@@ -17,91 +19,161 @@ public class MarketplaceSeeder
1719

1820
// [Test]
1921
[Timeout(1800000)]
20-
public async Task SeedMarketplace()
22+
public async Task SeedMarketplace_Playfab()
2123
{
2224
throw new Exception("Please only run this when you need to seed the marketplace for testing as it will create a lot of listings and offers, using a fair amount of gas and credits.");
2325
EndToEndTestHarness testHarness = new EndToEndTestHarness();
2426
bool done = false;
25-
testHarness.Login(async wallet =>
26-
{
27-
Address collection = new Address("0x079294e6ffec16234578c672fa3fbfd4b6c48640");
28-
ERC1155 universallyMintableNft = new ERC1155(collection);
29-
30-
Address erc20UniversallyMintable = new Address("0x88e57238a23e2619fd42f479d546560b44c698fe");
31-
ERC20 universallyMintableToken = new ERC20(erc20UniversallyMintable);
32-
33-
TransactionReturn mintResult = await wallet.SendTransaction(_chain, new Transaction[]
34-
{
35-
new RawTransaction(collection, "0",
36-
universallyMintableNft.MintBatch(wallet.GetWalletAddress(), new BigInteger[] {1, 2, 3, 4, 5}, new BigInteger[] {100000, 100000, 100000, 100000, 100000}).CallData),
37-
new RawTransaction(erc20UniversallyMintable, "0",
38-
universallyMintableToken.Mint(wallet.GetWalletAddress(), 1000000000000).CallData)
39-
});
40-
Assert.IsNotNull(mintResult);
41-
Assert.IsTrue(mintResult is SuccessfulTransactionReturn);
4227

43-
List<Step> finalSteps = new List<Step>();
44-
Address USDC = new Address("0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359");
45-
Address[] possibleCurrencyAddresses = new Address[]
46-
{ USDC };
47-
Checkout checkout = new Checkout(wallet, _chain);
48-
for (int i = 0; i < 10; i++)
28+
WaaSEndToEndTestConfig config = WaaSEndToEndTestConfig.GetConfig();
29+
30+
string email = config.PlayFabEmail;
31+
var request = new LoginWithEmailAddressRequest { Email = email, Password = config.PlayFabPassword };
32+
PlayFabClientAPI.LoginWithEmailAddress(request, result =>
33+
{
34+
testHarness.LoginWithPlayFab(result, email, async wallet =>
4935
{
50-
Step[] steps = await checkout.GenerateListingTransaction(collection, (i % 5 + 1).ToString(), i + 1,
51-
ContractType.ERC1155, possibleCurrencyAddresses.GetRandomObjectFromArray(), 1,
52-
DateTime.Now + TimeSpan.FromDays(365));
53-
if (i % 5 == 3)
36+
try
5437
{
55-
await Task.Delay(1000);
38+
await DoSeedMarketplace(wallet);
5639
}
57-
foreach (var step in steps)
40+
catch (Exception e)
5841
{
59-
finalSteps.Add(step);
42+
Assert.Fail(e.Message);
6043
}
61-
}
62-
63-
for (int i = 0; i < 10; i++)
64-
{
65-
Step[] steps = await checkout.GenerateOfferTransaction(collection, (i % 5 + 1).ToString(), i + 1,
66-
ContractType.ERC1155, possibleCurrencyAddresses.GetRandomObjectFromArray(), 1,
67-
DateTime.Now + TimeSpan.FromDays(365));
68-
if (i % 5 == 3)
44+
finally
6945
{
70-
await Task.Delay(1000);
71-
}
72-
foreach (var step in steps)
73-
{
74-
finalSteps.Add(step);
46+
done = true;
7547
}
48+
}, (error, method, email, methods) =>
49+
{
50+
Assert.Fail(error);
51+
done = true;
52+
});
53+
}, error =>
54+
{
55+
Assert.Fail(error.ErrorMessage);
56+
done = true;
57+
});
58+
59+
60+
while (!done)
61+
{
62+
await Task.Yield();
63+
}
64+
}
65+
66+
// [Test]
67+
[Timeout(1800000)]
68+
public async Task SeedMarketplace_Guest()
69+
{
70+
throw new Exception("Please only run this when you need to seed the marketplace for testing as it will create a lot of listings and offers, using a fair amount of gas and credits.");
71+
EndToEndTestHarness testHarness = new EndToEndTestHarness();
72+
bool done = false;
73+
74+
testHarness.Login(async wallet =>
75+
{
76+
try
77+
{
78+
await DoSeedMarketplace(wallet);
7679
}
77-
78-
Transaction[] transactions = new Transaction[10];
79-
for (int j = 0; j < finalSteps.Count; j+=10)
80+
catch (Exception e)
8081
{
81-
for (int i = 0; i < 10; i++)
82-
{
83-
transactions[i] = new RawTransaction(finalSteps[j+i].to, finalSteps[j+i].value, finalSteps[j+i].data);
84-
}
85-
TransactionReturn result = await wallet.SendTransaction(_chain, transactions);
86-
Assert.IsNotNull(result);
87-
// Assert.IsTrue(result is SuccessfulTransactionReturn);
88-
if (result is SuccessfulTransactionReturn success)
89-
{
90-
Debug.Log(ChainDictionaries.BlockExplorerOf[_chain].AppendTrailingSlashIfNeeded() + "tx/" + success.txHash);
91-
// Application.OpenURL(ChainDictionaries.BlockExplorerOf[_chain].AppendTrailingSlashIfNeeded() + "tx/" + success.txHash);
92-
}
82+
Assert.Fail(e.Message);
83+
}
84+
finally
85+
{
86+
done = true;
9387
}
94-
95-
done = true;
9688
}, (error, method, email, methods) =>
9789
{
9890
Assert.Fail(error);
9991
done = true;
10092
});
93+
10194
while (!done)
10295
{
10396
await Task.Yield();
10497
}
10598
}
99+
100+
private async Task DoSeedMarketplace(IWallet wallet)
101+
{
102+
Address collection = new Address("0x079294e6ffec16234578c672fa3fbfd4b6c48640");
103+
ERC1155 universallyMintableNft = new ERC1155(collection);
104+
105+
Address erc20UniversallyMintable = new Address("0x88e57238a23e2619fd42f479d546560b44c698fe");
106+
ERC20 universallyMintableToken = new ERC20(erc20UniversallyMintable);
107+
108+
TransactionReturn mintResult = await wallet.SendTransaction(_chain, new Transaction[]
109+
{
110+
new RawTransaction(collection, "0",
111+
universallyMintableNft.MintBatch(wallet.GetWalletAddress(),
112+
new BigInteger[] { 1, 2, 3, 4, 5 },
113+
new BigInteger[] { 100000, 100000, 100000, 100000, 100000 }).CallData),
114+
new RawTransaction(erc20UniversallyMintable, "0",
115+
universallyMintableToken.Mint(wallet.GetWalletAddress(), 1000000000000).CallData)
116+
});
117+
Assert.IsNotNull(mintResult);
118+
Assert.IsTrue(mintResult is SuccessfulTransactionReturn);
119+
120+
List<Step> finalSteps = new List<Step>();
121+
Address USDC = new Address("0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359");
122+
Address[] possibleCurrencyAddresses = new Address[] { USDC };
123+
Checkout checkout = new Checkout(wallet, _chain);
124+
for (int i = 0; i < 10; i++)
125+
{
126+
Step[] steps = await checkout.GenerateListingTransaction(collection, (i % 5 + 1).ToString(),
127+
i + 1,
128+
ContractType.ERC1155, possibleCurrencyAddresses.GetRandomObjectFromArray(), 1,
129+
DateTime.Now + TimeSpan.FromDays(365));
130+
if (i % 5 == 3)
131+
{
132+
await Task.Delay(1000);
133+
}
134+
135+
foreach (var step in steps)
136+
{
137+
finalSteps.Add(step);
138+
}
139+
}
140+
141+
for (int i = 0; i < 10; i++)
142+
{
143+
Step[] steps = await checkout.GenerateOfferTransaction(collection, (i % 5 + 1).ToString(),
144+
i + 1,
145+
ContractType.ERC1155, possibleCurrencyAddresses.GetRandomObjectFromArray(), 1,
146+
DateTime.Now + TimeSpan.FromDays(365));
147+
if (i % 5 == 3)
148+
{
149+
await Task.Delay(1000);
150+
}
151+
152+
foreach (var step in steps)
153+
{
154+
finalSteps.Add(step);
155+
}
156+
}
157+
158+
Transaction[] transactions = new Transaction[10];
159+
for (int j = 0; j < finalSteps.Count; j += 10)
160+
{
161+
for (int i = 0; i < 10; i++)
162+
{
163+
transactions[i] = new RawTransaction(finalSteps[j + i].to, finalSteps[j + i].value,
164+
finalSteps[j + i].data);
165+
}
166+
167+
TransactionReturn result = await wallet.SendTransaction(_chain, transactions);
168+
Assert.IsNotNull(result);
169+
// Assert.IsTrue(result is SuccessfulTransactionReturn);
170+
if (result is SuccessfulTransactionReturn success)
171+
{
172+
Debug.Log(ChainDictionaries.BlockExplorerOf[_chain].AppendTrailingSlashIfNeeded() + "tx/" +
173+
success.txHash);
174+
// Application.OpenURL(ChainDictionaries.BlockExplorerOf[_chain].AppendTrailingSlashIfNeeded() + "tx/" + success.txHash);
175+
}
176+
}
177+
}
106178
}
107179
}

Packages/Sequence-Unity/Sequence/SequenceSDK/Marketplace/DataTypes/StepType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public enum StepType
1414
createOffer,
1515
signEIP712,
1616
signEIP191,
17+
cancel
1718
}
1819
}

0 commit comments

Comments
 (0)