Example #1
0
        public async Task <MigrationOperation> GetMigration(int id, MichelineFormat format, Symbols quote)
        {
            var sql = $@"
                SELECT      o.*, b.""Hash""
                FROM        ""MigrationOps"" as o
                INNER JOIN  ""Blocks"" as b 
                        ON  b.""Level"" = o.""Level""
                WHERE       o.""Id"" = @id
                LIMIT       1";

            using var db = GetConnection();
            var rows = await db.QueryAsync(sql, new { id });

            // TODO: optimize for QueryFirstOrDefaultAsync

            #region include storage
            var storages = await AccountRepository.GetStorages(db,
                                                               rows.Where(x => x.StorageId != null)
                                                               .Select(x => (int)x.StorageId)
                                                               .Distinct()
                                                               .ToList(),
                                                               format);

            #endregion

            #region include diffs
            var diffs = await BigMapsRepository.GetMigrationDiffs(db,
                                                                  rows.Where(x => x.BigMapUpdates != null)
                                                                  .Select(x => (int)x.Id)
                                                                  .ToList(),
                                                                  format);

            #endregion

            return(rows.Select(row => new MigrationOperation
            {
                Id = row.Id,
                Level = row.Level,
                Block = row.Hash,
                Timestamp = row.Timestamp,
                Account = Accounts.GetAlias(row.AccountId),
                Kind = MigrationKinds.ToString(row.Kind),
                BalanceChange = row.BalanceChange,
                Storage = row.StorageId == null ? null : storages?[row.StorageId],
                Diffs = row.BigMapUpdates == null ? null : diffs?[row.Id],
                Quote = Quotes.Get(quote, row.Level)
            }).FirstOrDefault());
        }
        public async Task <IEnumerable <OriginationOperation> > GetOriginations(string hash, MichelineFormat format, Symbols quote)
        {
            var sql = $@"
                SELECT      o.*, b.""Hash"", sc.""ParameterSchema"", sc.""StorageSchema"", sc.""CodeSchema"", sc.""Views""
                FROM        ""OriginationOps"" as o
                INNER JOIN  ""Blocks"" as b 
                        ON  b.""Level"" = o.""Level""
                LEFT  JOIN  ""Scripts"" as sc
                        ON  sc.""Id"" = o.""ScriptId""
                WHERE       o.""OpHash"" = @hash::character(51)
                ORDER BY    o.""Id""";

            using var db = GetConnection();
            var rows = await db.QueryAsync(sql, new { hash });

            #region include storage
            var storages = await AccountRepository.GetStorages(db,
                                                               rows.Where(x => x.StorageId != null)
                                                               .Select(x => (int)x.StorageId)
                                                               .Distinct()
                                                               .ToList(),
                                                               format);

            #endregion

            #region include diffs
            var diffs = await BigMapsRepository.GetOriginationDiffs(db,
                                                                    rows.Where(x => x.BigMapUpdates != null)
                                                                    .Select(x => (int)x.Id)
                                                                    .ToList(),
                                                                    format);

            #endregion

            return(rows.Select(row =>
            {
                var contract = row.ContractId == null ? null
                    : (RawContract)Accounts.Get((int)row.ContractId);

                MichelineArray code = null;
                if (row.ParameterSchema != null)
                {
                    code = new();
                    code.Add(Micheline.FromBytes(row.ParameterSchema));
                    code.Add(Micheline.FromBytes(row.StorageSchema));
                    if (row.Views != null)
                    {
                        code.AddRange(((byte[][])row.Views).Select(x => Micheline.FromBytes(x)));
                    }
                    code.Add(Micheline.FromBytes(row.CodeSchema));
                }

                return new OriginationOperation
                {
                    Id = row.Id,
                    Level = row.Level,
                    Block = row.Hash,
                    Timestamp = row.Timestamp,
                    Hash = hash,
                    Initiator = row.InitiatorId != null ? Accounts.GetAlias(row.InitiatorId) : null,
                    Sender = Accounts.GetAlias(row.SenderId),
                    Counter = row.Counter,
                    Nonce = row.Nonce,
                    GasLimit = row.GasLimit,
                    GasUsed = row.GasUsed,
                    StorageLimit = row.StorageLimit,
                    StorageUsed = row.StorageUsed,
                    BakerFee = row.BakerFee,
                    StorageFee = row.StorageFee ?? 0,
                    AllocationFee = row.AllocationFee ?? 0,
                    ContractDelegate = row.DelegateId != null ? Accounts.GetAlias(row.DelegateId) : null,
                    ContractBalance = row.Balance,
                    Code = (int)format % 2 == 0 ? code : code.ToJson(),
                    Storage = row.StorageId == null ? null : storages?[row.StorageId],
                    Diffs = diffs?.GetValueOrDefault((int)row.Id),
                    Status = OpStatuses.ToString(row.Status),
                    OriginatedContract = contract == null ? null :
                                         new OriginatedContract
                    {
                        Alias = contract.Alias,
                        Address = contract.Address,
                        Kind = contract.KindString,
                        TypeHash = contract.TypeHash,
                        CodeHash = contract.CodeHash
                    },
                    ContractManager = row.ManagerId != null ? Accounts.GetAlias(row.ManagerId) : null,
                    Errors = row.Errors != null ? OperationErrorSerializer.Deserialize(row.Errors) : null,
                    Quote = Quotes.Get(quote, row.Level)
                };
            }));
        }
Example #3
0
 public BigMapsController(BigMapsRepository bigMaps)
 {
     BigMaps = bigMaps;
 }
Example #4
0
        public async Task <IEnumerable <MigrationOperation> > GetMigrations(
            AccountParameter account,
            MigrationKindParameter kind,
            Int64Parameter balanceChange,
            Int32Parameter level,
            DateTimeParameter timestamp,
            SortParameter sort,
            OffsetParameter offset,
            int limit,
            MichelineFormat format,
            Symbols quote,
            bool includeStorage = false,
            bool includeDiffs   = false)
        {
            var sql = new SqlBuilder(@"SELECT o.*, b.""Hash"" FROM ""MigrationOps"" AS o INNER JOIN ""Blocks"" as b ON b.""Level"" = o.""Level""")
                      .Filter("AccountId", account)
                      .Filter("Kind", kind)
                      .Filter("BalanceChange", balanceChange)
                      .FilterA(@"o.""Level""", level)
                      .FilterA(@"o.""Timestamp""", timestamp)
                      .Take(sort, offset, limit, x => x == "level" ? ("Id", "Level") : ("Id", "Id"), "o");

            using var db = GetConnection();
            var rows = await db.QueryAsync(sql.Query, sql.Params);

            #region include storage
            var storages = includeStorage
                ? await AccountRepository.GetStorages(db,
                                                      rows.Where(x => x.StorageId != null)
                                                      .Select(x => (int)x.StorageId)
                                                      .Distinct()
                                                      .ToList(),
                                                      format)
                : null;

            #endregion

            #region include diffs
            var diffs = includeDiffs
                ? await BigMapsRepository.GetMigrationDiffs(db,
                                                            rows.Where(x => x.BigMapUpdates != null)
                                                            .Select(x => (int)x.Id)
                                                            .ToList(),
                                                            format)
                : null;

            #endregion

            return(rows.Select(row => new MigrationOperation
            {
                Id = row.Id,
                Level = row.Level,
                Block = row.Hash,
                Timestamp = row.Timestamp,
                Account = Accounts.GetAlias(row.AccountId),
                Kind = MigrationKinds.ToString(row.Kind),
                BalanceChange = row.BalanceChange,
                Storage = row.StorageId == null ? null : storages?[row.StorageId],
                Diffs = row.BigMapUpdates == null ? null : diffs?[row.Id],
                Quote = Quotes.Get(quote, row.Level)
            }));
        }
