Skip to content

Commit

Permalink
Add delegation amount field
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Sep 8, 2020
1 parent 2ade845 commit 8363fd8
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Tzkt.Api/Models/Operations/DelegationOperation.cs
Expand Up @@ -73,6 +73,10 @@ public class DelegationOperation : Operation
/// </summary>
public long BakerFee { get; set; }

/// <summary>
/// Sender's balance at the time of delegation operation (aka delegation amount).
/// </summary>
public long Amount { get; set; }

/// <summary>
/// Information about the previous delegate of the account. `null` if there is no previous delegate
Expand Down
29 changes: 23 additions & 6 deletions Tzkt.Api/Repositories/OperationRepository.cs
Expand Up @@ -2950,7 +2950,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(string hash,
{
var sql = @"
SELECT o.""Id"", o.""Level"", o.""Timestamp"", o.""SenderId"", o.""InitiatorId"", o.""Counter"", o.""BakerFee"",
o.""GasLimit"", o.""GasUsed"", o.""Status"", o.""Nonce"", o.""PrevDelegateId"", o.""DelegateId"", o.""Errors"", b.""Hash""
o.""GasLimit"", o.""GasUsed"", o.""Status"", o.""Nonce"", o.""Amount"", o.""PrevDelegateId"", o.""DelegateId"", o.""Errors"", b.""Hash""
FROM ""DelegationOps"" as o
INNER JOIN ""Blocks"" as b
ON b.""Level"" = o.""Level""
Expand All @@ -2974,6 +2974,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(string hash,
GasLimit = row.GasLimit,
GasUsed = row.GasUsed,
BakerFee = row.BakerFee,
Amount = row.Amount,
PrevDelegate = row.PrevDelegateId != null ? Accounts.GetAlias(row.PrevDelegateId) : null,
NewDelegate = row.DelegateId != null ? Accounts.GetAlias(row.DelegateId) : null,
Status = StatusToString(row.Status),
Expand All @@ -2986,7 +2987,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(string hash,
{
var sql = @"
SELECT o.""Id"", o.""Level"", o.""Timestamp"", o.""SenderId"", o.""InitiatorId"", o.""BakerFee"",
o.""GasLimit"", o.""GasUsed"", o.""Status"", o.""Nonce"", o.""PrevDelegateId"", o.""DelegateId"", o.""Errors"", b.""Hash""
o.""GasLimit"", o.""GasUsed"", o.""Status"", o.""Nonce"", o.""Amount"", o.""PrevDelegateId"", o.""DelegateId"", o.""Errors"", b.""Hash""
FROM ""DelegationOps"" as o
INNER JOIN ""Blocks"" as b
ON b.""Level"" = o.""Level""
Expand All @@ -3010,6 +3011,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(string hash,
GasLimit = row.GasLimit,
GasUsed = row.GasUsed,
BakerFee = row.BakerFee,
Amount = row.Amount,
PrevDelegate = row.PrevDelegateId != null ? Accounts.GetAlias(row.PrevDelegateId) : null,
NewDelegate = row.DelegateId != null ? Accounts.GetAlias(row.DelegateId) : null,
Status = StatusToString(row.Status),
Expand All @@ -3022,7 +3024,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(string hash,
{
var sql = @"
SELECT o.""Id"", o.""Level"", o.""Timestamp"", o.""SenderId"", o.""InitiatorId"", o.""BakerFee"",
o.""GasLimit"", o.""GasUsed"", o.""Status"", o.""PrevDelegateId"", o.""DelegateId"", o.""Errors"", b.""Hash""
o.""GasLimit"", o.""GasUsed"", o.""Status"", o.""Amount"", o.""PrevDelegateId"", o.""DelegateId"", o.""Errors"", b.""Hash""
FROM ""DelegationOps"" as o
INNER JOIN ""Blocks"" as b
ON b.""Level"" = o.""Level""
Expand All @@ -3046,6 +3048,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(string hash,
GasLimit = row.GasLimit,
GasUsed = row.GasUsed,
BakerFee = row.BakerFee,
Amount = row.Amount,
PrevDelegate = row.PrevDelegateId != null ? Accounts.GetAlias(row.PrevDelegateId) : null,
NewDelegate = row.DelegateId != null ? Accounts.GetAlias(row.DelegateId) : null,
Status = StatusToString(row.Status),
Expand All @@ -3058,7 +3061,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(Block block,
{
var sql = @"
SELECT ""Id"", ""Timestamp"", ""OpHash"", ""SenderId"", ""InitiatorId"", ""Counter"", ""BakerFee"",
""GasLimit"", ""GasUsed"", ""Status"", ""Nonce"", ""PrevDelegateId"", ""DelegateId"", ""Errors""
""GasLimit"", ""GasUsed"", ""Status"", ""Nonce"", o.""Amount"", ""PrevDelegateId"", ""DelegateId"", ""Errors""
FROM ""DelegationOps""
WHERE ""Level"" = @level
ORDER BY ""Id""";
Expand All @@ -3080,6 +3083,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(Block block,
GasLimit = row.GasLimit,
GasUsed = row.GasUsed,
BakerFee = row.BakerFee,
Amount = row.Amount,
PrevDelegate = row.PrevDelegateId != null ? Accounts.GetAlias(row.PrevDelegateId) : null,
NewDelegate = row.DelegateId != null ? Accounts.GetAlias(row.DelegateId) : null,
Status = StatusToString(row.Status),
Expand Down Expand Up @@ -3132,6 +3136,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(Block block,
GasLimit = row.GasLimit,
GasUsed = row.GasUsed,
BakerFee = row.BakerFee,
Amount = row.Amount,
PrevDelegate = row.PrevDelegateId != null ? Accounts.GetAlias(row.PrevDelegateId) : null,
NewDelegate = row.DelegateId != null ? Accounts.GetAlias(row.DelegateId) : null,
Status = StatusToString(row.Status),
Expand Down Expand Up @@ -3171,6 +3176,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(Block block,
case "gasLimit": columns.Add(@"o.""GasLimit"""); break;
case "gasUsed": columns.Add(@"o.""GasUsed"""); break;
case "bakerFee": columns.Add(@"o.""BakerFee"""); break;
case "amount": columns.Add(@"o.""Amount"""); break;
case "prevDelegate": columns.Add(@"o.""PrevDelegateId"""); break;
case "newDelegate": columns.Add(@"o.""DelegateId"""); break;
case "status": columns.Add(@"o.""Status"""); break;
Expand Down Expand Up @@ -3260,6 +3266,10 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(Block block,
foreach (var row in rows)
result[j++][i] = row.BakerFee;
break;
case "amount":
foreach (var row in rows)
result[j++][i] = row.Amount;
break;
case "prevDelegate":
foreach (var row in rows)
result[j++][i] = row.PrevDelegateId != null ? await Accounts.GetAliasAsync(row.PrevDelegateId) : null;
Expand Down Expand Up @@ -3315,6 +3325,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(Block block,
case "gasLimit": columns.Add(@"o.""GasLimit"""); break;
case "gasUsed": columns.Add(@"o.""GasUsed"""); break;
case "bakerFee": columns.Add(@"o.""BakerFee"""); break;
case "amount": columns.Add(@"o.""Amount"""); break;
case "prevDelegate": columns.Add(@"o.""PrevDelegateId"""); break;
case "newDelegate": columns.Add(@"o.""DelegateId"""); break;
case "status": columns.Add(@"o.""Status"""); break;
Expand Down Expand Up @@ -3401,6 +3412,10 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(Block block,
foreach (var row in rows)
result[j++] = row.BakerFee;
break;
case "amount":
foreach (var row in rows)
result[j++] = row.Amount;
break;
case "prevDelegate":
foreach (var row in rows)
result[j++] = row.PrevDelegateId != null ? await Accounts.GetAliasAsync(row.PrevDelegateId) : null;
Expand Down Expand Up @@ -3430,7 +3445,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(RawAccount ac
{
var sql = $@"
SELECT o.""Id"", o.""Level"", o.""Timestamp"", o.""OpHash"", o.""SenderId"", o.""InitiatorId"", o.""Counter"", o.""BakerFee"",
o.""GasLimit"", o.""GasUsed"", o.""Status"", o.""Nonce"", o.""PrevDelegateId"", o.""DelegateId"", o.""Errors"", b.""Hash""
o.""GasLimit"", o.""GasUsed"", o.""Status"", o.""Nonce"", o.""Amount"", o.""PrevDelegateId"", o.""DelegateId"", o.""Errors"", b.""Hash""
FROM ""DelegationOps"" as o
INNER JOIN ""Blocks"" as b
ON b.""Level"" = o.""Level""
Expand All @@ -3457,6 +3472,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(RawAccount ac
GasLimit = row.GasLimit,
GasUsed = row.GasUsed,
BakerFee = row.BakerFee,
Amount = row.Amount,
PrevDelegate = row.PrevDelegateId != null ? Accounts.GetAlias(row.PrevDelegateId) : null,
NewDelegate = row.DelegateId != null ? Accounts.GetAlias(row.DelegateId) : null,
Status = StatusToString(row.Status),
Expand All @@ -3469,7 +3485,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(RawAccount ac
{
var sql = $@"
SELECT o.""Id"", o.""Level"", o.""Timestamp"", o.""OpHash"", o.""SenderId"", o.""InitiatorId"", o.""Counter"", o.""BakerFee"",
o.""GasLimit"", o.""GasUsed"", o.""Status"", o.""Nonce"", o.""PrevDelegateId"", o.""DelegateId"", o.""Errors"", b.""Hash""
o.""GasLimit"", o.""GasUsed"", o.""Status"", o.""Nonce"", o.""Amount"", o.""PrevDelegateId"", o.""DelegateId"", o.""Errors"", b.""Hash""
FROM ""DelegationOps"" as o
INNER JOIN ""Blocks"" as b
ON b.""Level"" = o.""Level""
Expand Down Expand Up @@ -3498,6 +3514,7 @@ public async Task<IEnumerable<DelegationOperation>> GetDelegations(RawAccount ac
GasLimit = row.GasLimit,
GasUsed = row.GasUsed,
BakerFee = row.BakerFee,
Amount = row.Amount,
PrevDelegate = row.PrevDelegateId != null ? Accounts.GetAlias(row.PrevDelegateId) : null,
NewDelegate = row.DelegateId != null ? Accounts.GetAlias(row.DelegateId) : null,
Status = StatusToString(row.Status),
Expand Down
2 changes: 2 additions & 0 deletions Tzkt.Data/Models/Operations/DelegationOperation.cs
Expand Up @@ -11,6 +11,8 @@ public class DelegationOperation : InternalOperation
public int? PrevDelegateId { get; set; }
public int? ResetDeactivation { get; set; }

public long Amount { get; set; }

#region relations
[ForeignKey(nameof(DelegateId))]
public Delegate Delegate { get; set; }
Expand Down
Expand Up @@ -38,6 +38,7 @@ public async Task Init(Block block, RawOperation op, RawDelegationContent conten
Sender = sender,
Delegate = delegat,
PrevDelegate = sender.Delegate,
Amount = sender.Balance - content.Fee,
Status = content.Metadata.Result.Status switch
{
"applied" => OperationStatus.Applied,
Expand Down
Expand Up @@ -38,6 +38,7 @@ public async Task Init(Block block, RawOperation op, RawDelegationContent conten
Sender = sender,
Delegate = delegat,
PrevDelegate = sender.Delegate,
Amount = sender.Balance - content.Fee,
Status = content.Metadata.Result.Status switch
{
"applied" => OperationStatus.Applied,
Expand Down
Expand Up @@ -38,6 +38,7 @@ public async Task Init(Block block, RawOperation op, RawDelegationContent conten
Sender = sender,
Delegate = delegat,
PrevDelegate = sender.Delegate,
Amount = sender.Balance - content.Fee,
Status = content.Metadata.Result.Status switch
{
"applied" => OperationStatus.Applied,
Expand Down
Expand Up @@ -38,6 +38,7 @@ public async Task Init(Block block, RawOperation op, RawDelegationContent conten
Sender = sender,
Delegate = delegat,
PrevDelegate = sender.Delegate,
Amount = sender.Balance - content.Fee,
Status = content.Metadata.Result.Status switch
{
"applied" => OperationStatus.Applied,
Expand Down
Expand Up @@ -39,6 +39,7 @@ public async Task Init(Block block, RawOperation op, RawDelegationContent conten
Sender = sender,
Delegate = delegat,
PrevDelegate = sender.Delegate,
Amount = sender.Balance - content.Fee,
Status = content.Metadata.Result.Status switch
{
"applied" => OperationStatus.Applied,
Expand Down Expand Up @@ -74,6 +75,7 @@ public async Task Init(Block block, TransactionOperation parent, RawInternalDele
Sender = sender,
Delegate = delegat,
PrevDelegate = sender.Delegate,
Amount = sender.Balance,
Status = content.Result.Status switch
{
"applied" => OperationStatus.Applied,
Expand Down
Expand Up @@ -39,6 +39,7 @@ public async Task Init(Block block, RawOperation op, RawDelegationContent conten
Sender = sender,
Delegate = delegat,
PrevDelegate = sender.Delegate,
Amount = sender.Balance - content.Fee,
Status = content.Metadata.Result.Status switch
{
"applied" => OperationStatus.Applied,
Expand Down Expand Up @@ -76,6 +77,7 @@ public async Task Init(Block block, TransactionOperation parent, RawInternalDele
Sender = sender,
Delegate = delegat,
PrevDelegate = sender.Delegate,
Amount = sender.Balance,
Status = content.Result.Status switch
{
"applied" => OperationStatus.Applied,
Expand Down

0 comments on commit 8363fd8

Please sign in to comment.