Skip to content

Commit 51dffbe

Browse files
authored
Additional NFT Metadata in ThirdwebInsight (#135)
1 parent a65e8c0 commit 51dffbe

File tree

7 files changed

+264
-81
lines changed

7 files changed

+264
-81
lines changed

Thirdweb.Console/Program.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343

4444
#region Indexer
4545

46-
// Create a ThirdwebInsight instance
47-
var insight = await ThirdwebInsight.Create(client);
46+
// // Create a ThirdwebInsight instance
47+
// var insight = await ThirdwebInsight.Create(client);
4848

4949
// // Setup some filters
5050
// var address = await Utils.GetAddressFromENS(client, "vitalik.eth");
@@ -89,6 +89,17 @@
8989
// );
9090
// Console.WriteLine($"Transactions: {JsonConvert.SerializeObject(transactions, Formatting.Indented)}");
9191

92+
// // Use ToNFT to ToNFTList extensions
93+
// var convertedNft = erc721Tokens[0].ToNFT();
94+
95+
// var convertedNfts = erc721Tokens.ToNFTList();
96+
97+
// // Use NFT Extensions (GetNFTImageBytes, or GetNFTSprite in Unity)
98+
// var imageBytes = await convertedNft.GetNFTImageBytes(client);
99+
// var pathToSave = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "nft.png");
100+
// await File.WriteAllBytesAsync(pathToSave, imageBytes);
101+
// Console.WriteLine($"NFT image saved to: {pathToSave}");
102+
92103
#endregion
93104

94105
#region AI

Thirdweb/Thirdweb.Extensions/ThirdwebExtensions.Types.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public enum NFTType
149149
/// Represents an NFT with metadata, owner, type, and supply information.
150150
/// </summary>
151151
[Serializable]
152+
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
152153
public struct NFT
153154
{
154155
/// <summary>
@@ -181,6 +182,7 @@ public struct NFT
181182
/// Represents the metadata of an NFT.
182183
/// </summary>
183184
[Serializable]
185+
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
184186
public struct NFTMetadata
185187
{
186188
/// <summary>
@@ -213,6 +215,12 @@ public struct NFTMetadata
213215
[JsonProperty("name")]
214216
public string Name { get; set; }
215217

218+
/// <summary>
219+
/// Gets or sets the video URL of the NFT.
220+
/// </summary>
221+
[JsonProperty("video_url")]
222+
public string VideoUrl { get; set; }
223+
216224
/// <summary>
217225
/// Gets or sets the animation URL of the NFT.
218226
/// </summary>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Thirdweb.Indexer;
2+
3+
public static class ThirdwebInsightExtensions
4+
{
5+
public static NFT ToNFT(this Token_NFT token)
6+
{
7+
if (token == null)
8+
{
9+
return new NFT();
10+
}
11+
12+
return new NFT()
13+
{
14+
Type = token.Contract?.Type switch
15+
{
16+
"ERC721" => NFTType.ERC721,
17+
"ERC1155" => NFTType.ERC1155,
18+
_ => throw new Exception($"Unknown NFT type: {token.Contract.Type}")
19+
},
20+
Metadata = new NFTMetadata()
21+
{
22+
Id = token.TokenId,
23+
Description = token.Description,
24+
Image = token.ImageUrl,
25+
Name = token.Name,
26+
VideoUrl = token.VideoUrl,
27+
AnimationUrl = token.AnimationUrl,
28+
ExternalUrl = token.ExternalUrl,
29+
BackgroundColor = token.BackgroundColor,
30+
Attributes = token.ExtraMetadata?.Attributes,
31+
Properties = token.ExtraMetadata?.Properties,
32+
}
33+
};
34+
}
35+
36+
public static List<NFT> ToNFTList(this IEnumerable<Token_NFT> tokens)
37+
{
38+
if (tokens == null)
39+
{
40+
return new List<NFT>();
41+
}
42+
43+
return tokens.Select(token => token.ToNFT()).ToList();
44+
}
45+
}

0 commit comments

Comments
 (0)