public async Task <IActionResult> Post([FromBody] ExchangeTransferRequest request) { var isValid = await ValidateRequest(); if ((isValid as OkResult)?.StatusCode != Ok().StatusCode) { return(isValid); } if (request == null) { return(BadRequest("Incorrect Json request")); } if (request.PaidAmount <= 0) { return(Json( new TransferErrorReturn { TransferResponse = new TransferErrorResponse { TransferError = TransferError.INVALID_AMOUNT, TimeStamp = DateTime.UtcNow.Ticks } })); } var store = request.GetRequest(); var rate = await GetRatesWithSession(store.AssetPair, new AprRequest { Markup = new Lykke.Contracts.Pay.AssertPairRateRequest { Percent = store.Markup.Percent ?? 0, Pips = store.Markup.Pips ?? 0 } }); if (rate == null) { return(null); } var newSessionId = rate.SessionId; if (!string.IsNullOrEmpty(MerchantSessionId) && !MerchantSessionId.Equals(newSessionId)) { throw new InvalidDataException("Session expired"); } StoreNewSessionId(newSessionId); store.Amount = store.Amount / rate.Ask; var resultTransfer = await PostTransfer(request.AssetPair.Replace(request.BaseAsset, string.Empty), store); return(resultTransfer); }
public async Task <IActionResult> Purchase([FromBody] PurchaseRequest request) { var isValid = await ValidateRequest(); if ((isValid as OkResult)?.StatusCode != Ok().StatusCode) { return(isValid); } var store = request.GetRequest(); var rate = await GetRatesWithSession(store.AssetPair, new AprRequest { Markup = new AssertPairRateRequest { Percent = store.Markup.Percent ?? 0, Pips = store.Markup.Pips ?? 0 } }); if (rate == null) { await Log.WriteWarningAsync(nameof(PurchaseController), nameof(Purchase), LogContextPayRequest(store), $"Not found rate for {request.AssetPair}, return internal error"); return(await JsonAndStoreError(store, new TransferErrorReturn { TransferResponse = new TransferErrorResponse { TransferError = TransferError.INTERNAL_ERROR, TimeStamp = DateTime.UtcNow.Ticks } })); } var newSessionId = rate.SessionId; if (!string.IsNullOrEmpty(MerchantSessionId) && !MerchantSessionId.Equals(newSessionId)) { throw new InvalidDataException("Session expired"); } var pairReal = await GetRate(request.AssetPair); if (pairReal == null) { await Log.WriteWarningAsync(nameof(PurchaseController), nameof(Purchase), LogContextPayRequest(store), $"Not found asset pair {request.AssetPair}, return internal error"); return(await JsonAndStoreError(store, new TransferErrorReturn { TransferResponse = new TransferErrorResponse { TransferError = TransferError.INTERNAL_ERROR, TimeStamp = DateTime.UtcNow.Ticks } })); } string merchantClientId = string.Empty; try { merchantClientId = await(await HttpClient.GetAsync($"{PayApiSettings.Services.MerchantClientService}{MerchantId}")) .Content .ReadAsStringAsync(); } catch { } if (string.IsNullOrEmpty(merchantClientId)) { await Log.WriteWarningAsync(nameof(PurchaseController), nameof(Purchase), LogContextPayRequest(store), $"Not found merchantClientId for {MerchantId}"); return(await JsonAndStoreError(store, new TransferErrorReturn { TransferResponse = new TransferErrorResponse { TransferError = TransferError.INTERNAL_ERROR, TimeStamp = DateTime.UtcNow.Ticks } })); } double realAmount; if (request.Markup != null) { double fee = (double)request.PaidAmount * (request.Markup.Percent / 100); fee += Math.Pow(10, -1 * rate.Accuracy) * request.Markup.Pips; fee += request.Markup.FixedFee; realAmount = (double)request.PaidAmount - fee; } else { realAmount = (double)request.PaidAmount; } double outSpread = rate.Ask + (rate.Ask - rate.Bid) * PayApiSettings.SpredK; double reakAsk = (double)((JObject)pairReal)["AskPrice"]; double featToSell = realAmount * (reakAsk / outSpread); double btcToBuy = featToSell / reakAsk; store.Amount = btcToBuy; store.SourceAddress = PayApiSettings.HotWalletAddress; store.MerchantId = MerchantId; var post = await PostTransferRaw(BitcoinAssert, store); var result = post as IActionResult; if (result != null) { return(result); } string fiatAssert = request.AssetPair.Replace(BitcoinAssert, string.Empty); //try //{ var transResult = await _exchangeOperationClient.TrustedTransferAsync(PayApiSettings.LykkePayId, merchantClientId, realAmount, fiatAssert); //TODO Add logs here if wrong; var trageResult = await _exchangeOperationClient.TrustedTradeAsync(PayApiSettings.LykkePayId, request.AssetPair, featToSell, fiatAssert); //TODO Add logs here if wrong; //} //catch (Exception e) //{ // Console.WriteLine(e); // throw; //} return(Json(post)); }