Exemplo n.º 1
0
        public async Task Write(StreamWriter csv, string address, DateTime from, DateTime to, int limit, string delimiter, string separator)
        {
            var account = await Accounts.GetAsync(address);

            if (account == null)
            {
                return;
            }

            var sql = new StringBuilder();

            if (account.DelegationsCount > 0)
            {
                UnionDelegations(sql);
            }
            if (account.OriginationsCount > 0)
            {
                UnionOriginations(sql);
            }
            if (account.TransactionsCount > 0)
            {
                UnionTransactions(sql);
            }
            if (account.RevealsCount > 0)
            {
                UnionReveals(sql);
            }
            if (account.MigrationsCount > 0)
            {
                UnionMigrations(sql);
            }

            if (account is RawUser user)
            {
                if (user.Activated == true)
                {
                    UnionActivations(sql);
                }
                if (user.RegisterConstantsCount > 0)
                {
                    UnionRegisterConstant(sql);
                }
            }

            if (account is RawDelegate delegat)
            {
                if (delegat.BlocksCount > 0)
                {
                    UnionBaking(sql);
                }
                if (delegat.EndorsementsCount > 0)
                {
                    UnionEndorsements(sql);
                }
                if (delegat.DoubleBakingCount > 0)
                {
                    UnionDoubleBaking(sql);
                }
                if (delegat.DoubleEndorsingCount > 0)
                {
                    UnionDoubleEndorsing(sql);
                }
                if (delegat.NonceRevelationsCount > 0)
                {
                    UnionNonceRevelations(sql);
                }
                if (delegat.RevelationPenaltiesCount > 0)
                {
                    UnionRevelationPenalties(sql);
                }
            }

            if (sql.Length == 0)
            {
                return;
            }

            sql.AppendLine(@"ORDER BY ""Id""");
            sql.AppendLine(@"LIMIT @limit");

            using var db = GetConnection();
            var rows = await db.QueryAsync(sql.ToString(), new { account = account.Id, from, to, limit });

            #region write header
            csv.Write("Block level");
            csv.Write(delimiter);
            csv.Write("Datetime");
            csv.Write(delimiter);
            csv.Write("Operation");
            csv.Write(delimiter);
            if (account is RawDelegate)
            {
                csv.Write("Reward");
                csv.Write(delimiter);
                csv.Write("Loss");
                csv.Write(delimiter);
            }
            csv.Write("Received");
            csv.Write(delimiter);
            csv.Write("From address");
            csv.Write(delimiter);
            csv.Write("Sent");
            csv.Write(delimiter);
            csv.Write("Fee");
            csv.Write(delimiter);
            csv.Write("To address");
            csv.Write(delimiter);
            csv.Write("Explorer link");
            csv.Write("\n");
            #endregion

            #region write rows
            var format = new NumberFormatInfo {
                NumberDecimalSeparator = separator
            };

            foreach (var row in rows)
            {
                csv.Write(row.Level);
                csv.Write(delimiter);
                csv.Write(row.Timestamp.ToString("yyyy-MM-dd HH:mm:ss"));
                csv.Write(delimiter);
                csv.Write(Operations[row.Type]);
                csv.Write(delimiter);
                if (account is RawDelegate)
                {
                    csv.Write(row.Reward == null ? "" : ((decimal)row.Reward / 1_000_000m).ToString(format));
                    csv.Write(delimiter);
                    csv.Write(row.Loss == null ? "" : ((decimal) - row.Loss / 1_000_000m).ToString(format));
                    csv.Write(delimiter);
                }
                csv.Write(row.Received == null ? "" : ((decimal)row.Received / 1_000_000m).ToString(format));
                csv.Write(delimiter);
                // WARN: possible NullReferenceException if chain reorgs during request execution (very unlikely)
                csv.Write(row.From == null ? "" : Accounts.Get(row.From).Address);
                csv.Write(delimiter);
                csv.Write(row.Sent == null ? "" : ((decimal) - row.Sent / 1_000_000m).ToString(format));
                csv.Write(delimiter);
                csv.Write(row.Fee == null ? "" : ((decimal) - row.Fee / 1_000_000m).ToString(format));
                csv.Write(delimiter);
                // WARN: possible NullReferenceException if chain reorgs during request execution (very unlikely)
                csv.Write(row.To == null ? "" : Accounts.Get(row.To).Address);
                csv.Write(delimiter);
                csv.Write(row.Nonce != null
                    ? $"https://tzkt.io/{row.OpHash}/{row.Counter}/{row.Nonce}"
                    : row.Counter != null
                        ? $"https://tzkt.io/{row.OpHash}/{row.Counter}"
                        : row.OpHash != null
                            ? $"https://tzkt.io/{row.OpHash}"
                            : "");

                csv.Write("\n");
            }
            #endregion

            csv.Flush();
        }
