Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Thirdweb.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
// // Create a ThirdwebInsight instance
// var insight = await ThirdwebInsight.Create(client);

// var ethPriceToday = await insight.GetTokenPrice(addressOrSymbol: "ETH", chainId: 1);
// Console.WriteLine($"ETH price today: {ethPriceToday.PriceUsd}");

// var ethPriceYesterday = await insight.GetTokenPrice(addressOrSymbol: "ETH", chainId: 1, timestamp: Utils.GetUnixTimeStampNow() - 86400);
// Console.WriteLine($"ETH price yesterday: {ethPriceYesterday.PriceUsd}");

// var multiTokenPrices = await insight.GetTokenPrices(addressOrSymbols: new[] { "POL", "APE" }, chainIds: new BigInteger[] { 137, 33139 });
// Console.WriteLine($"Multi token prices: {JsonConvert.SerializeObject(multiTokenPrices, Formatting.Indented)}");

// // Setup some filters
// var address = await Utils.GetAddressFromENS(client, "vitalik.eth");
// var chains = new BigInteger[] { 1, 137, 42161 };
Expand Down
27 changes: 27 additions & 0 deletions Thirdweb/Thirdweb.Indexer/ThirdwebInsight.Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ public class Meta
public BigInteger TotalPages { get; set; }
}

#region Price API

public class Token_Price
{
[JsonProperty("chain_id")]
public BigInteger ChainId { get; set; }

[JsonProperty("address")]
public string Address { get; set; }

[JsonProperty("symbol")]
public string Symbol { get; set; }

[JsonProperty("price_usd")]
public double PriceUsd { get; set; }

[JsonProperty("price_usd_cents")]
public double PriceUsdCents { get; set; }

public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}

#endregion

#region Tokens API

public class Token_ERC20 : Token { }
Expand Down
38 changes: 38 additions & 0 deletions Thirdweb/Thirdweb.Indexer/ThirdwebInsight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,44 @@ public static Task<ThirdwebInsight> Create(ThirdwebClient client)
return Task.FromResult(new ThirdwebInsight(client));
}

public async Task<Token_Price> GetTokenPrice(string addressOrSymbol, BigInteger chainId, long? timestamp = null)
{
var prices = await this.GetTokenPrices(new[] { addressOrSymbol }, new[] { chainId }, timestamp).ConfigureAwait(false);
if (prices.Length == 0)
{
throw new Exception("Token price not found.");
}
return prices[0];
}

public async Task<Token_Price[]> GetTokenPrices(string[] addressOrSymbols, BigInteger[] chainIds, long? timestamp = null)
{
var addresses = addressOrSymbols.Where(Utils.IsValidAddress).ToArray();
var symbols = addressOrSymbols.Except(addresses).ToArray();

var url = AppendChains($"{Constants.INSIGHT_API_URL}/v1/tokens/price", chainIds);

if (addresses.Length > 0)
{
url += $"&address={string.Join("&address=", addresses)}";
}

if (symbols.Length > 0)
{
url += $"&symbol={string.Join("&symbol=", symbols)}";
}

if (timestamp.HasValue)
{
url += $"&timestamp={timestamp}";
}

var response = await this._httpClient.GetAsync(url).ConfigureAwait(false);
_ = response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<ResponseModel<Token_Price>>(responseContent).Data;
}

/// <summary>
/// Get the token balances of an address.
/// </summary>
Expand Down
Loading