Skip to content

Commit

Permalink
Add API endpoints for access implicit operations by id
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Aug 2, 2021
1 parent 2b861a1 commit 063e974
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Tzkt.Api/Controllers/OperationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,25 @@ public async Task<ActionResult<IEnumerable<MigrationOperation>>> GetMigrations(
}
}

/// <summary>
/// Get migration by id
/// </summary>
/// <remarks>
/// Returns migration operation with specified id.
/// </remarks>
/// <param name="id">Operation id</param>
/// <param name="micheline">Format of the parameters, storage and diffs: `0` - JSON, `1` - JSON string, `2` - raw micheline, `3` - raw micheline string</param>
/// <param name="quote">Comma-separated list of ticker symbols to inject historical prices into response</param>
/// <returns></returns>
[HttpGet("migrations/{id:int}")]
public Task<MigrationOperation> GetMigrationById(
[Required][Min(0)] int id,
MichelineFormat micheline = MichelineFormat.Json,
Symbols quote = Symbols.None)
{
return Operations.GetMigration(id, micheline, quote);
}

/// <summary>
/// Get migrations count
/// </summary>
Expand Down Expand Up @@ -1738,6 +1757,23 @@ public async Task<ActionResult<IEnumerable<RevelationPenaltyOperation>>> GetReve
}
}

/// <summary>
/// Get revelation penalty by id
/// </summary>
/// <remarks>
/// Returns revelation penalty operation with specified id.
/// </remarks>
/// <param name="id">Operation id</param>
/// <param name="quote">Comma-separated list of ticker symbols to inject historical prices into response</param>
/// <returns></returns>
[HttpGet("revelation_penalties/{id:int}")]
public Task<RevelationPenaltyOperation> GetRevelationPenaltyById(
[Required][Min(0)] int id,
Symbols quote = Symbols.None)
{
return Operations.GetRevelationPenalty(id, quote);
}

/// <summary>
/// Get revelation penalties count
/// </summary>
Expand Down Expand Up @@ -1828,6 +1864,23 @@ public async Task<ActionResult<IEnumerable<BakingOperation>>> GetBaking(
}
}

/// <summary>
/// Get baking by id
/// </summary>
/// <remarks>
/// Returns baking operation with specified id.
/// </remarks>
/// <param name="id">Operation id</param>
/// <param name="quote">Comma-separated list of ticker symbols to inject historical prices into response</param>
/// <returns></returns>
[HttpGet("baking/{id:int}")]
public Task<BakingOperation> GetBakingById(
[Required][Min(0)] int id,
Symbols quote = Symbols.None)
{
return Operations.GetBaking(id, quote);
}

/// <summary>
/// Get baking count
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions Tzkt.Api/Repositories/OperationRepository.Baking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ public async Task<int> GetBakingsCount(
return await db.QueryFirstAsync<int>(sql.Query, sql.Params);
}

public async Task<BakingOperation> 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<IEnumerable<BakingOperation>> GetBakings(
AccountParameter baker,
Int32Parameter level,
Expand Down
47 changes: 47 additions & 0 deletions Tzkt.Api/Repositories/OperationRepository.Migrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,53 @@ public async Task<int> GetMigrationsCount(
return await db.QueryFirstAsync<int>(sql.Query, sql.Params);
}

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<MigrationOperation>> GetMigrations(
AccountParameter account,
MigrationKindParameter kind,
Expand Down
28 changes: 28 additions & 0 deletions Tzkt.Api/Repositories/OperationRepository.RevelationPenalties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ public async Task<int> GetRevelationPenaltiesCount(
return await db.QueryFirstAsync<int>(sql.Query, sql.Params);
}

public async Task<RevelationPenaltyOperation> 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<IEnumerable<RevelationPenaltyOperation>> GetRevelationPenalties(
AccountParameter baker,
Int32Parameter level,
Expand Down

0 comments on commit 063e974

Please sign in to comment.