Esempio n. 1
0
        public async Task<IActionResult> transact()
        {
            try
            {
                using var reader = new StreamReader(Request.Body);
                var parsed_body = JsonConvert.DeserializeObject<dynamic>(await reader.ReadToEndAsync());

                Transaction transaction;
                try
                {
                    string recipient = parsed_body.recipient?.ToString() ?? throw new ArgumentException("Invalid recipient supplied.");

                    if (!long.TryParse(parsed_body.amount?.ToString(), out long amount)) throw new ArgumentException("Invalid recipient supplied.");

                    transaction = _ITransactionPool.ExistingTransaction(_IWallet.PublicKey);

                    if (transaction == null)
                        transaction = _IWallet.CreateTransaction(recipient, amount,_IBlockChain.LocalChain);
                    else
                        transaction.Update(_IWallet, recipient, amount);
                }
                catch (Exception ex)
                {
                    return StatusCode(StatusCodes.Status400BadRequest, new ApiResponse { message = ex.Message });
                }

                _ITransactionPool.SetTransaction(transaction);

                await _IRedis.BroadcastTransaction(transaction);

                return StatusCode(StatusCodes.Status200OK, transaction);
            }
            catch (Exception ex)
            {
                _ILogger.LogError(ex, $"~/api/transact requested from {HttpContext.GetIP()}. Payload : { HttpContext.Request.Body}");

                if (IsDebugging)
                    return StatusCode(StatusCodes.Status500InternalServerError, new ApiResponse { message = ex.Message, data = ex.ToReleventInfo() });
                else
                    return StatusCode(StatusCodes.Status500InternalServerError, new ApiResponse { message = ex.Message });
            }

        }