Skip to content

Commit 8b516f1

Browse files
Fix packContents fetching on webGL (#51)
1 parent ad712c0 commit 8b516f1

File tree

4 files changed

+97
-95
lines changed

4 files changed

+97
-95
lines changed

Assets/Thirdweb/Core/Scripts/Pack.cs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Numerics;
43
using System.Threading.Tasks;
@@ -20,8 +19,7 @@ public class Pack : Routable
2019
/// <summary>
2120
/// Interact with a Marketplace contract.
2221
/// </summary>
23-
public Pack(string chain, string address)
24-
: base($"{address}{subSeparator}pack")
22+
public Pack(string chain, string address) : base($"{address}{subSeparator}pack")
2523
{
2624
this.chain = chain;
2725
this.contractAddress = address;
@@ -193,14 +191,17 @@ public async Task<PackContents> GetPackContents(string packId)
193191
var erc20R = new List<ERC20Contents>();
194192
var erc721R = new List<ERC721Contents>();
195193
var erc1155R = new List<ERC1155Contents>();
196-
foreach (var tokenReward in packContents.Contents)
194+
for (int i = 0; i < packContents.Contents.Count; i++)
197195
{
196+
var tokenReward = packContents.Contents[i];
197+
var amount = packContents.PerUnitAmounts[i];
198198
switch (tokenReward.TokenType)
199199
{
200200
case 0:
201201
var tempERC20 = new ERC20Contents();
202202
tempERC20.contractAddress = tokenReward.AssetContract;
203-
tempERC20.quantityPerReward = tokenReward.TotalAmount.ToString();
203+
tempERC20.quantityPerReward = amount.ToString().ToEth(18);
204+
tempERC20.totalRewards = (tokenReward.TotalAmount / amount).ToString().ToEth(18);
204205
erc20R.Add(tempERC20);
205206
break;
206207
case 1:
@@ -213,17 +214,18 @@ public async Task<PackContents> GetPackContents(string packId)
213214
var tempERC1155 = new ERC1155Contents();
214215
tempERC1155.contractAddress = tokenReward.AssetContract;
215216
tempERC1155.tokenId = tokenReward.TokenId.ToString();
216-
tempERC1155.quantityPerReward = tokenReward.TotalAmount.ToString();
217+
tempERC1155.quantityPerReward = amount.ToString();
218+
tempERC1155.totalRewards = (tokenReward.TotalAmount / amount).ToString();
217219
erc1155R.Add(tempERC1155);
218220
break;
219221
default:
220222
break;
221223
}
222224
}
223225
PackContents contents = new PackContents();
224-
contents.erc20Contents = erc20R;
225-
contents.erc721Contents = erc721R;
226-
contents.erc1155Contents = erc1155R;
226+
contents.erc20Rewards = erc20R;
227+
contents.erc721Rewards = erc721R;
228+
contents.erc1155Rewards = erc1155R;
227229
return contents;
228230
}
229231
}
@@ -406,20 +408,20 @@ public override string ToString()
406408
[System.Serializable]
407409
public class PackContents
408410
{
409-
public List<ERC20Contents> erc20Contents;
410-
public List<ERC721Contents> erc721Contents;
411-
public List<ERC1155Contents> erc1155Contents;
411+
public List<ERC20Contents> erc20Rewards;
412+
public List<ERC721Contents> erc721Rewards;
413+
public List<ERC1155Contents> erc1155Rewards;
412414

413415
public override string ToString()
414416
{
415-
string erc20str = "ERC20 Contents:\n";
416-
foreach (var content in erc20Contents)
417+
string erc20str = "\n";
418+
foreach (var content in erc20Rewards)
417419
erc20str += content.ToString();
418-
string erc721str = "ERC721 Contents:\n";
419-
foreach (var content in erc721Contents)
420+
string erc721str = "\n";
421+
foreach (var content in erc721Rewards)
420422
erc721str += content.ToString();
421-
string erc1155str = "ERC1155 Contents:\n";
422-
foreach (var content in erc1155Contents)
423+
string erc1155str = "\n";
424+
foreach (var content in erc1155Rewards)
423425
erc1155str += content.ToString();
424426
return "PackContents:\n" + erc20str + erc721str + erc1155str;
425427
}
@@ -462,7 +464,7 @@ public class ERC20Contents : ERC20Reward
462464

463465
public override string ToString()
464466
{
465-
return "ERC20Contents:\n" + $"totalRewards: {totalRewards.ToString()}\n" + base.ToString();
467+
return base.ToString() + $"totalRewards: {totalRewards.ToString()}\n";
466468
}
467469
}
468470