Exemplo n.º 2
0
        public async Task Write(StreamWriter csv, string address, DateTime from, DateTime to, int limit)
        {
            var account = await Accounts.GetAsync(address);

            if (account == null)
            {
                return;
            }

            var sql = new StringBuilder();

            if (account.DelegationsCount > 0)
            {
                UnionDelegations(sql);
            }
            if (account.OriginationsCount > 0)
            {
                UnionOriginations(sql);
            }
            if (account.TransactionsCount > 0)
            {
                UnionTransactions(sql);
            }
            if (account.RevealsCount > 0)
            {
                UnionReveals(sql);
            }
            if (account.MigrationsCount > 0)
            {
                UnionMigrations(sql);
            }

            if (account is RawUser user)
            {
                if (user.Activated == true)
                {
                    UnionActivations(sql);
                }
            }

            if (account is RawDelegate delegat)
            {
                if (delegat.BlocksCount > 0)
                {
                    UnionBaking(sql);
                }
                if (delegat.EndorsementsCount > 0)
                {
                    UnionEndorsements(sql);
                }
                if (delegat.DoubleBakingCount > 0)
                {
                    UnionDoubleBaking(sql);
                }
                if (delegat.DoubleEndorsingCount > 0)
                {
                    UnionDoubleEndorsing(sql);
                }
                if (delegat.NonceRevelationsCount > 0)
                {
                    UnionNonceRevelations(sql);
                }
                if (delegat.RevelationPenaltiesCount > 0)
                {
                    UnionRevelationPenalties(sql);
                }
            }

            if (sql.Length == 0)
            {
                return;
            }

            sql.AppendLine(@"ORDER BY ""Id""");
            sql.AppendLine(@"LIMIT @limit");

            using var db = GetConnection();
            var rows = await db.QueryAsync(sql.ToString(), new { account = account.Id, from, to, limit });

            #region write header
            csv.Write("Block level;");
            csv.Write("Datetime;");
            csv.Write("Operation;");
            if (account is RawDelegate)
            {
                csv.Write("Reward;");
                csv.Write("Loss;");
            }
            csv.Write("Received;");
            csv.Write("From address;");
            csv.Write("Sent;");
            csv.Write("Fee;");
            csv.Write("To address;");
            csv.Write("Explorer link;\n");
            #endregion

            #region write rows
            foreach (var row in rows)
            {
                csv.Write(row.Level);
                csv.Write(";");
                csv.Write(row.Timestamp);
                csv.Write(";");
                csv.Write(Operations[row.Type]);
                csv.Write(";");
                if (account is RawDelegate)
                {
                    csv.Write(row.Reward == null ? "" : row.Reward / 1_000_000m);
                    csv.Write(";");
                    csv.Write(row.Loss == null ? "" : -row.Loss / 1_000_000m);
                    csv.Write(";");
                }
                csv.Write(row.Received == null ? "" : row.Received / 1_000_000m);
                csv.Write(";");
                csv.Write(row.From == null ? "" : Accounts.Get(row.From).Address);
                csv.Write(";");
                csv.Write(row.Sent == null ? "" : -row.Sent / 1_000_000m);
                csv.Write(";");
                csv.Write(row.Fee == null ? "" : -row.Fee / 1_000_000m);
                csv.Write(";");
                csv.Write(row.To == null ? "" : Accounts.Get(row.To).Address);
                csv.Write(";");
                csv.Write(row.Nonce != null
                    ? $"https://tzkt.io/{row.OpHash}/{row.Counter}/{row.Nonce}"
                    : row.Counter != null
                        ? $"https://tzkt.io/{row.OpHash}/{row.Counter}"
                        : row.OpHash != null
                            ? $"https://tzkt.io/{row.OpHash}"
                            : "");

                csv.Write("\n");
            }
            #endregion

            csv.Flush();
        }