public async Task <JsonResult> ImportSyncAPI(int apiId, int portfolioId) { ExchangeApiInfo exchangeApiInfo = CurrentUser.ExchangeApiList.FirstOrDefault(x => x.Id == apiId); if (exchangeApiInfo == null) { return(Json(ResultsItem.Error("This API Id cannot be found"))); } if (!CurrentUser.HasPortfolio(portfolioId)) { return(Json(ResultsItem.Error(Lang.PortfolioNotFound))); } // Test -> Move to .Tests solution //List<Core.Data.ServiceModels.ImportedCoin> importedCoins = FetchAPILogic.ImportCSV_Coins(exchangeApiInfo.Exchange, // System.IO.File.OpenRead(@"C:\Users\Ref\Downloads\pegatrade_importFiles\api_test1\2nd-half.csv")); List <Core.Data.ServiceModels.ImportedCoin> importedCoins = await FetchAPILogic.ImportAPI_Coins(exchangeApiInfo, CurrentUser); if (importedCoins.IsNullOrEmpty()) { return(Json(ResultsItem.Error(Lang.UnableToImportWrongKeyPermission))); } List <CryptoCoin> existingExchangeCoins = (await CryptoLogic.GetAllUserCoinsByPortfolioIdAsync(portfolioId)).Where(x => x.Exchange == exchangeApiInfo.Exchange).ToList(); DateTime latestDate = existingExchangeCoins.IsNullOrEmpty() ? DateTime.MinValue : existingExchangeCoins.Max(x => x.OrderDate); // Get rid of ANY imported coins that are less than already existing date. // We dont' want to mess with those, we just want to add new ones. importedCoins = importedCoins.Where(x => x.OrderDate > latestDate).ToList(); if (importedCoins.IsNullOrEmpty()) { Json(ResultsItem.Error(Lang.ApiAlreadyUptoDate)); } List <CryptoCoin> fetchedCoins = FetchAPILogic.FormatCoinsAndGenerateTotalPricePaid(importedCoins, await GetAllHistoricCoinPrices()); if (fetchedCoins.Count > 0) { // Get all current holding coins that will be affected by new coins. Delete them from db as well, we'll re-add them. existingExchangeCoins = existingExchangeCoins.Where(x => x.OrderType == Types.OrderType.Buy && fetchedCoins.Any(f => f.Symbol.EqualsTo(x.Symbol))).ToList(); await CryptoLogic.DeleteUserCoinsAsync(existingExchangeCoins, CurrentUser); existingExchangeCoins.ForEach(x => x.CoinId = 0); // Reset PK so DB can generate a new one. existingExchangeCoins.AddRange(fetchedCoins); existingExchangeCoins = CryptoLogic.FormatCoinsAndBoughtSoldLogicUpdate(existingExchangeCoins); ResultsItem insertResults = await CryptoLogic.InsertCoinsToUserPortfolioAsync(existingExchangeCoins, CurrentUser, portfolioId); if (insertResults.IsSuccess) { return(Json(ResultsItem.Success("Successfully imported coins to your portfolio."))); } } return(Json(ResultsItem.Error(Lang.ApiNoNewTradesOrError))); }
public static ResultsItem InsertNewAPI(ExchangeApiInfo exchangeApiInfo, PegaUser user) { var validateRequest = IsValidDBRequest(user, 0); if (!validateRequest.IsSuccess) { return(validateRequest); } return(CryptoRepository.InsertNewAPI(exchangeApiInfo, user)); }
public static async Task <List <ImportedCoin> > ImportAPI_Coins(ExchangeApiInfo exchangeApiInfo, PegaUser user) { switch (exchangeApiInfo.Exchange) { case Types.Exchanges.BitTrex: return(await FetchAPIRepository.ApiImport_BitTrex(exchangeApiInfo.ApiPublic, exchangeApiInfo.ApiPrivate, user)); case Types.Exchanges.GDax: return(await FetchAPIRepository.ApiImport_GDax(exchangeApiInfo.ApiPublic, exchangeApiInfo.ApiPrivate, exchangeApiInfo.ApiThirdKey, user)); } return(new List <ImportedCoin>()); }
public JsonResult CreateNewImportAPI(ExchangeApiInfo exchangeApiInfo) { if (!ModelState.IsValid) { return(Json(ResultsItem.Error(ModelState.GetAllErrorsString()))); } exchangeApiInfo.ApiAction = Types.ApiAction.TradesImport; exchangeApiInfo.ApiPrivate = exchangeApiInfo.ApiPrivate.Replace("%2B", "+"); ResultsItem apiResult = CryptoLogic.InsertNewAPI(exchangeApiInfo, CurrentUser); return(Json(apiResult)); }
public async Task <JsonResult> ImportEtherAddress(ExchangeApiInfo apiInfo) { var resultsPair = await FetchAPILogic.ApiImport_EtherAddress(await GetAllCoinsMarketDetailsAPI(), apiInfo.ApiPublic, apiInfo.PortfolioID, CurrentUser); if (!resultsPair.Result.IsSuccess) { return(Json(resultsPair.Result)); } await CryptoLogic.DeleteAllUserCoinByExchangeAsync(apiInfo.PortfolioID, Types.Exchanges.EtherAddressLookup, CurrentUser); ResultsItem insertResult = await CryptoLogic.InsertCoinsToUserPortfolioAsync(resultsPair.Value, CurrentUser, apiInfo.PortfolioID); return(Json(insertResult)); }