@@ -504,7 +506,7 @@ public class ERC1155Reward
504506

505507
public override string ToString()
506508
{
507-
return "ERC1155Reward:\n" + $"contractAddress: {contractAddress.ToString()}\n" + $"tokenId: {tokenId.ToString()}\n" + $"contractAddress: {tokenId.ToString()}\n";
509+
return "ERC1155Reward:\n" + $"contractAddress: {contractAddress.ToString()}\n" + $"tokenId: {tokenId.ToString()}\n" + $"quantityPerReward: {quantityPerReward.ToString()}\n";
508510
}
509511
}
510512

@@ -515,7 +517,7 @@ public class ERC1155Contents : ERC1155Reward
515517

516518
public override string ToString()
517519
{
518-
return "ERC1155Contents:\n" + $"totalRewards: {totalRewards.ToString()}\n" + base.ToString();
520+
return base.ToString() + $"totalRewards: {totalRewards.ToString()}\n";
519521
}
520522
}
521523
}

Assets/Thirdweb/Core/Scripts/Utils.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public async static Task<List<NFT>> ToNFTList(this List<TokenData721> tokenDataL
151151
{
152152
List<Thirdweb.Contracts.Pack.ContractDefinition.Token> tokenList = new List<Contracts.Pack.ContractDefinition.Token>();
153153
// Add ERC20 Rewards
154-
foreach (var erc20Reward in packContents.erc20Contents)
154+
foreach (var erc20Reward in packContents.erc20Rewards)
155155
{
156156
tokenList.Add(
157157
new Thirdweb.Contracts.Pack.ContractDefinition.Token()
@@ -164,7 +164,7 @@ public async static Task<List<NFT>> ToNFTList(this List<TokenData721> tokenDataL
164164
);
165165
}
166166
// Add ERC721 Rewards
167-
foreach (var erc721Reward in packContents.erc721Contents)
167+
foreach (var erc721Reward in packContents.erc721Rewards)
168168
{
169169
tokenList.Add(
170170
new Thirdweb.Contracts.Pack.ContractDefinition.Token()
@@ -177,7 +177,7 @@ public async static Task<List<NFT>> ToNFTList(this List<TokenData721> tokenDataL
177177
);
178178
}
179179
// Add ERC1155 Rewards
180-
foreach (var erc1155Reward in packContents.erc1155Contents)
180+
foreach (var erc1155Reward in packContents.erc1155Rewards)
181181
{
182182
tokenList.Add(
183183
new Thirdweb.Contracts.Pack.ContractDefinition.Token()
@@ -196,17 +196,17 @@ public static List<BigInteger> ToPackRewardUnitsList(this PackContents packConte
196196
{
197197
List<BigInteger> rewardUnits = new List<BigInteger>();
198198
// Add ERC20 Rewards
199-
foreach (var content in packContents.erc20Contents)
199+
foreach (var content in packContents.erc20Rewards)
200200
{
201201
rewardUnits.Add(BigInteger.Parse(content.quantityPerReward.ToWei()));
202202
}
203203
// Add ERC721 Rewards
204-
foreach (var content in packContents.erc721Contents)
204+
foreach (var content in packContents.erc721Rewards)
205205
{
206206
rewardUnits.Add(1);
207207
}
208208
// Add ERC1155 Rewards
209-
foreach (var content in packContents.erc1155Contents)
209+
foreach (var content in packContents.erc1155Rewards)
210210
{
211211
rewardUnits.Add(BigInteger.Parse(content.quantityPerReward));
212212
}

Assets/Thirdweb/Examples/Scripts/Prefabs/Prefab_Reading.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public async void FetchPackContents()
9393
Contract contract = ThirdwebManager.Instance.SDK.GetContract(PACK_CONTRACT);
9494

9595
PackContents packContents = await contract.pack.GetPackContents("0");
96-
Debugger.Instance.Log("[Fetch Pack Contents] Pack #0", "ERC721 Contents:\n" + packContents.erc721Contents[0].ToString());
96+
Debugger.Instance.Log("[Fetch Pack Contents] Pack #0", packContents.ToString());
9797
}
9898
catch (System.Exception e)
9999
{

Assets/WebGLTemplates/Thirdweb/lib/thirdweb-unity-bridge.js

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

0 commit comments

Comments
 (0)