Skip to content

Commit 4b0aa23

Browse files
Add Pack Contract wrapper GetContract("...").pack
1 parent b0fc35f commit 4b0aa23

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed

Assets/Thirdweb/Scripts/Contract.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public class Contract : Routable
2626
/// Call any Marketplace supported functions
2727
/// </summary>
2828
public Marketplace marketplace;
29+
/// <summary>
30+
/// Call any Pack supported functions
31+
/// </summary>
32+
public Pack pack;
2933

3034
public Contract(string chain, string address, string abi = null) : base(abi != null ? $"{address}{Routable.subSeparator}{abi}" : address)
3135
{
@@ -36,6 +40,7 @@ public Contract(string chain, string address, string abi = null) : base(abi != n
3640
this.ERC721 = new ERC721(baseRoute);
3741
this.ERC1155 = new ERC1155(baseRoute);
3842
this.marketplace = new Marketplace(chain, address);
43+
this.pack = new Pack(chain, address);
3944
}
4045

4146
public async Task<CurrencyValue> GetBalance()

Assets/Thirdweb/Scripts/Pack.cs

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
4+
namespace Thirdweb
5+
{
6+
/// <summary>
7+
/// Interact with a Pack contract.
8+
/// </summary>
9+
public class Pack : Routable
10+
{
11+
public string chain;
12+
public string address;
13+
14+
/// <summary>
15+
/// Interact with a Marketplace contract.
16+
/// </summary>
17+
public Pack(string chain, string address) : base($"{address}{subSeparator}pack")
18+
{
19+
this.chain = chain;
20+
this.address = address;
21+
}
22+
23+
/// READ FUNCTIONS
24+
25+
/// <summary>
26+
/// Get a NFT in this contract by its ID
27+
/// </summary>
28+
public async Task<NFT> Get(string tokenId)
29+
{
30+
return await Bridge.InvokeRoute<NFT>(getRoute("get"), Utils.ToJsonStringArray(tokenId));
31+
}
32+
33+
/// <summary>
34+
/// Get a all NFTs in this contract
35+
/// </summary>
36+
public async Task<List<NFT>> GetAll(QueryAllParams queryParams = null)
37+
{
38+
return await Bridge.InvokeRoute<List<NFT>>(getRoute("getAll"), Utils.ToJsonStringArray(queryParams));
39+
}
40+
41+
/// <summary>
42+
/// Get a all NFTs owned by the connected wallet
43+
/// </summary>
44+
/// <param name="address">Optional wallet address to query NFTs of</param>
45+
public async Task<List<NFT>> GetOwned(string address = null)
46+
{
47+
return await Bridge.InvokeRoute<List<NFT>>(getRoute("getOwned"), Utils.ToJsonStringArray(address));
48+
}
49+
50+
/// <summary>
51+
/// Get the balance of the given NFT for the connected wallet
52+
/// </summary>
53+
public async Task<string> Balance(string tokenId)
54+
{
55+
return await Bridge.InvokeRoute<string>(getRoute("balance"), new string[] { });
56+
}
57+
58+
/// <summary>
59+
/// Get the balance of the given NFT for the given wallet address
60+
/// </summary>
61+
public async Task<string> BalanceOf(string address, string tokenId)
62+
{
63+
return await Bridge.InvokeRoute<string>(getRoute("balanceOf"), Utils.ToJsonStringArray(address, tokenId));
64+
}
65+
66+
/// <summary>
67+
/// Check whether the given contract address has been approved to transfer NFTs on behalf of the given wallet address
68+
/// </summary>
69+
/// <param name="address">The wallet address</param>
70+
/// <param name="contractAddress">The contract address to check approval for</param>
71+
public async Task<string> IsApprovedForAll(string address, string approvedContract)
72+
{
73+
return await Bridge.InvokeRoute<string>(getRoute("isApproved"), Utils.ToJsonStringArray(address, approvedContract));
74+
}
75+
76+
public async Task<int> TotalCount()
77+
{
78+
return await Bridge.InvokeRoute<int>(getRoute("totalCount"), new string[] { });
79+
}
80+
81+
/// <summary>
82+
/// Get the total suppply in circulation for thge given NFT
83+
/// </summary>
84+
public async Task<int> TotalSupply(string tokenId)
85+
{
86+
return await Bridge.InvokeRoute<int>(getRoute("totalSupply"), Utils.ToJsonStringArray(tokenId));
87+
}
88+
89+
/// <summary>
90+
/// Get all the possible contents of a given pack
91+
/// </summary>
92+
public async Task<PackContents> GetPackContents(string packId)
93+
{
94+
return await Bridge.InvokeRoute<PackContents>(getRoute("getPackContents"), Utils.ToJsonStringArray(packId));
95+
}
96+
97+
/// WRITE FUNCTIONS
98+
99+
/// <summary>
100+
/// Set approval to the given contract to transfer NFTs on behalf of the connected wallet
101+
/// </summary>
102+
public async Task<TransactionResult> SetApprovalForAll(string contractToApprove, bool approved)
103+
{
104+
return await Bridge.InvokeRoute<TransactionResult>(getRoute("isApproved"), Utils.ToJsonStringArray(contractToApprove, approved));
105+
}
106+
107+
/// <summary>
108+
/// Transfer NFTs to the given address
109+
/// </summary>
110+
public async Task<TransactionResult> Transfer(string to, string tokenId, int amount)
111+
{
112+
return await Bridge.InvokeRoute<TransactionResult>(getRoute("transfer"), Utils.ToJsonStringArray(to, tokenId, amount));
113+
}
114+
115+
/// <summary>
116+
/// Create a new Pack with all the possible rewards (requires approval to transfer tokens/NFTs defined as rewards)
117+
/// </summary>
118+
public async Task<TransactionResult> Create(NewPackInput pack)
119+
{
120+
return await Bridge.InvokeRoute<TransactionResult>(getRoute("create"), Utils.ToJsonStringArray(pack));
121+
}
122+
123+
/// <summary>
124+
/// Create a new Pack with all the possible rewards and mints it to the given address (requires approval to transfer tokens/NFTs defined as rewards)
125+
/// </summary>
126+
public async Task<TransactionResult> CreateTo(string receiverAddress, NewPackInput pack)
127+
{
128+
return await Bridge.InvokeRoute<TransactionResult>(getRoute("createTo"), Utils.ToJsonStringArray(receiverAddress, pack));
129+
}
130+
131+
/// <summary>
132+
/// Add new contents to an existing pack
133+
/// </summary>
134+
public async Task<TransactionResult> AddPackContents(string packId, PackRewards newContents)
135+
{
136+
return await Bridge.InvokeRoute<TransactionResult>(getRoute("addPackContents"), Utils.ToJsonStringArray(packId, newContents));
137+
}
138+
139+
/// <summary>
140+
/// Open a pack and transfer the rewards to the connected wallet
141+
/// </summary>
142+
public async Task<PackRewards> Open(string packId, string amount = "1")
143+
{
144+
return await Bridge.InvokeRoute<PackRewards>(getRoute("open"), Utils.ToJsonStringArray(packId, amount));
145+
}
146+
}
147+
148+
[System.Serializable]
149+
public struct PackRewards
150+
{
151+
public List<ERC20Reward> erc20Rewards;
152+
public List<ERC721Reward> erc721Rewards;
153+
public List<ERC1155Reward> erc1155Rewards;
154+
}
155+
156+
[System.Serializable]
157+
public class PackContents
158+
{
159+
public List<ERC20Contents> erc20Rewards;
160+
public List<ERC721Contents> erc721Rewards;
161+
public List<ERC1155Contents> erc1155Rewards;
162+
}
163+
164+
[System.Serializable]
165+
public class NewPackInput : PackContents
166+
{
167+
/// The Metadata of the pack NFT itself
168+
public NFTMetadata packMetadata;
169+
/// How many rewards can be obtained by opening a single pack
170+
public string rewardsPerPack;
171+
}
172+
173+
[System.Serializable]
174+
public class ERC20Reward
175+
{
176+
/// the Token contract address
177+
public string contractAddress;
178+
/// How many tokens can be otained when opening a pack and receiving this reward
179+
public string quantityPerReward;
180+
}
181+
182+
[System.Serializable]
183+
public class ERC20Contents : ERC20Reward
184+
{
185+
public string totalRewards;
186+
}
187+
188+
[System.Serializable]
189+
public class ERC721Reward
190+
{
191+
/// the ERC721 contract address
192+
public string contractAddress;
193+
/// the tokenId of the NFT to be rewarded
194+
public string tokenId;
195+
}
196+
197+
[System.Serializable]
198+
public class ERC721Contents : ERC721Reward
199+
{
200+
}
201+
202+
[System.Serializable]
203+
public class ERC1155Reward
204+
{
205+
/// the ERC1155 contract address
206+
public string contractAddress;
207+
/// the tokenId of the NFT to be rewarded
208+
public string tokenId;
209+
/// How many NFTs can be otained when opening a pack and receiving this reward
210+
public string quantityPerReward;
211+
}
212+
213+
[System.Serializable]
214+
public class ERC1155Contents : ERC1155Reward
215+
{
216+
public string totalRewards;
217+
}
218+
}

Assets/Thirdweb/Scripts/Pack.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)