From 063e9747c471adda9c00f304a503a39f50a82c43 Mon Sep 17 00:00:00 2001 From: 257Byte <257Byte@gmail.com> Date: Fri, 23 Jul 2021 01:57:16 +0300 Subject: [PATCH] Add API endpoints for access implicit operations by id --- Tzkt.Api/Controllers/OperationsController.cs | 53 +++++++++++++++++++ .../OperationRepository.Baking.cs | 27 ++++++++++ .../OperationRepository.Migrations.cs | 47 ++++++++++++++++ ...OperationRepository.RevelationPenalties.cs | 28 ++++++++++ 4 files changed, 155 insertions(+) diff --git a/Tzkt.Api/Controllers/OperationsController.cs b/Tzkt.Api/Controllers/OperationsController.cs index 0fbbbe45c..c1b9ee730 100644 --- a/Tzkt.Api/Controllers/OperationsController.cs +++ b/Tzkt.Api/Controllers/OperationsController.cs @@ -1648,6 +1648,25 @@ public async Task>> GetMigrations( } } + /// + /// Get migration by id + /// + /// + /// Returns migration operation with specified id. + /// + /// Operation id + /// Format of the parameters, storage and diffs: `0` - JSON, `1` - JSON string, `2` - raw micheline, `3` - raw micheline string + /// Comma-separated list of ticker symbols to inject historical prices into response + /// + [HttpGet("migrations/{id:int}")] + public Task GetMigrationById( + [Required][Min(0)] int id, + MichelineFormat micheline = MichelineFormat.Json, + Symbols quote = Symbols.None) + { + return Operations.GetMigration(id, micheline, quote); + } + /// /// Get migrations count /// @@ -1738,6 +1757,23 @@ public async Task>> GetReve } } + /// + /// Get revelation penalty by id + /// + /// + /// Returns revelation penalty operation with specified id. + /// + /// Operation id + /// Comma-separated list of ticker symbols to inject historical prices into response + /// + [HttpGet("revelation_penalties/{id:int}")] + public Task GetRevelationPenaltyById( + [Required][Min(0)] int id, + Symbols quote = Symbols.None) + { + return Operations.GetRevelationPenalty(id, quote); + } + /// /// Get revelation penalties count /// @@ -1828,6 +1864,23 @@ public async Task>> GetBaking( } } + /// + /// Get baking by id + /// + /// + /// Returns baking operation with specified id. + /// + /// Operation id + /// Comma-separated list of ticker symbols to inject historical prices into response + /// + [HttpGet("baking/{id:int}")] + public Task GetBakingById( + [Required][Min(0)] int id, + Symbols quote = Symbols.None) + { + return Operations.GetBaking(id, quote); + } + /// /// Get baking count /// diff --git a/Tzkt.Api/Repositories/OperationRepository.Baking.cs b/Tzkt.Api/Repositories/OperationRepository.Baking.cs index 08568f368..327597f44 100644 --- a/Tzkt.Api/Repositories/OperationRepository.Baking.cs +++ b/Tzkt.Api/Repositories/OperationRepository.Baking.cs @@ -22,6 +22,33 @@ public async Task GetBakingsCount( return await db.QueryFirstAsync(sql.Query, sql.Params); } + public async Task GetBaking(int id, Symbols quote) + { + var sql = $@" + SELECT ""Id"", ""Level"", ""Timestamp"", ""BakerId"", ""Hash"", ""Priority"", ""Deposit"", ""Reward"", ""Fees"" + FROM ""Blocks"" + WHERE ""Id"" = @id + LIMIT 1"; + + using var db = GetConnection(); + var row = await db.QueryFirstOrDefaultAsync(sql, new { id }); + if (row == null) return null; + + return new BakingOperation + { + Id = row.Id, + Level = row.Level, + Timestamp = row.Timestamp, + Baker = Accounts.GetAlias(row.BakerId), + Block = row.Hash, + Priority = row.Priority, + Deposit = row.Deposit, + Reward = row.Reward, + Fees = row.Fees, + Quote = Quotes.Get(quote, row.Level) + }; + } + public async Task> GetBakings( AccountParameter baker, Int32Parameter level, diff --git a/Tzkt.Api/Repositories/OperationRepository.Migrations.cs b/Tzkt.Api/Repositories/OperationRepository.Migrations.cs index 1a71e02a6..744abf45d 100644 --- a/Tzkt.Api/Repositories/OperationRepository.Migrations.cs +++ b/Tzkt.Api/Repositories/OperationRepository.Migrations.cs @@ -21,6 +21,53 @@ public async Task GetMigrationsCount( return await db.QueryFirstAsync(sql.Query, sql.Params); } + public async Task 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> GetMigrations( AccountParameter account, MigrationKindParameter kind, diff --git a/Tzkt.Api/Repositories/OperationRepository.RevelationPenalties.cs b/Tzkt.Api/Repositories/OperationRepository.RevelationPenalties.cs index d307fde34..5c711a4b8 100644 --- a/Tzkt.Api/Repositories/OperationRepository.RevelationPenalties.cs +++ b/Tzkt.Api/Repositories/OperationRepository.RevelationPenalties.cs @@ -21,6 +21,34 @@ public async Task GetRevelationPenaltiesCount( return await db.QueryFirstAsync(sql.Query, sql.Params); } + public async Task GetRevelationPenalty(int id, Symbols quote) + { + var sql = $@" + SELECT o.*, b.""Hash"" + FROM ""RevelationPenaltyOps"" as o + INNER JOIN ""Blocks"" as b + ON b.""Level"" = o.""Level"" + WHERE o.""Id"" = @id + LIMIT 1"; + + using var db = GetConnection(); + var row = await db.QueryFirstOrDefaultAsync(sql, new { id }); + if (row == null) return null; + + return new RevelationPenaltyOperation + { + Id = row.Id, + Level = row.Level, + Block = row.Hash, + Timestamp = row.Timestamp, + Baker = Accounts.GetAlias(row.BakerId), + MissedLevel = row.MissedLevel, + LostReward = row.LostReward, + LostFees = row.LostFees, + Quote = Quotes.Get(quote, row.Level) + }; + } + public async Task> GetRevelationPenalties( AccountParameter baker, Int32Parameter level,