コード例 #1
0
        public async Task <int> GetCount(bool?activated, Int64Parameter balance)
        {
            var sql = new SqlBuilder(@"SELECT COUNT(*) FROM ""Commitments""")
                      .Filter("Level", activated == null ? null : new Int32NullParameter {
                Null = !activated
            })
                      .Filter("Balance", balance);

            using var db = GetConnection();
            return(await db.QueryFirstAsync <int>(sql.Query, sql.Params));
        }
コード例 #2
0
        public async Task <ActionResult <IEnumerable <Commitment> > > Get(
            bool?activated,
            Int32NullParameter activationLevel,
            Int64Parameter balance,
            SelectParameter select,
            SortParameter sort,
            OffsetParameter offset,
            [Range(0, 10000)] int limit = 100)
        {
            #region validate
            if (sort != null && !sort.Validate("activationLevel", "balance"))
            {
                return(new BadRequest($"{nameof(sort)}", "Sorting by the specified field is not allowed."));
            }
            #endregion

            if (select == null)
            {
                return(Ok(await Commitments.Get(activated, activationLevel, balance, sort, offset, limit)));
            }

            if (select.Values != null)
            {
                if (select.Values.Length == 1)
                {
                    return(Ok(await Commitments.Get(activated, activationLevel, balance, sort, offset, limit, select.Values[0])));
                }
                else
                {
                    return(Ok(await Commitments.Get(activated, activationLevel, balance, sort, offset, limit, select.Values)));
                }
            }
            else
            {
                if (select.Fields.Length == 1)
                {
                    return(Ok(await Commitments.Get(activated, activationLevel, balance, sort, offset, limit, select.Fields[0])));
                }
                else
                {
                    return(Ok(new SelectionResponse
                    {
                        Cols = select.Fields,
                        Rows = await Commitments.Get(activated, activationLevel, balance, sort, offset, limit, select.Fields)
                    }));
                }
            }
        }
コード例 #3
0
        public async Task <ActionResult <IEnumerable <Delegator> > > GetDelegators(
            [Address] string address,
            AccountTypeParameter type,
            Int64Parameter balance,
            Int32Parameter delegationLevel,
            SortParameter sort,
            OffsetParameter offset,
            [Range(0, 10000)] int limit = 100)
        {
            #region validate
            if (sort != null && !sort.Validate("balance", "delegationLevel"))
            {
                return(new BadRequest($"{nameof(sort)}", "Sorting by the specified field is not allowed."));
            }
            #endregion

            return(Ok(await Accounts.GetDelegators(address, type, balance, delegationLevel, sort, offset, limit)));
        }
コード例 #4
0
 public async Task <IEnumerable <Commitment> > Get(
     bool?activated,
     Int32NullParameter activationLevel,
     Int64Parameter balance,
     SortParameter sort,
     OffsetParameter offset,
     int limit)
 {
     var sql = new SqlBuilder(@"SELECT * FROM ""Commitments""")
               .Filter("Level", activated == null ? null : new Int32NullParameter {
         Null = !activated
     })
               .Filter("Level", activationLevel)
               .Filter("Balance", balance)
               .Take(sort, offset, limit, x => x switch
     {
         "balance" => ("Balance", "Balance"),
         "activationLevel" => ("Level", "Level"),
         _ => ("Id", "Id")
     });
コード例 #5
0
ファイル: AccountsController.cs プロジェクト: baking-bad/tzkt
        public Task <int> GetCount(
            AccountTypeParameter type,
            ContractKindParameter kind,
            Int64Parameter balance,
            BoolParameter staked)
        {
            #region optimize
            if (type == null && kind == null && balance == null && staked == null)
            {
                return(Task.FromResult(State.Current.AccountsCount));
            }

            if (kind?.Eq != null && type == null)
            {
                type = new AccountTypeParameter {
                    Eq = 2
                }
            }
            ;
            #endregion

            return(Accounts.GetCount(type, kind, balance, staked));
        }
コード例 #6
0
        public Task <int> GetCount(bool?activated, Int64Parameter balance)
        {
            #region optimize
            if (balance == null)
            {
                var state = State.Current;

                if (activated == null)
                {
                    return(Task.FromResult(state.CommitmentsCount));
                }
                else if (activated == true)
                {
                    return(Task.FromResult(state.ActivationOpsCount));
                }
                else
                {
                    return(Task.FromResult(state.CommitmentsCount - state.ActivationOpsCount));
                }
            }
            #endregion

            return(Commitments.GetCount(activated, balance));
        }
コード例 #7
0
ファイル: AccountsController.cs プロジェクト: baking-bad/tzkt
        public async Task <ActionResult <IEnumerable <Account> > > Get(
            AccountTypeParameter type,
            ContractKindParameter kind,
            AccountParameter @delegate,
            Int64Parameter balance,
            BoolParameter staked,
            Int32Parameter lastActivity,
            SelectParameter select,
            SortParameter sort,
            OffsetParameter offset,
            [Range(0, 10000)] int limit = 100)
        {
            #region validate
            if (@delegate != null)
            {
                if (@delegate.Eqx != null)
                {
                    return(new BadRequest($"{nameof(@delegate)}.eqx", "This parameter doesn't support .eqx mode."));
                }

                if (@delegate.Nex != null)
                {
                    return(new BadRequest($"{nameof(@delegate)}.nex", "This parameter doesn't support .nex mode."));
                }

                if (@delegate.Eq == -1 || @delegate.In?.Count == 0)
                {
                    return(Ok(Enumerable.Empty <Account>()));
                }
            }

            if (sort != null && !sort.Validate("id", "balance", "firstActivity", "lastActivity", "numTransactions", "numContracts"))
            {
                return(new BadRequest($"{nameof(sort)}", "Sorting by the specified field is not allowed."));
            }
            #endregion

            #region optimize
            if (kind?.Eq != null && type == null)
            {
                type = new AccountTypeParameter {
                    Eq = 2
                }
            }
            ;
            #endregion

            if (select == null)
            {
                return(Ok(await Accounts.Get(type, kind, @delegate, balance, staked, lastActivity, sort, offset, limit)));
            }

            if (select.Values != null)
            {
                if (select.Values.Length == 1)
                {
                    return(Ok(await Accounts.Get(type, kind, @delegate, balance, staked, lastActivity, sort, offset, limit, select.Values[0])));
                }
                else
                {
                    return(Ok(await Accounts.Get(type, kind, @delegate, balance, staked, lastActivity, sort, offset, limit, select.Values)));
                }
            }
            else
            {
                if (select.Fields.Length == 1)
                {
                    return(Ok(await Accounts.Get(type, kind, @delegate, balance, staked, lastActivity, sort, offset, limit, select.Fields[0])));
                }
                else
                {
                    return(Ok(new SelectionResponse
                    {
                        Cols = select.Fields,
                        Rows = await Accounts.Get(type, kind, @delegate, balance, staked, lastActivity, sort, offset, limit, select.Fields)
                    }));
                }
            }
        }
コード例 #8
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)
            }));
        }
コード例 #9
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);
        }