Exemplo n.º 1
0
 public string Balance(string id)
 {
     using (var client = CreateApi())
     {
         var balance = client.BalanceGet(Base58Encoding.Decode(id), 1);
         return(ConvUtils.FormatAmount(balance.Amount));
     }
 }
Exemplo n.º 2
0
 public string Balance(string id)
 {
     using (var client = CreateApi())
     {
         var balance = client.BalanceGet(id, "cs");
         return(ConvUtils.FormatAmount(balance.Amount));
     }
 }
Exemplo n.º 3
0
 public TokenAmounts AccountTokens(string id, string tokens)
 {
     using (var client = CreateApi())
     {
         var result = new TokenAmounts();
         if (id == null || tokens == null)
         {
             return(result);
         }
         foreach (var token in tokens.Split(","))
         {
             var balance = client.BalanceGet(Base58Encoding.Decode(id), sbyte.Parse(token));
             result.Tokens.Add(new TokenAmount {
                 Token = token, Value = ConvUtils.FormatAmount(balance.Amount)
             });
         }
         return(result);
     }
 }
Exemplo n.º 4
0
 public TokenAmounts AccountTokens(string id, string tokens)
 {
     using (var client = CreateApi())
     {
         var result = new TokenAmounts();
         if (id == null || tokens == null)
         {
             return(result);
         }
         foreach (var token in tokens.Split(","))
         {
             var balance = client.BalanceGet(ConvUtils.ConvertHashBackPartial(id), token);
             result.Tokens.Add(new TokenAmount {
                 Token = token, Value = ConvUtils.FormatAmount(balance.Amount)
             });
         }
         return(result);
     }
 }
Exemplo n.º 5
0
        // Updates the list of nodes, from signal server
        private async Task UpdateNetworkNodes(Network network)
        {
            // Create a connection to the signal server API
            using (var client = ApiFab.CreateSignalApi(network.SignalIp, network.SignalPort))
            {
                // Get the list of nodes from API
                var result = client.GetActiveNodes();

                // Convert nodes to nodeInfos
                var nodes = result.Nodes.Select(n => new NodeInfo(n)).ToList();

                // Try to get country and geo-location for all nodes
                try
                {
                    // Connect to DB
                    using (var db = CsmonDbContext.Create())
                    {
                        // Get nodes, stored in db
                        var dbNodes = db.Nodes.Where(n => n.Network == network.Id).ToList();

                        // Add new nodes into db and update existing
                        foreach (var node in nodes)
                        {
                            var dbNode = dbNodes.FirstOrDefault(n => n.PublicKey.Equals(node.PublicKey));
                            if (dbNode == null)
                            {
                                db.Nodes.Add(new Node(node, network.Id));
                            }
                            else if (!node.EqualsDbNode(dbNode))
                            {
                                db.Nodes.Update(node.UpdateDbNode(dbNode));
                            }
                        }
                        db.SaveChanges();

                        // Get Non-Active nodes from db
                        foreach (var node in dbNodes.Where(n => !nodes.Any(d => d.PublicKey.Equals(n.PublicKey))))
                        {
                            nodes.Add(new NodeInfo(node));
                        }

                        // Find geo data for nodes
                        foreach (var ip in nodes.Select(n => n.Ip).Distinct())
                        {
                            // Try to find node in db by ip address
                            var location = db.Locations.FirstOrDefault(l => l.Ip.Equals(ip));

                            // If not found, try to get info by ip using ipapi.co web service
                            if (location == null)
                            {
                                try
                                {
                                    var uri     = new Uri("https://ipapi.co/" + $"{ip}/json/");
                                    var nodeStr = await GetAsync(uri);

                                    nodeStr     = nodeStr.Replace("\"latitude\": null,", "");
                                    nodeStr     = nodeStr.Replace("\"longitude\": null,", "");
                                    location    = JsonConvert.DeserializeObject <Location>(nodeStr);
                                    location.Ip = ip;
                                    if (location.Org.Length > 64)
                                    {
                                        location.Org = location.Org.Substring(0, 64);
                                    }
                                    // Store data in db
                                    db.Locations.Add(location);
                                    db.SaveChanges();
                                }
                                catch (Exception e)
                                {
                                    _logger.LogError(e, "");
                                    continue;
                                }
                            }

                            // Set location data to nodes
                            foreach (var nodeInfo in nodes.Where(n => n.Ip.Equals(ip)))
                            {
                                nodeInfo.SetLocation(location);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "");
                }

                // Collect additional info about nodes from the network
                try
                {
                    using (var cnt = ApiFab.CreateReleaseApi(network.Ip))
                    {
                        var page = 0;
                        while (true)
                        {
                            var writers = cnt.WritersGet(page++);
                            if (!writers.Writers.Any())
                            {
                                break;
                            }
                            foreach (var writer in writers.Writers)
                            {
                                var key  = Base58Encoding.Encode(writer.Address);
                                var node = nodes.FirstOrDefault(n => n.PublicKey == key);
                                if (node == null)
                                {
                                    continue;
                                }
                                node.TotalFee    = ConvUtils.FormatAmount(writer.FeeCollected);
                                node.TimesWriter = writer.TimesWriter;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "");
                }

                // Hide Ip addresses before output
                foreach (var nodeInfo in nodes)
                {
                    nodeInfo.HideIp();
                }

                // Put the ordered list into the storage
                _states[network.Id].Nodes = nodes.OrderByDescending(n => n.Active).ThenBy(n => n.Ip).ToList();
            }
        }