public async Task <FaucetResponse> TryRequestEthAsync(string node, Address?address, UInt256 value) { if (!_enabled) { if (_logger.IsInfo) { _logger.Info("NDM Faucet is disabled."); } return(FaucetResponse.FaucetDisabled); } if (!_initialized) { if (_logger.IsInfo) { _logger.Info("NDM Faucet is not initialized."); } return(FaucetResponse.FaucetDisabled); } if (_today.Date != _timestamper.UtcNow.Date) { lock (_locker) { _today = _timestamper.UtcNow; _todayRequestsTotalValueWei = 0; } if (_logger.IsInfo) { _logger.Info($"NDM Faucet has updated its today's date ({_today.Date:d}) and reset the total requests value."); } } if (_faucetAddress is null || _faucetAddress == Address.Zero) { if (_logger.IsWarn) { _logger.Warn("NDM Faucet address is not set."); } return(FaucetResponse.FaucetAddressNotSet); } if (string.IsNullOrWhiteSpace(node) || address is null || address == Address.Zero) { if (_logger.IsInfo) { _logger.Info("Invalid NDM Faucet request."); } return(FaucetResponse.InvalidNodeAddress); } if (_faucetAddress == address) { if (_logger.IsInfo) { _logger.Info("NDM Faucet request cannot be processed for the same address as NDM Faucet."); } return(FaucetResponse.SameAddressAsFaucet); } if (value == 0) { if (_logger.IsInfo) { _logger.Info("NDM Faucet request cannot be processed for the zero value."); } return(FaucetResponse.ZeroValue); } if (value > _maxValue) { if (_logger.IsInfo) { _logger.Info($"NDM Faucet request from: {node} has too big value: {value} wei > {_maxValue} wei."); } return(FaucetResponse.TooBigValue); } if (_logger.IsInfo) { _logger.Info($"Received NDM Faucet request from: {node}, address: {address}, value: {value} wei."); } if (_pendingRequests.TryGetValue(node, out _)) { if (_logger.IsInfo) { _logger.Info($"NDM Faucet request from: {node} is already being processed."); } return(FaucetResponse.RequestAlreadyProcessing); } var latestRequest = await _requestRepository.GetLatestAsync(node); var requestedAt = _timestamper.UtcNow; if (!(latestRequest is null) && latestRequest.RequestedAt.Date >= requestedAt.Date) { if (_logger.IsInfo) { _logger.Info($"NDM Faucet request from: {node} was already processed today at: {latestRequest.RequestedAt}."); } return(FaucetResponse.RequestAlreadyProcessedToday(FaucetRequestDetails.From(latestRequest))); } if (!_pendingRequests.TryAdd(node, true)) { if (_logger.IsWarn) { _logger.Warn($"Couldn't start processing NDM Faucet request from: {node}."); } return(FaucetResponse.RequestError); } lock (_locker) { _todayRequestsTotalValueWei += value; if (_logger.IsInfo) { _logger.Info($"Increased NDM Faucet total value of today's ({_today.Date:d}) requests to {_todayRequestsTotalValueWei} wei."); } } if (_todayRequestsTotalValueWei > _dailyRequestsTotalValueWei) { if (_logger.IsInfo) { _logger.Info($"Daily ({_today.Date:d}) requests value for NDM Faucet was reached ({_dailyRequestsTotalValueWei} wei)."); } return(FaucetResponse.DailyRequestsTotalValueReached); } if (_logger.IsInfo) { _logger.Info($"NDM Faucet is processing request for: {node}, address: {address}, value: {value} wei."); } try { var nonce = await _blockchainBridge.GetNonceAsync(_faucetAddress); var transaction = new Transaction { Value = value, GasLimit = Transaction.BaseTxGasCost, GasPrice = 20.GWei(), To = address, SenderAddress = _faucetAddress, Nonce = nonce }; _wallet.Sign(transaction, await _blockchainBridge.GetNetworkIdAsync()); Keccak?transactionHash = await _blockchainBridge.SendOwnTransactionAsync(transaction); if (transactionHash == null) { return(FaucetResponse.ProcessingRequestError); } if (latestRequest is null) { Keccak requestId = Keccak.Compute(Rlp.Encode(Rlp.Encode(node)).Bytes); latestRequest = new EthRequest(requestId, node, address, value, requestedAt, transactionHash); await _requestRepository.AddAsync(latestRequest); } else { latestRequest.UpdateRequestDetails(address, value, requestedAt, transactionHash); await _requestRepository.UpdateAsync(latestRequest); } if (_logger.IsInfo) { _logger.Info($"NDM Faucet has successfully processed request for: {node}, address: {address}, value: {value} wei."); } return(FaucetResponse.RequestCompleted(FaucetRequestDetails.From(latestRequest))); } catch (Exception ex) { if (_logger.IsError) { _logger.Error(ex.Message, ex); } lock (_locker) { _todayRequestsTotalValueWei -= value; if (_logger.IsInfo) { _logger.Info($"Decreased NDM Faucet total value of today's ({_today.Date:d}) requests to {_todayRequestsTotalValueWei} wei."); } } return(FaucetResponse.ProcessingRequestError); } finally { _pendingRequests.TryRemove(node, out _); } }
public async Task <bool> TryRequestEthAsync(string host, Address address, UInt256 value) { if (!_enabled) { if (_logger.IsInfo) { _logger.Info("NDM Faucet is disabled"); } return(false); } if (_faucetAddress is null || _faucetAddress == Address.Zero) { if (_logger.IsWarn) { _logger.Warn("NDM Faucet address is not set"); } return(false); } if (string.IsNullOrWhiteSpace(host) || address is null || address == Address.Zero || _faucetAddress == address || value == 0) { if (_logger.IsInfo) { _logger.Info("Invalid NDM Faucet ETH request"); } return(false); } if (value > _maxValue) { if (_logger.IsInfo) { _logger.Info($"ETH request from: {host} has too big value: {value} wei > {_maxValue} wei"); } return(false); } if (_pendingRequests.TryGetValue(host, out _)) { if (_logger.IsInfo) { _logger.Info($"ETH request from: {host} is already being processed."); } return(false); } if (_logger.IsInfo) { _logger.Info($"Received ETH request from: {host}, address: {address}, value: {value} wei"); } var latestRequest = await _requestRepository.GetLatestAsync(host); var requestedAt = _timestamp.UtcNow; if (!(latestRequest is null) && latestRequest.RequestedAt.Date >= requestedAt.Date) { if (_logger.IsInfo) { _logger.Info($"ETH request from: {host} was already processed today at: {latestRequest.RequestedAt}"); } return(false); } if (!_pendingRequests.TryAdd(host, true)) { if (_logger.IsWarn) { _logger.Warn($"Couldn't start processing ETH request from: {host}"); } return(false); } if (_logger.IsInfo) { _logger.Info($"Processing ETH request for: {host}, address: {address}, value: {value} wei"); } try { var faucetAccount = _blockchainBridge.GetAccount(_faucetAddress); var transaction = new Transaction { Value = value, GasLimit = 21000, GasPrice = 20.GWei(), To = address, SenderAddress = _faucetAddress, Nonce = faucetAccount?.Nonce ?? 0 }; _blockchainBridge.Sign(transaction); var transactionHash = _blockchainBridge.SendTransaction(transaction); if (latestRequest is null) { var requestId = Keccak.Compute(Rlp.Encode(Rlp.Encode(host))); await _requestRepository.AddAsync(new EthRequest(requestId, host, address, value, requestedAt, transactionHash)); } else { latestRequest.UpdateRequestDate(requestedAt); await _requestRepository.UpdateAsync(latestRequest); } if (_logger.IsInfo) { _logger.Info($"ETH request was successfully processed for: {host}, address: {address}, value: {value} wei"); } return(true); } catch (Exception e) { if (_logger.IsError) { _logger.Error(e.Message, e); } return(false); } finally { _pendingRequests.TryRemove(host, out _); } }