public async Task <GlobalData> GlobalAsync(FiatCurrency convert = FiatCurrency.USD) { GlobalData data = new GlobalData(); try { HttpClient _httpClient = new HttpClient { BaseAddress = new Uri(Endpoints.CoinMarketCapApiUrl) }; var url = QueryStringService.AppendQueryString(Endpoints.GlobalData, new Dictionary <string, string> { { "convert", convert.ToString() } }); var response = await _httpClient.GetAsync(url); data = await JsonParserService.ParseResponse <GlobalData>(response); if (data == null) { data = new GlobalData { Data = null, Metadata = new GlobalMetadata { Error = int.MinValue }, Success = false }; } data.Success = data.Metadata.Error == null; } catch { } return(data); }
public async Task <CoinMarketCap.Pro.Model.GlobalMetrics.QuotesData> GlobalMetricsQuotesLatestAsync(FiatCurrency convert = FiatCurrency.USD) { HttpClient _httpClient = new HttpClient { BaseAddress = new Uri(Pro.Config.GlobalMetrics.CoinMarketCapProApiUrl) }; _httpClient.DefaultRequestHeaders.Add("X-CMC_PRO_API_KEY", this.ProApiKey); var url = QueryStringService.AppendQueryString(Pro.Config.GlobalMetrics.GlobalMetricsLatest, new Dictionary <string, string> { { "convert", convert.ToString() }, }); var response = await _httpClient.GetAsync(url); CoinMarketCap.Pro.Model.GlobalMetrics.QuotesData data = await JsonParserService.ParseResponse <CoinMarketCap.Pro.Model.GlobalMetrics.QuotesData>(response); if (data == null) { data = new CoinMarketCap.Pro.Model.GlobalMetrics.QuotesData { Data = null, Status = new Status { ErrorCode = int.MinValue }, Success = false }; } data.Success = data.Status.ErrorCode == 0; // Add to Status List this.statusList.Add(data.Status); return(data); }
public async Task <Pro.Model.Cryptocurrency.ListingsData> CryptocurrencyListingsHistoricalAsync( DateTime timestamp, int?start = 1, int?limit = 100, FiatCurrency convert = FiatCurrency.USD, SortBy sort = SortBy.market_cap, SortDirection sort_dir = SortDirection.desc, CryptocurrencyType cryptocurrency_type = CryptocurrencyType.all ) { HttpClient _httpClient = new HttpClient { BaseAddress = new Uri(Pro.Config.Cryptocurrency.CoinMarketCapProApiUrl) }; _httpClient.DefaultRequestHeaders.Add("X-CMC_PRO_API_KEY", this.ProApiKey); var url = QueryStringService.AppendQueryString(Pro.Config.Cryptocurrency.CryptocurrencyListingsHistorical, new Dictionary <string, string> { { "timestamp", timestamp.ToUnixTimeSeconds().ToString() }, { "start", start.HasValue && start.Value >= 1 ? start.Value.ToString() : null }, { "limit", limit.HasValue && limit.Value >= 1 ? limit.Value.ToString() : null }, { "convert", convert.ToString() }, { "sort", sort.ToString() }, { "sort_dir", sort_dir.ToString() }, { "cryptocurrency_type", cryptocurrency_type.ToString() }, }); var response = await _httpClient.GetAsync(url); Pro.Model.Cryptocurrency.ListingsData data = await JsonParserService.ParseResponse <Pro.Model.Cryptocurrency.ListingsData>(response); if (data == null) { data = new Pro.Model.Cryptocurrency.ListingsData { Data = null, Status = new Status { ErrorCode = int.MinValue }, Success = false }; } data.Success = data.Status.ErrorCode == 0; // Add to Status List this.statusList.Add(data.Status); return(data); }
public async Task <TickersData> TickersAsync(int?start, int?limit, SortBy sort = SortBy.id, FiatCurrency convert = FiatCurrency.USD) { TickersData data = new TickersData(); try { HttpClient _httpClient = new HttpClient { BaseAddress = new Uri(Endpoints.CoinMarketCapApiUrl) }; var url = QueryStringService.AppendQueryString(Endpoints.Ticker, new Dictionary <string, string> { { "start", start >= 1 ? start.ToString() : null }, { "limit", limit >= 1 ? limit.ToString() : null }, { "sort", sort.ToString() }, { "convert", convert.ToString() } }); var response = await _httpClient.GetAsync(url); data = await JsonParserService.ParseResponse <TickersData>(response); if (data == null) { data = new TickersData { Data = null, Metadata = new TickerMetadata { Error = int.MinValue }, Success = false }; } data.Success = data.Metadata.Error == null; } catch { } return(data); }
// --- // 1234.40 => 1,234.40 USD public static string FormatAmount(long cents, FiatCurrency currency) { return((cents / 100m).ToString("N2", System.Globalization.CultureInfo.InvariantCulture) + " " + currency.ToString().ToUpperInvariant()); }
/// <summary> /// Get orderbook for specify markets. /// </summary> /// <param name="crypto"></param> /// <param name="fiat"></param> /// <returns></returns> public OrderBookModel OrderBook(CryptoCurrency crypto, FiatCurrency fiat) { IRestRequest req = new RestRequest($"/stats/orderbook_{crypto.ToString().ToLower()}{fiat.ToString().ToLower()}.json"); string response = client.Execute(req).Content; OrderBookModel orderbook = JsonConvert.DeserializeObject <OrderBookModel>(response); return(orderbook); }
private async Task <EstimationResult> Estimation( RuntimeConfig rcfg, BigInteger inputAmount, TradableCurrency?ethereumToken, FiatCurrency fiatCurrency, bool reversed, double discount, BigInteger depositLimitMin, BigInteger depositLimitMax ) { bool allowed = false; var centsPerAsset = 0L; var centsPerGold = 0L; var resultCurrencyAmount = BigInteger.Zero; var resultGoldAmount = BigInteger.Zero; object viewAmount = null; var viewAmountCurrency = ""; var limitsData = (EstimateLimitsView)null; // default estimation: specified currency to GOLD if (!reversed) { // fiat if (ethereumToken == null) { var res = await CoreLogic.Finance.Estimation.BuyGoldFiat( services : HttpContext.RequestServices, fiatCurrency : fiatCurrency, fiatAmountCents : (long)inputAmount, discount : discount ); allowed = res.Allowed; centsPerGold = res.CentsPerGoldRate; resultCurrencyAmount = inputAmount; resultGoldAmount = res.ResultGoldAmount; viewAmount = resultGoldAmount.ToString(); viewAmountCurrency = "GOLD"; limitsData = new EstimateLimitsView() { Currency = fiatCurrency.ToString().ToUpper(), Min = (long)depositLimitMin / 100d, Max = (long)depositLimitMax / 100d, Cur = (long)resultCurrencyAmount / 100d, }; } // cryptoasset else { var res = await CoreLogic.Finance.Estimation.BuyGoldCrypto( services : HttpContext.RequestServices, ethereumToken : ethereumToken.Value, fiatCurrency : fiatCurrency, cryptoAmount : inputAmount, discount : discount ); allowed = res.Allowed; centsPerGold = res.CentsPerGoldRate; centsPerAsset = res.CentsPerAssetRate; resultCurrencyAmount = inputAmount; resultGoldAmount = res.ResultGoldAmount; viewAmount = resultGoldAmount.ToString(); viewAmountCurrency = "GOLD"; limitsData = new EstimateLimitsView() { Currency = fiatCurrency.ToString().ToUpper(), Min = depositLimitMin.ToString(), Max = depositLimitMax.ToString(), Cur = resultCurrencyAmount.ToString(), }; } } // reversed estimation: GOLD to specified currency else { // fiat if (ethereumToken == null) { var res = await CoreLogic.Finance.Estimation.BuyGoldFiatRev( services : HttpContext.RequestServices, fiatCurrency : fiatCurrency, requiredGoldAmount : inputAmount, discount : discount ); allowed = res.Allowed; centsPerGold = res.CentsPerGoldRate; resultCurrencyAmount = res.ResultCentsAmount; resultGoldAmount = res.ResultGoldAmount; viewAmount = (long)resultCurrencyAmount / 100d; viewAmountCurrency = fiatCurrency.ToString().ToUpper(); limitsData = new EstimateLimitsView() { Currency = fiatCurrency.ToString().ToUpper(), Min = (long)depositLimitMin / 100d, Max = (long)depositLimitMax / 100d, Cur = (long)resultCurrencyAmount / 100d, }; } // cryptoasset else { var res = await CoreLogic.Finance.Estimation.BuyGoldCryptoRev( services : HttpContext.RequestServices, ethereumToken : ethereumToken.Value, fiatCurrency : fiatCurrency, requiredGoldAmount : inputAmount, discount : discount ); allowed = res.Allowed; centsPerGold = res.CentsPerGoldRate; centsPerAsset = res.CentsPerAssetRate; resultCurrencyAmount = res.ResultAssetAmount; resultGoldAmount = res.ResultGoldAmount; viewAmount = resultCurrencyAmount.ToString(); viewAmountCurrency = ethereumToken.Value.ToString().ToUpper(); limitsData = new EstimateLimitsView() { Currency = fiatCurrency.ToString().ToUpper(), Min = depositLimitMin.ToString(), Max = depositLimitMax.ToString(), Cur = resultCurrencyAmount.ToString(), }; } } var limitExceeded = resultCurrencyAmount <depositLimitMin || resultCurrencyAmount> depositLimitMax; return(new EstimationResult() { TradingAllowed = allowed, IsLimitExceeded = limitExceeded, View = new EstimateView() { Amount = viewAmount, AmountCurrency = viewAmountCurrency, Limits = limitsData, }, CentsPerAssetRate = centsPerAsset, CentsPerGoldRate = centsPerGold, ResultCurrencyAmount = resultCurrencyAmount, ResultGoldAmount = resultGoldAmount, }); }
private async Task <EstimationResult> Estimation(RuntimeConfig rcfg, BigInteger inputAmount, TradableCurrency?ethereumToken, FiatCurrency fiatCurrency, string ethAddress, bool reversed, BigInteger withdrawalLimitMin, BigInteger withdrawalLimitMax) { var allowed = false; var centsPerAsset = 0L; var centsPerGold = 0L; var resultGoldAmount = BigInteger.Zero; var resultCurrencyAmount = BigInteger.Zero; var resultCurrencyAmountMinusFee = BigInteger.Zero; object viewAmount = null; var viewAmountCurrency = ""; object viewFee = null; var viewFeeCurrency = ""; var limitsData = (EstimateLimitsView)null; // default estimation: GOLD to specified currency if (!reversed) { // fiat if (ethereumToken == null) { var res = await CoreLogic.Finance.Estimation.SellGoldFiat( services : HttpContext.RequestServices, fiatCurrency : fiatCurrency, goldAmount : inputAmount ); allowed = res.Allowed; centsPerGold = res.CentsPerGoldRate; resultGoldAmount = res.ResultGoldAmount; resultCurrencyAmount = res.ResultCentsAmount; var mntBalance = ethAddress != null ? await EthereumObserver.GetAddressMntBalance(ethAddress) : BigInteger.Zero; var fee = CoreLogic.Finance.Estimation.SellingFeeForFiat(res.ResultCentsAmount, mntBalance); resultCurrencyAmountMinusFee = res.ResultCentsAmount - fee; viewAmount = (long)resultCurrencyAmountMinusFee / 100d; viewAmountCurrency = fiatCurrency.ToString().ToUpper(); viewFee = fee / 100d; viewFeeCurrency = fiatCurrency.ToString().ToUpper(); limitsData = new EstimateLimitsView() { Currency = fiatCurrency.ToString().ToUpper(), Min = (long)withdrawalLimitMin / 100d, Max = (long)withdrawalLimitMax / 100d, Cur = (long)resultCurrencyAmountMinusFee / 100d, }; } // cryptoasset else { var res = await CoreLogic.Finance.Estimation.SellGoldCrypto( services : HttpContext.RequestServices, ethereumToken : ethereumToken.Value, fiatCurrency : fiatCurrency, goldAmount : inputAmount ); allowed = res.Allowed; centsPerGold = res.CentsPerGoldRate; centsPerAsset = res.CentsPerAssetRate; resultGoldAmount = res.ResultGoldAmount; resultCurrencyAmount = res.ResultAssetAmount; var fee = CoreLogic.Finance.Estimation.SellingFeeForCrypto(ethereumToken.Value, res.ResultAssetAmount); resultCurrencyAmountMinusFee = res.ResultAssetAmount - fee; viewAmount = resultCurrencyAmountMinusFee.ToString(); viewAmountCurrency = ethereumToken.Value.ToString().ToUpper(); viewFee = fee.ToString(); viewFeeCurrency = ethereumToken.Value.ToString().ToUpper(); limitsData = new EstimateLimitsView() { Currency = fiatCurrency.ToString().ToUpper(), Min = withdrawalLimitMin.ToString(), Max = withdrawalLimitMax.ToString(), Cur = resultCurrencyAmountMinusFee.ToString(), }; } } // reversed estimation: specified currency to GOLD else { // fiat if (ethereumToken == null) { var mntBalance = ethAddress != null ? await EthereumObserver.GetAddressMntBalance(ethAddress) : BigInteger.Zero; var fee = CoreLogic.Finance.Estimation.SellingFeeForFiat((long)inputAmount, mntBalance); var res = await CoreLogic.Finance.Estimation.SellGoldFiatRev( services : HttpContext.RequestServices, fiatCurrency : fiatCurrency, requiredFiatAmountWithFeeCents : (long)inputAmount + fee ); allowed = res.Allowed; centsPerGold = res.CentsPerGoldRate; resultGoldAmount = res.ResultGoldAmount; resultCurrencyAmount = res.ResultCentsAmount; resultCurrencyAmountMinusFee = inputAmount; viewAmount = res.ResultGoldAmount.ToString(); viewAmountCurrency = "GOLD"; viewFee = fee.ToString(); viewFeeCurrency = fiatCurrency.ToString().ToUpper(); limitsData = new EstimateLimitsView() { Currency = fiatCurrency.ToString().ToUpper(), Min = (long)withdrawalLimitMin / 100d, Max = (long)withdrawalLimitMax / 100d, Cur = (long)resultCurrencyAmountMinusFee / 100d, }; } // cryptoasset else { var fee = CoreLogic.Finance.Estimation.SellingFeeForCrypto(ethereumToken.Value, inputAmount); var res = await CoreLogic.Finance.Estimation.SellGoldCryptoRev( services : HttpContext.RequestServices, ethereumToken : ethereumToken.Value, fiatCurrency : fiatCurrency, requiredCryptoAmountWithFee : inputAmount + fee ); allowed = res.Allowed; centsPerGold = res.CentsPerGoldRate; centsPerAsset = res.CentsPerAssetRate; resultGoldAmount = res.ResultGoldAmount; resultCurrencyAmount = res.ResultAssetAmount; resultCurrencyAmountMinusFee = inputAmount; viewAmount = res.ResultGoldAmount.ToString(); viewAmountCurrency = "GOLD"; viewFee = fee.ToString(); viewFeeCurrency = ethereumToken.Value.ToString().ToUpper(); limitsData = new EstimateLimitsView() { Currency = fiatCurrency.ToString().ToUpper(), Min = withdrawalLimitMin.ToString(), Max = withdrawalLimitMax.ToString(), Cur = resultCurrencyAmountMinusFee.ToString(), }; } } var limitExceeded = resultCurrencyAmountMinusFee <withdrawalLimitMin || resultCurrencyAmountMinusFee> withdrawalLimitMax; return(new EstimationResult() { TradingAllowed = allowed, IsLimitExceeded = limitExceeded, View = new EstimateView() { Amount = viewAmount, AmountCurrency = viewAmountCurrency, Fee = viewFee, FeeCurrency = viewFeeCurrency, Limits = limitsData, }, CentsPerAssetRate = centsPerAsset, CentsPerGoldRate = centsPerGold, ResultGoldAmount = resultGoldAmount, ResultCurrencyAmount = resultCurrencyAmount, }); }