Skip to content

Commit

Permalink
Fix packContents fetching on webGL (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges authored Mar 23, 2023
1 parent ad712c0 commit 8b516f1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 95 deletions.
44 changes: 23 additions & 21 deletions Assets/Thirdweb/Core/Scripts/Pack.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
Expand All @@ -20,8 +19,7 @@ public class Pack : Routable
/// <summary>
/// Interact with a Marketplace contract.
/// </summary>
public Pack(string chain, string address)
: base($"{address}{subSeparator}pack")
public Pack(string chain, string address) : base($"{address}{subSeparator}pack")
{
this.chain = chain;
this.contractAddress = address;
Expand Down Expand Up @@ -193,14 +191,17 @@ public async Task<PackContents> GetPackContents(string packId)
var erc20R = new List<ERC20Contents>();
var erc721R = new List<ERC721Contents>();
var erc1155R = new List<ERC1155Contents>();
foreach (var tokenReward in packContents.Contents)
for (int i = 0; i < packContents.Contents.Count; i++)
{
var tokenReward = packContents.Contents[i];
var amount = packContents.PerUnitAmounts[i];
switch (tokenReward.TokenType)
{
case 0:
var tempERC20 = new ERC20Contents();
tempERC20.contractAddress = tokenReward.AssetContract;
tempERC20.quantityPerReward = tokenReward.TotalAmount.ToString();
tempERC20.quantityPerReward = amount.ToString().ToEth(18);
tempERC20.totalRewards = (tokenReward.TotalAmount / amount).ToString().ToEth(18);
erc20R.Add(tempERC20);
break;
case 1:
Expand All @@ -213,17 +214,18 @@ public async Task<PackContents> GetPackContents(string packId)
var tempERC1155 = new ERC1155Contents();
tempERC1155.contractAddress = tokenReward.AssetContract;
tempERC1155.tokenId = tokenReward.TokenId.ToString();
tempERC1155.quantityPerReward = tokenReward.TotalAmount.ToString();
tempERC1155.quantityPerReward = amount.ToString();
tempERC1155.totalRewards = (tokenReward.TotalAmount / amount).ToString();
erc1155R.Add(tempERC1155);
break;
default:
break;
}
}
PackContents contents = new PackContents();
contents.erc20Contents = erc20R;
contents.erc721Contents = erc721R;
contents.erc1155Contents = erc1155R;
contents.erc20Rewards = erc20R;
contents.erc721Rewards = erc721R;
contents.erc1155Rewards = erc1155R;
return contents;
}
}
Expand Down Expand Up @@ -406,20 +408,20 @@ public override string ToString()
[System.Serializable]
public class PackContents
{
public List<ERC20Contents> erc20Contents;
public List<ERC721Contents> erc721Contents;
public List<ERC1155Contents> erc1155Contents;
public List<ERC20Contents> erc20Rewards;
public List<ERC721Contents> erc721Rewards;
public List<ERC1155Contents> erc1155Rewards;

public override string ToString()
{
string erc20str = "ERC20 Contents:\n";
foreach (var content in erc20Contents)
string erc20str = "\n";
foreach (var content in erc20Rewards)
erc20str += content.ToString();
string erc721str = "ERC721 Contents:\n";
foreach (var content in erc721Contents)
string erc721str = "\n";
foreach (var content in erc721Rewards)
erc721str += content.ToString();
string erc1155str = "ERC1155 Contents:\n";
foreach (var content in erc1155Contents)
string erc1155str = "\n";
foreach (var content in erc1155Rewards)
erc1155str += content.ToString();
return "PackContents:\n" + erc20str + erc721str + erc1155str;
}
Expand Down Expand Up @@ -462,7 +464,7 @@ public class ERC20Contents : ERC20Reward

public override string ToString()
{
return "ERC20Contents:\n" + $"totalRewards: {totalRewards.ToString()}\n" + base.ToString();
return base.ToString() + $"totalRewards: {totalRewards.ToString()}\n";
}
}

