Exemple #1
0
        public async Task <IActionResult> CreateInvoice(int chainId)
        {
            var current = await _settingsRepository.GetSettingAsync <EthereumLikeConfiguration>(
                EthereumLikeConfiguration.SettingsKey(chainId));

            current ??= new EthereumLikeConfiguration()
            {
                ChainId = chainId
            };
            if (!string.IsNullOrEmpty(current?.InvoiceId) &&
                Request.Method.Equals("get", StringComparison.InvariantCultureIgnoreCase))
            {
                return(View("Confirm",
                            new ConfirmModel()
                {
                    Title = $"Generate new donation link?",
                    Description =
                        "This previously linked donation instructions will be erased. If you paid anything to it, you will lose access.",
                    Action = "Confirm and generate",
                }));
            }

            var user = await _userManager.GetUserAsync(User);

            var    httpClient = _httpClientFactory.CreateClient(EthereumLikeExtensions.EthereumInvoiceCreateHttpClient);
            string invoiceUrl;
            var    response = await httpClient.PostAsync($"{Server.HexToUTF8String()}{invoiceEndpoint.HexToUTF8String()}",
                                                         new FormUrlEncodedContent(new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("choiceKey", $"license_{chainId}"),
                new KeyValuePair <string, string>("posData",
                                                  JsonConvert.SerializeObject(new { Host = Request.Host, ChainId = chainId })),
                new KeyValuePair <string, string>("orderID", $"eth_{Request.Host}_{chainId}"),
                new KeyValuePair <string, string>("email", user.Email),
                new KeyValuePair <string, string>("redirectUrl",
                                                  Url.Action("Callback", "EthereumConfig", new { chainId }, Request.Scheme)),
            }));

            if (response.StatusCode == System.Net.HttpStatusCode.Found)
            {
                HttpResponseHeaders headers = response.Headers;
                if (headers != null && headers.Location != null)
                {
                    invoiceUrl        = $"{Server.HexToUTF8String()}{headers.Location}";
                    current.InvoiceId = headers.Location.ToString()
                                        .Replace("/i/", string.Empty, StringComparison.InvariantCultureIgnoreCase);
                    await UpdateChainConfig(chainId, current);

                    return(Redirect(invoiceUrl));
                }
            }

            TempData.SetStatusMessageModel(new StatusMessageModel()
            {
                Severity = StatusMessageModel.StatusSeverity.Error, Message = $"Couldn't connect to donation server, try again later."
            });
            return(RedirectToAction("UpdateChainConfig", new { chainId }));
        }
Exemple #2
0
 public async Task <IActionResult> UpdateChainConfig(int chainId)
 {
     return(View("Ethereum/UpdateChainConfig",
                 (await _settingsRepository.GetSettingAsync <EthereumLikeConfiguration>(
                      EthereumLikeConfiguration.SettingsKey(chainId))) ?? new EthereumLikeConfiguration()
     {
         ChainId = chainId, Web3ProviderUrl = ""
     }));
 }
Exemple #3
0
        private async Task LoopThroughChainWatchers(CancellationToken cancellationToken)
        {
            var chainIds = _btcPayNetworkProvider.GetAll().OfType <EthereumBTCPayNetwork>()
                           .Select(network => network.ChainId).Distinct().ToList();

            foreach (var chainId in chainIds)
            {
                try
                {
                    var settings = await _settingsRepository.GetSettingAsync <EthereumLikeConfiguration>(
                        EthereumLikeConfiguration.SettingsKey(chainId));

                    if (settings is null || string.IsNullOrEmpty(settings.Web3ProviderUrl))
                    {
                        var val     = _configuration.GetValue <string>($"chain{chainId}_web3", null);
                        var valUser = _configuration.GetValue <string>($"chain{chainId}_web3_user", null);
                        var valPass = _configuration.GetValue <string>($"chain{chainId}_web3_password", null);
                        if (val != null && First)
                        {
                            Logs.PayServer.LogInformation($"Setting eth chain {chainId} web3 to {val}");
                            settings ??= new EthereumLikeConfiguration()
                            {
                                ChainId              = chainId,
                                Web3ProviderUrl      = val,
                                Web3ProviderPassword = valPass,
                                Web3ProviderUsername = valUser
                            };
                            await _settingsRepository.UpdateSetting(settings,
                                                                    EthereumLikeConfiguration.SettingsKey(chainId));
                        }
                    }

                    var currentlyRunning = _chainHostedServices.ContainsKey(chainId);
                    if (!currentlyRunning || (currentlyRunning))
                    {
                        await HandleChainWatcher(settings, cancellationToken);
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            First = false;
        }
Exemple #4
0
        public async Task <IActionResult> UpdateChainConfig(int chainId, EthereumLikeConfiguration vm)
        {
            var current = await _settingsRepository.GetSettingAsync <EthereumLikeConfiguration>(
                EthereumLikeConfiguration.SettingsKey(chainId));

            if (current?.Web3ProviderUrl != vm.Web3ProviderUrl)
            {
                vm.ChainId = chainId;
                await _settingsRepository.UpdateSetting(vm, EthereumLikeConfiguration.SettingsKey(chainId));
            }

            TempData.SetStatusMessageModel(new StatusMessageModel()
            {
                Severity = StatusMessageModel.StatusSeverity.Success, Message = $"Chain {chainId} updated"
            });
            return(RedirectToAction(nameof(UpdateChainConfig)));
        }