Skip to content

Commit 4e914aa

Browse files
committed
Direct Listings Tests
1 parent 571c8cb commit 4e914aa

File tree

3 files changed

+215
-5
lines changed

3 files changed

+215
-5
lines changed

Thirdweb.Tests/Thirdweb.Extensions/Thirdweb.MarketplaceExtensions.Tests.cs

Lines changed: 162 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,27 @@ namespace Thirdweb.Tests.Extensions;
44

55
public class MarketplaceExtensionsTests : BaseTests
66
{
7-
private readonly string _marketplaceContractAddress = "0xc9671F631E8313D53ec0b5358e1a499c574fCe6A";
7+
private readonly string _marketplaceContractAddress = "0xb80E83b73571e63b3581b68f20bFC9E97965F329";
8+
private readonly string _drop1155ContractAddress = "0x37116cAe5e152C1A7345AAB0EC286Ff6E97c0605";
89

910
private readonly BigInteger _chainId = 421614;
1011

1112
public MarketplaceExtensionsTests(ITestOutputHelper output)
1213
: base(output) { }
1314

14-
private async Task<IThirdwebWallet> GetSmartWallet()
15+
private async Task<IThirdwebWallet> GetSmartWallet(int claimAmount)
1516
{
1617
var privateKeyWallet = await PrivateKeyWallet.Generate(this.Client);
17-
return await SmartWallet.Create(personalWallet: privateKeyWallet, chainId: 421614);
18+
var smartWallet = await SmartWallet.Create(personalWallet: privateKeyWallet, chainId: 421614);
19+
20+
if (claimAmount > 0)
21+
{
22+
var drop1155Contract = await ThirdwebContract.Create(this.Client, this._drop1155ContractAddress, this._chainId);
23+
var tokenId = 0;
24+
_ = await drop1155Contract.DropERC1155_Claim(smartWallet, await smartWallet.GetAddress(), tokenId, claimAmount);
25+
}
26+
27+
return smartWallet;
1828
}
1929

2030
private async Task<ThirdwebContract> GetMarketplaceContract()
@@ -24,6 +34,155 @@ private async Task<ThirdwebContract> GetMarketplaceContract()
2434

2535
#region IDirectListings
2636

37+
[Fact(Timeout = 120000)]
38+
public async Task Marketplace_DirectListings_CreateListing_Success()
39+
{
40+
var contract = await this.GetMarketplaceContract();
41+
var wallet = await this.GetSmartWallet(1);
42+
43+
var listingParams = new ListingParameters()
44+
{
45+
AssetContract = this._drop1155ContractAddress,
46+
TokenId = 0,
47+
Quantity = 1,
48+
Currency = Constants.NATIVE_TOKEN_ADDRESS,
49+
PricePerToken = 1,
50+
StartTimestamp = Utils.GetUnixTimeStampNow(),
51+
EndTimestamp = Utils.GetUnixTimeStampNow() + 3600,
52+
Reserved = false
53+
};
54+
55+
var receipt = await contract.Marketplace_DirectListings_CreateListing(wallet, listingParams, true);
56+
57+
Assert.NotNull(receipt);
58+
Assert.True(receipt.TransactionHash.Length == 66);
59+
60+
var listingId = await contract.Marketplace_DirectListings_TotalListings() - 1;
61+
var listing = await contract.Marketplace_DirectListings_GetListing(listingId);
62+
63+
Assert.NotNull(listing);
64+
Assert.Equal(listing.ListingId, listingId);
65+
Assert.Equal(listing.TokenId, listingParams.TokenId);
66+
Assert.Equal(listing.Quantity, listingParams.Quantity);
67+
Assert.Equal(listing.PricePerToken, listingParams.PricePerToken);
68+
Assert.True(listing.StartTimestamp >= listingParams.StartTimestamp);
69+
Assert.True(listing.EndTimestamp >= listingParams.EndTimestamp);
70+
Assert.Equal(listing.ListingCreator, await wallet.GetAddress());
71+
Assert.Equal(listing.AssetContract, listingParams.AssetContract);
72+
Assert.Equal(listing.Currency, listingParams.Currency);
73+
Assert.Equal(TokenType.ERC1155, listing.TokenTypeEnum);
74+
Assert.Equal(Status.CREATED, listing.StatusEnum);
75+
Assert.Equal(listing.Reserved, listingParams.Reserved);
76+
77+
var isCurrencyApproved = await contract.Marketplace_DirectListings_IsCurrencyApprovedForListing(listingId, Constants.NATIVE_TOKEN_ADDRESS);
78+
Assert.True(isCurrencyApproved);
79+
80+
var currencyPriceForListing = await contract.Marketplace_DirectListings_CurrencyPriceForListing(listingId, Constants.NATIVE_TOKEN_ADDRESS);
81+
Assert.Equal(currencyPriceForListing, listingParams.PricePerToken);
82+
}
83+
84+
[Fact(Timeout = 120000)]
85+
public async Task Marketplace_DirectListings_UpdateListing_Success()
86+
{
87+
var contract = await this.GetMarketplaceContract();
88+
var wallet = await this.GetSmartWallet(1);
89+
90+
var originalTotal = await contract.Marketplace_DirectListings_TotalListings();
91+
92+
var originalListing = new ListingParameters()
93+
{
94+
AssetContract = this._drop1155ContractAddress,
95+
TokenId = 0,
96+
Quantity = 1,
97+
Currency = Constants.NATIVE_TOKEN_ADDRESS,
98+
PricePerToken = 1,
99+
StartTimestamp = Utils.GetUnixTimeStampNow() + 1800,
100+
EndTimestamp = Utils.GetUnixTimeStampNow() + 3600,
101+
Reserved = false
102+
};
103+
104+
var receipt = await contract.Marketplace_DirectListings_CreateListing(wallet, originalListing, true);
105+
Assert.NotNull(receipt);
106+
107+
var listingId = await contract.Marketplace_DirectListings_TotalListings() - 1;
108+
Assert.True(listingId == originalTotal);
109+
110+
var updatedListingParams = originalListing;
111+
updatedListingParams.PricePerToken = 2;
112+
113+
var updatedReceipt = await contract.Marketplace_DirectListings_UpdateListing(wallet, listingId, updatedListingParams);
114+
Assert.NotNull(updatedReceipt);
115+
Assert.True(updatedReceipt.TransactionHash.Length == 66);
116+
117+
var listing = await contract.Marketplace_DirectListings_GetListing(listingId);
118+
Assert.NotNull(listing);
119+
Assert.Equal(listing.PricePerToken, 2);
120+
}
121+
122+
[Fact(Timeout = 120000)]
123+
public async Task Marketplace_DirectListings_CancelListing_Success()
124+
{
125+
var contract = await this.GetMarketplaceContract();
126+
var wallet = await this.GetSmartWallet(1);
127+
128+
var originalTotal = await contract.Marketplace_DirectListings_TotalListings();
129+
130+
var originalListing = new ListingParameters()
131+
{
132+
AssetContract = this._drop1155ContractAddress,
133+
TokenId = 0,
134+
Quantity = 1,
135+
Currency = Constants.NATIVE_TOKEN_ADDRESS,
136+
PricePerToken = 1,
137+
StartTimestamp = Utils.GetUnixTimeStampNow() + 1800,
138+
EndTimestamp = Utils.GetUnixTimeStampNow() + 3600,
139+
Reserved = false
140+
};
141+
142+
var receipt = await contract.Marketplace_DirectListings_CreateListing(wallet, originalListing, true);
143+
Assert.NotNull(receipt);
144+
145+
var listingId = await contract.Marketplace_DirectListings_TotalListings() - 1;
146+
Assert.True(listingId == originalTotal);
147+
148+
var removeReceipt = await contract.Marketplace_DirectListings_CancelListing(wallet, listingId);
149+
Assert.NotNull(removeReceipt);
150+
Assert.True(removeReceipt.TransactionHash.Length == 66);
151+
}
152+
153+
[Fact(Timeout = 120000)]
154+
public async Task Marketplace_DirectListings_ApproveBuyerForListing()
155+
{
156+
var contract = await this.GetMarketplaceContract();
157+
var wallet = await this.GetSmartWallet(1);
158+
159+
var reservedListing = new ListingParameters()
160+
{
161+
AssetContract = this._drop1155ContractAddress,
162+
TokenId = 0,
163+
Quantity = 1,
164+
Currency = Constants.NATIVE_TOKEN_ADDRESS,
165+
PricePerToken = 1,
166+
StartTimestamp = Utils.GetUnixTimeStampNow(),
167+
EndTimestamp = Utils.GetUnixTimeStampNow() + 3600,
168+
Reserved = true
169+
};
170+
171+
var receipt = await contract.Marketplace_DirectListings_CreateListing(wallet, reservedListing, true);
172+
Assert.NotNull(receipt);
173+
Assert.True(receipt.TransactionHash.Length == 66);
174+
175+
var listingId = await contract.Marketplace_DirectListings_TotalListings() - 1;
176+
177+
var buyer = await PrivateKeyWallet.Generate(this.Client);
178+
var approveReceipt = await contract.Marketplace_DirectListings_ApproveBuyerForListing(wallet, listingId, await buyer.GetAddress(), true);
179+
Assert.NotNull(approveReceipt);
180+
Assert.True(approveReceipt.TransactionHash.Length == 66);
181+
182+
var isApproved = await contract.Marketplace_DirectListings_IsBuyerApprovedForListing(listingId, await buyer.GetAddress());
183+
Assert.True(isApproved);
184+
}
185+
27186
[Fact(Timeout = 120000)]
28187
public async Task Marketplace_DirectListings_TotalListings_Success()
29188
{

Thirdweb/Thirdweb.Extensions/ThirdwebMarketplaceExtensions.Types.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public class ListingParameters
114114
/// <summary>
115115
/// Represents a listing in the marketplace.
116116
/// </summary>
117-
[Struct("Listing")]
117+
[FunctionOutput]
118118
public class Listing
119119
{
120120
/// <summary>
@@ -264,7 +264,7 @@ public class AuctionParameters
264264
/// <summary>
265265
/// Represents an auction in the marketplace.
266266
/// </summary>
267-
[Struct("Auction")]
267+
[FunctionOutput]
268268
public class Auction
269269
{
270270
/// <summary>

Thirdweb/Thirdweb.Extensions/ThirdwebMarketplaceExtensions.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,57 @@ public static async Task<Listing> Marketplace_DirectListings_GetListing(this Thi
335335
return await contract.Read<Listing>("getListing", listingId);
336336
}
337337

338+
/// <summary>
339+
/// Checks whether a buyer is approved for a direct listing.
340+
/// </summary>
341+
/// <param name="contract">The contract instance.</param>
342+
/// <param name="listingId">The ID of the listing.</param>
343+
/// <param name="buyer">The address of the buyer to check.</param>
344+
/// <returns>A task that represents a boolean indicating if the buyer is approved.</returns>
345+
public static async Task<bool> Marketplace_DirectListings_IsBuyerApprovedForListing(this ThirdwebContract contract, BigInteger listingId, string buyer)
346+
{
347+
if (contract == null)
348+
{
349+
throw new ArgumentNullException(nameof(contract));
350+
}
351+
352+
return await contract.Read<bool>("isBuyerApprovedForListing", listingId, buyer);
353+
}
354+
355+
/// <summary>
356+
/// Checks whether a currency is approved for a direct listing.
357+
/// </summary>
358+
/// <param name="contract">The contract instance.</param>
359+
/// <param name="listingId">The ID of the listing.</param>
360+
/// <param name="currency">The address of the currency to check.</param>
361+
/// <returns>A task that represents a boolean indicating if the currency is approved.</returns>
362+
public static async Task<bool> Marketplace_DirectListings_IsCurrencyApprovedForListing(this ThirdwebContract contract, BigInteger listingId, string currency)
363+
{
364+
if (contract == null)
365+
{
366+
throw new ArgumentNullException(nameof(contract));
367+
}
368+
369+
return await contract.Read<bool>("isCurrencyApprovedForListing", listingId, currency);
370+
}
371+
372+
/// <summary>
373+
/// Gets the price per token for a direct listing in the specified currency.
374+
/// </summary>
375+
/// <param name="contract">The contract instance.</param>
376+
/// <param name="listingId">The ID of the listing.</param>
377+
/// <param name="currency">The address of the currency to check.</param>
378+
/// <returns>A task that represents the price per token in the specified currency.</returns>
379+
public static async Task<BigInteger> Marketplace_DirectListings_CurrencyPriceForListing(this ThirdwebContract contract, BigInteger listingId, string currency)
380+
{
381+
if (contract == null)
382+
{
383+
throw new ArgumentNullException(nameof(contract));
384+
}
385+
386+
return await contract.Read<BigInteger>("currencyPriceForListing", listingId, currency);
387+
}
388+
338389
#endregion
339390

340391
#region IEnglishAuctions

0 commit comments

Comments
 (0)