/// <summary>
 ///     To faucet drip dto
 /// </summary>
 /// <param name="drip">Source </param>
 /// <returns>Dto</returns>
 public static FaucetDripDto ToDto(this FaucetDrip drip)
 {
     return(new(ethAmount : drip.EthAmount, tokenAmount : drip.TokenAmount, drip.Transaction.ToDto()));
 }
Ejemplo n.º 2
0
        private async Task <IActionResult> RequestFromFaucetAsync(OpenFaucetDto request, CancellationToken cancellationToken)
        {
            try
            {
                JwtUser?jwtUser = this.JwtUser;

                if (jwtUser == null)
                {
                    return(ResultHelpers.ExceptionResult(message: "Not authorised (1).", nameof(FaucetController), statusCode: HttpStatusCode.Unauthorized));
                }

                IPAddress ipAddress = this._remoteIpAddressRetriever.Get(this.HttpContext);

                EthereumNetwork network = request.Network;

                if (!this._faucetContractInfo.IsSupported(network))
                {
                    return(ResultHelpers.ExceptionResult(message: "Faucet is not supported on network.", nameof(FaucetController), statusCode: HttpStatusCode.Forbidden));
                }

                INetworkBlockHeader?networkBlockHeader = this._ethereumBlockStatus.GetLatestBlockRetrievedOnNetwork(network);

                if (networkBlockHeader == null)
                {
                    return(ResultHelpers.ExceptionResult($"Network {network.Name} is not ready [BNF].", nameof(FaucetController), statusCode: HttpStatusCode.ServiceUnavailable));
                }

                if (this._networkManager.Supported.All(x => x.Name != network.Name))
                {
                    return(ResultHelpers.ExceptionResult(message: "Faucet is not supported on network.", nameof(FaucetController), statusCode: HttpStatusCode.Forbidden));
                }

                AccountAddress address = new(request.Address.ToSpan());

                if (jwtUser.AccountAddress != address)
                {
                    return(ResultHelpers.ExceptionResult(message: "Not authorised (2).", nameof(FaucetController), statusCode: HttpStatusCode.Unauthorized));
                }

                INetworkAccount account = new NetworkAccount(network: network, address: address);

                if (this._ethereumAccountManager.IsAccountConfiguredForUse(account))
                {
                    return(ResultHelpers.ExceptionResult(message: "Server cannot give itself token or eth.", nameof(FaucetController), statusCode: HttpStatusCode.Forbidden));
                }

                FaucetDrip drip = await this._faucetManager.OpenAsync(ipAddress : ipAddress, recipient : account, networkBlockHeader : networkBlockHeader, cancellationToken : cancellationToken);

                return(ResultHelpers.JsonResult(drip.ToDto(), statusCode: HttpStatusCode.OK));
            }
            catch (TooMuchTokenException)
            {
                return(ResultHelpers.ExceptionResult(message: "Too much token!", nameof(FaucetController), statusCode: HttpStatusCode.Forbidden));
            }
            catch (TooFrequentTokenException)
            {
                return(ResultHelpers.ExceptionResult(message: "Too much token!", nameof(FaucetController), statusCode: HttpStatusCode.Forbidden));
            }
            catch (InsufficientTokenException)
            {
                return(ResultHelpers.ExceptionResult(message: "Insufficient token!", nameof(FaucetController), statusCode: HttpStatusCode.Forbidden));
            }
        }