Skip to content

Commit 3c69aa0

Browse files
committed
Handle approvals option
1 parent 6f219b2 commit 3c69aa0

File tree

1 file changed

+114
-2
lines changed

1 file changed

+114
-2
lines changed

Thirdweb/Thirdweb.Extensions/ThirdwebMarketplaceExtensions.cs

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ public static class ThirdwebMarketplaceExtensions
1212
/// <param name="contract">The contract instance.</param>
1313
/// <param name="wallet">The wallet used for the transaction.</param>
1414
/// <param name="parameters">The parameters of the listing to be created.</param>
15+
/// <param name="handleApprovals">Whether to handle token approvals automatically.</param>
1516
/// <returns>A task that represents the transaction receipt of the listing creation.</returns>
16-
public static async Task<ThirdwebTransactionReceipt> Marketplace_DirectListings_CreateListing(this ThirdwebContract contract, IThirdwebWallet wallet, ListingParameters parameters)
17+
public static async Task<ThirdwebTransactionReceipt> Marketplace_DirectListings_CreateListing(
18+
this ThirdwebContract contract,
19+
IThirdwebWallet wallet,
20+
ListingParameters parameters,
21+
bool handleApprovals = false
22+
)
1723
{
1824
if (contract == null)
1925
{
@@ -30,6 +36,56 @@ public static async Task<ThirdwebTransactionReceipt> Marketplace_DirectListings_
3036
throw new ArgumentNullException(nameof(parameters));
3137
}
3238

39+
if (handleApprovals)
40+
{
41+
var assetContractAddress = parameters.AssetContract;
42+
43+
var prepTasks = new List<Task>();
44+
45+
var assetContractTask = ThirdwebContract.Create(contract.Client, assetContractAddress, contract.Chain);
46+
prepTasks.Add(assetContractTask);
47+
48+
var walletAddressTask = wallet.GetAddress();
49+
prepTasks.Add(walletAddressTask);
50+
51+
await Task.WhenAll(prepTasks);
52+
53+
var assetContract = assetContractTask.Result;
54+
var walletAddress = walletAddressTask.Result;
55+
56+
TokenType assetType;
57+
if (await assetContract.SupportsInterface(Constants.IERC1155_INTERFACE_ID))
58+
{
59+
assetType = TokenType.ERC1155;
60+
}
61+
else if (await assetContract.SupportsInterface(Constants.IERC721_INTERFACE_ID))
62+
{
63+
assetType = TokenType.ERC721;
64+
}
65+
else
66+
{
67+
throw new ArgumentException("Asset contract does not support ERC1155 or ERC721 interface.");
68+
}
69+
70+
if (assetType == TokenType.ERC721)
71+
{
72+
var tokenId = parameters.TokenId;
73+
var @operator = await assetContract.ERC721_GetApproved(tokenId);
74+
if (@operator != contract.Address)
75+
{
76+
_ = await assetContract.ERC721_Approve(wallet, contract.Address, tokenId);
77+
}
78+
}
79+
else
80+
{
81+
var isApprovedForAll = await assetContract.ERC1155_IsApprovedForAll(walletAddress, contract.Address);
82+
if (!isApprovedForAll)
83+
{
84+
_ = await assetContract.ERC1155_SetApprovalForAll(wallet, contract.Address, true);
85+
}
86+
}
87+
}
88+
3389
return await contract.Write(wallet, "createListing", 0, parameters);
3490
}
3591

@@ -264,8 +320,14 @@ public static async Task<Listing> Marketplace_DirectListings_GetListing(this Thi
264320
/// <param name="contract">The contract instance.</param>
265321
/// <param name="wallet">The wallet used for the transaction.</param>
266322
/// <param name="parameters">The parameters of the auction to be created.</param>
323+
/// <param name="handleApprovals">Whether to handle token approvals automatically.</param>
267324
/// <returns>A task that represents the transaction receipt of the auction creation.</returns>
268-
public static async Task<ThirdwebTransactionReceipt> Marketplace_EnglishAuctions_CreateAuction(this ThirdwebContract contract, IThirdwebWallet wallet, AuctionParameters parameters)
325+
public static async Task<ThirdwebTransactionReceipt> Marketplace_EnglishAuctions_CreateAuction(
326+
this ThirdwebContract contract,
327+
IThirdwebWallet wallet,
328+
AuctionParameters parameters,
329+
bool handleApprovals = false
330+
)
269331
{
270332
if (contract == null)
271333
{
@@ -282,6 +344,56 @@ public static async Task<ThirdwebTransactionReceipt> Marketplace_EnglishAuctions
282344
throw new ArgumentNullException(nameof(parameters));
283345
}
284346

347+
if (handleApprovals)
348+
{
349+
var assetContractAddress = parameters.AssetContract;
350+
351+
var prepTasks = new List<Task>();
352+
353+
var assetContractTask = ThirdwebContract.Create(contract.Client, assetContractAddress, contract.Chain);
354+
prepTasks.Add(assetContractTask);
355+
356+
var walletAddressTask = wallet.GetAddress();
357+
prepTasks.Add(walletAddressTask);
358+
359+
await Task.WhenAll(prepTasks);
360+
361+
var assetContract = assetContractTask.Result;
362+
var walletAddress = walletAddressTask.Result;
363+
364+
TokenType assetType;
365+
if (await assetContract.SupportsInterface(Constants.IERC1155_INTERFACE_ID))
366+
{
367+
assetType = TokenType.ERC1155;
368+
}
369+
else if (await assetContract.SupportsInterface(Constants.IERC721_INTERFACE_ID))
370+
{
371+
assetType = TokenType.ERC721;
372+
}
373+
else
374+
{
375+
throw new ArgumentException("Asset contract does not support ERC1155 or ERC721 interface.");
376+
}
377+
378+
if (assetType == TokenType.ERC721)
379+
{
380+
var tokenId = parameters.TokenId;
381+
var @operator = await assetContract.ERC721_GetApproved(tokenId);
382+
if (@operator != contract.Address)
383+
{
384+
_ = await assetContract.ERC721_Approve(wallet, contract.Address, tokenId);
385+
}
386+
}
387+
else
388+
{
389+
var isApprovedForAll = await assetContract.ERC1155_IsApprovedForAll(walletAddress, contract.Address);
390+
if (!isApprovedForAll)
391+
{
392+
_ = await assetContract.ERC1155_SetApprovalForAll(wallet, contract.Address, true);
393+
}
394+
}
395+
}
396+
285397
return await contract.Write(wallet, "createAuction", 0, parameters);
286398
}
287399

0 commit comments

Comments
 (0)