Expand Down Expand Up @@ -504,7 +506,7 @@ public class ERC1155Reward

public override string ToString()
{
return "ERC1155Reward:\n" + $"contractAddress: {contractAddress.ToString()}\n" + $"tokenId: {tokenId.ToString()}\n" + $"contractAddress: {tokenId.ToString()}\n";
return "ERC1155Reward:\n" + $"contractAddress: {contractAddress.ToString()}\n" + $"tokenId: {tokenId.ToString()}\n" + $"quantityPerReward: {quantityPerReward.ToString()}\n";
}
}

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

public override string ToString()
{
return "ERC1155Contents:\n" + $"totalRewards: {totalRewards.ToString()}\n" + base.ToString();
return base.ToString() + $"totalRewards: {totalRewards.ToString()}\n";
}
}
}
12 changes: 6 additions & 6 deletions Assets/Thirdweb/Core/Scripts/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public async static Task<List<NFT>> ToNFTList(this List<TokenData721> tokenDataL
{
List<Thirdweb.Contracts.Pack.ContractDefinition.Token> tokenList = new List<Contracts.Pack.ContractDefinition.Token>();
// Add ERC20 Rewards
foreach (var erc20Reward in packContents.erc20Contents)
foreach (var erc20Reward in packContents.erc20Rewards)
{
tokenList.Add(
new Thirdweb.Contracts.Pack.ContractDefinition.Token()
Expand All @@ -164,7 +164,7 @@ public async static Task<List<NFT>> ToNFTList(this List<TokenData721> tokenDataL
);
}
// Add ERC721 Rewards
foreach (var erc721Reward in packContents.erc721Contents)
foreach (var erc721Reward in packContents.erc721Rewards)
{
tokenList.Add(
new Thirdweb.Contracts.Pack.ContractDefinition.Token()
Expand All @@ -177,7 +177,7 @@ public async static Task<List<NFT>> ToNFTList(this List<TokenData721> tokenDataL
);
}
// Add ERC1155 Rewards
foreach (var erc1155Reward in packContents.erc1155Contents)
foreach (var erc1155Reward in packContents.erc1155Rewards)
{
tokenList.Add(
new Thirdweb.Contracts.Pack.ContractDefinition.Token()
Expand All @@ -196,17 +196,17 @@ public static List<BigInteger> ToPackRewardUnitsList(this PackContents packConte
{
List<BigInteger> rewardUnits = new List<BigInteger>();
// Add ERC20 Rewards
foreach (var content in packContents.erc20Contents)
foreach (var content in packContents.erc20Rewards)
{
rewardUnits.Add(BigInteger.Parse(content.quantityPerReward.ToWei()));
}
// Add ERC721 Rewards
foreach (var content in packContents.erc721Contents)
foreach (var content in packContents.erc721Rewards)
{
rewardUnits.Add(1);
}
// Add ERC1155 Rewards
foreach (var content in packContents.erc1155Contents)
foreach (var content in packContents.erc1155Rewards)
{
rewardUnits.Add(BigInteger.Parse(content.quantityPerReward));
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Thirdweb/Examples/Scripts/Prefabs/Prefab_Reading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async void FetchPackContents()
Contract contract = ThirdwebManager.Instance.SDK.GetContract(PACK_CONTRACT);

PackContents packContents = await contract.pack.GetPackContents("0");
Debugger.Instance.Log("[Fetch Pack Contents] Pack #0", "ERC721 Contents:\n" + packContents.erc721Contents[0].ToString());
Debugger.Instance.Log("[Fetch Pack Contents] Pack #0", packContents.ToString());
}
catch (System.Exception e)
{
Expand Down
134 changes: 67 additions & 67 deletions Assets/WebGLTemplates/Thirdweb/lib/thirdweb-unity-bridge.js

Large diffs are not rendered by default.

0 comments on commit 8b516f1

Please sign in to comment.