Skip to content

Commit c3d2f8e

Browse files
authored
Insight Token Price API (#137)
1 parent 9b69090 commit c3d2f8e

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

Diff for: Thirdweb.Console/Program.cs

+9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@
4646
// // Create a ThirdwebInsight instance
4747
// var insight = await ThirdwebInsight.Create(client);
4848

49+
// var ethPriceToday = await insight.GetTokenPrice(addressOrSymbol: "ETH", chainId: 1);
50+
// Console.WriteLine($"ETH price today: {ethPriceToday.PriceUsd}");
51+
52+
// var ethPriceYesterday = await insight.GetTokenPrice(addressOrSymbol: "ETH", chainId: 1, timestamp: Utils.GetUnixTimeStampNow() - 86400);
53+
// Console.WriteLine($"ETH price yesterday: {ethPriceYesterday.PriceUsd}");
54+
55+
// var multiTokenPrices = await insight.GetTokenPrices(addressOrSymbols: new[] { "POL", "APE" }, chainIds: new BigInteger[] { 137, 33139 });
56+
// Console.WriteLine($"Multi token prices: {JsonConvert.SerializeObject(multiTokenPrices, Formatting.Indented)}");
57+
4958
// // Setup some filters
5059
// var address = await Utils.GetAddressFromENS(client, "vitalik.eth");
5160
// var chains = new BigInteger[] { 1, 137, 42161 };

Diff for: Thirdweb/Thirdweb.Indexer/ThirdwebInsight.Types.cs

+27
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ public class Meta
4747
public BigInteger TotalPages { get; set; }
4848
}
4949

50+
#region Price API
51+
52+
public class Token_Price
53+
{
54+
[JsonProperty("chain_id")]
55+
public BigInteger ChainId { get; set; }
56+
57+
[JsonProperty("address")]
58+
public string Address { get; set; }
59+
60+
[JsonProperty("symbol")]
61+
public string Symbol { get; set; }
62+
63+
[JsonProperty("price_usd")]
64+
public double PriceUsd { get; set; }
65+
66+
[JsonProperty("price_usd_cents")]
67+
public double PriceUsdCents { get; set; }
68+
69+
public override string ToString()
70+
{
71+
return JsonConvert.SerializeObject(this);
72+
}
73+
}
74+
75+
#endregion
76+
5077
#region Tokens API
5178

5279
public class Token_ERC20 : Token { }

Diff for: Thirdweb/Thirdweb.Indexer/ThirdwebInsight.cs

+38
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,44 @@ public static Task<ThirdwebInsight> Create(ThirdwebClient client)
4747
return Task.FromResult(new ThirdwebInsight(client));
4848
}
4949

50+
public async Task<Token_Price> GetTokenPrice(string addressOrSymbol, BigInteger chainId, long? timestamp = null)
51+
{
52+
var prices = await this.GetTokenPrices(new[] { addressOrSymbol }, new[] { chainId }, timestamp).ConfigureAwait(false);
53+
if (prices.Length == 0)
54+
{
55+
throw new Exception("Token price not found.");
56+
}
57+
return prices[0];
58+
}
59+
60+
public async Task<Token_Price[]> GetTokenPrices(string[] addressOrSymbols, BigInteger[] chainIds, long? timestamp = null)
61+
{
62+
var addresses = addressOrSymbols.Where(Utils.IsValidAddress).ToArray();
63+
var symbols = addressOrSymbols.Except(addresses).ToArray();
64+
65+
var url = AppendChains($"{Constants.INSIGHT_API_URL}/v1/tokens/price", chainIds);
66+
67+
if (addresses.Length > 0)
68+
{
69+
url += $"&address={string.Join("&address=", addresses)}";
70+
}
71+
72+
if (symbols.Length > 0)
73+
{
74+
url += $"&symbol={string.Join("&symbol=", symbols)}";
75+
}
76+
77+
if (timestamp.HasValue)
78+
{
79+
url += $"&timestamp={timestamp}";
80+
}
81+
82+
var response = await this._httpClient.GetAsync(url).ConfigureAwait(false);
83+
_ = response.EnsureSuccessStatusCode();
84+
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
85+
return JsonConvert.DeserializeObject<ResponseModel<Token_Price>>(responseContent).Data;
86+
}
87+
5088
/// <summary>
5189
/// Get the token balances of an address.
5290
/// </summary>

0 commit comments

Comments
 (0)