Example #5
0
        public async Task <object[]> GetMigrations(
            AccountParameter account,
            MigrationKindParameter kind,
            Int64Parameter balanceChange,
            Int32Parameter level,
            DateTimeParameter timestamp,
            SortParameter sort,
            OffsetParameter offset,
            int limit,
            string field,
            MichelineFormat format,
            Symbols quote)
        {
            var columns = new HashSet <string>(1);
            var joins   = new HashSet <string>(1);

            switch (field)
            {
            case "id": columns.Add(@"o.""Id"""); break;

            case "level": columns.Add(@"o.""Level"""); break;

            case "timestamp": columns.Add(@"o.""Timestamp"""); break;

            case "account": columns.Add(@"o.""AccountId"""); break;

            case "kind": columns.Add(@"o.""Kind"""); break;

            case "balanceChange": columns.Add(@"o.""BalanceChange"""); break;

            case "block":
                columns.Add(@"b.""Hash""");
                joins.Add(@"INNER JOIN ""Blocks"" as b ON b.""Level"" = o.""Level""");
                break;

            case "storage": columns.Add(@"o.""StorageId"""); break;

            case "diffs":
                columns.Add(@"o.""Id""");
                columns.Add(@"o.""BigMapUpdates""");
                break;

            case "quote": columns.Add(@"o.""Level"""); break;
            }

            if (columns.Count == 0)
            {
                return(Array.Empty <object>());
            }

            var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""MigrationOps"" as o {string.Join(' ', joins)}")
                      .Filter("AccountId", account)
                      .Filter("Kind", kind)
                      .Filter("BalanceChange", balanceChange)
                      .FilterA(@"o.""Level""", level)
                      .FilterA(@"o.""Timestamp""", timestamp)
                      .Take(sort, offset, limit, x => x == "level" ? ("Id", "Level") : ("Id", "Id"), "o");

            using var db = GetConnection();
            var rows = await db.QueryAsync(sql.Query, sql.Params);

            //TODO: optimize memory allocation
            var result = new object[rows.Count()];
            var j      = 0;

            switch (field)
            {
            case "id":
                foreach (var row in rows)
                {
                    result[j++] = row.Id;
                }
                break;

            case "level":
                foreach (var row in rows)
                {
                    result[j++] = row.Level;
                }
                break;

            case "block":
                foreach (var row in rows)
                {
                    result[j++] = row.Hash;
                }
                break;

            case "timestamp":
                foreach (var row in rows)
                {
                    result[j++] = row.Timestamp;
                }
                break;

            case "account":
                foreach (var row in rows)
                {
                    result[j++] = await Accounts.GetAliasAsync(row.AccountId);
                }
                break;

            case "kind":
                foreach (var row in rows)
                {
                    result[j++] = MigrationKinds.ToString(row.Kind);
                }
                break;

            case "balanceChange":
                foreach (var row in rows)
                {
                    result[j++] = row.BalanceChange;
                }
                break;

            case "storage":
                var storages = await AccountRepository.GetStorages(db,
                                                                   rows.Where(x => x.StorageId != null)
                                                                   .Select(x => (int)x.StorageId)
                                                                   .Distinct()
                                                                   .ToList(),
                                                                   format);

                if (storages != null)
                {
                    foreach (var row in rows)
                    {
                        result[j++] = row.StorageId == null ? null : storages[row.StorageId];
                    }
                }
                break;

            case "diffs":
                var diffs = await BigMapsRepository.GetMigrationDiffs(db,
                                                                      rows.Where(x => x.BigMapUpdates != null)
                                                                      .Select(x => (int)x.Id)
                                                                      .ToList(),
                                                                      format);

                if (diffs != null)
                {
                    foreach (var row in rows)
                    {
                        result[j++] = row.BigMapUpdates == null ? null : diffs[row.Id];
                    }
                }
                break;

            case "quote":
                foreach (var row in rows)
                {
                    result[j++] = Quotes.Get(quote, row.Level);
                }
                break;
            }

            return(result);
        }