Ejemplo n.º 1
0
        public async Task <IActionResult> RequestTokens([FromRoute] string address, [FromServices] WalletClient walletClient, [FromServices] IOptions <Configuration.Token> tokenOptions)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (Client.WalletAddress.Decode58Check(address) == null)
            {
                return(NotFound());
            }

            var result = new RequestTokensResult()
            {
                Result          = RequestTokensResult.Type.MaximumReachedPerDay,
                TransactionHash = null,
            };

            var ipAddress           = HttpContext.Connection.RemoteIpAddress.ToString();
            var receivedTokensToday = _context.UserReceivedTokens.Where(rt =>
                                                                        (rt.Address == address || rt.Ip == ipAddress) &&
                                                                        rt.CreatedAt.Date == DateTime.Today
                                                                        ).ToList();
            var sumTokensReceivedToday = receivedTokensToday.Sum(rt => rt.ReceivedTokens);

            if (sumTokensReceivedToday < tokenOptions.Value.TransferMaxPerAccountEveryDay)
            {
                try
                {
                    var userAccount = await walletClient.GetAddressInformation(address);

                    var asset = userAccount.AssetV2.SingleOrDefault(a => a.Key.Equals(tokenOptions.Value.Id, StringComparison.CurrentCultureIgnoreCase));

                    if (asset.Key == null || asset.Value <= tokenOptions.Value.TransferOnlyWhenHasLessOrEqualThan)
                    {
                        var canReceiveToday = (tokenOptions.Value.TransferMaxPerAccountEveryDay - sumTokensReceivedToday);
                        var amount          = (canReceiveToday <= tokenOptions.Value.TransferSteps
                                                    ? canReceiveToday
                                                    : tokenOptions.Value.TransferSteps
                                               );

                        var transferResult = await walletClient.TransferTokenFromProperty(amount, address);

                        if (transferResult.Result)
                        {
                            var transactionHash = Common.Utils
                                                  .ToHexString(Crypto.Sha256.Hash(transferResult.Transaction.RawData.ToByteArray()))
                                                  .ToLower();

                            result.TransactionHash = transactionHash;
                            result.Result          = RequestTokensResult.Type.Transferred;

                            _context.UserReceivedTokens.Add(new UserReceivedToken()
                            {
                                Address        = address,
                                Ip             = ipAddress,
                                ReceivedTokens = amount,
                                CreatedAt      = DateTime.UtcNow,
                            });
                            _context.SaveChanges();
                        }
                        else
                        {
                            result.Result = RequestTokensResult.Type.ServerError;
                        }
                    }
                    else
                    {
                        result.Result = RequestTokensResult.Type.HasEnough;
                    }
                }
                catch (RpcException e)
                {
                    result.Result = RequestTokensResult.Type.ServerError;
                    _logger.LogError("Couldn't transfer tokens to {Receiver}! Error:", address);
                    _logger.LogError(e.Message);
                    _logger.LogError(e.StackTrace);
                }
                catch (DbUpdateException e)
                {
                    _logger.LogError("Couldn't save the userReceivedToken in Database! Error:");
                    _logger.LogError(e.Message);
                    _logger.LogError(e.StackTrace);
                }
            }

            return(Ok(result));
        }