Skip to content

Commit

Permalink
Include storage in txs via WS by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Mar 13, 2021
1 parent 3328475 commit 5c8db8e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
27 changes: 25 additions & 2 deletions Tzkt.Api/Repositories/OperationRepository.cs
Expand Up @@ -4306,7 +4306,8 @@ public async Task<IEnumerable<TransactionOperation>> GetTransactions(Block block
OffsetParameter offset,
int limit,
MichelineFormat format,
Symbols quote)
Symbols quote,
bool includeStorage = false)
{
#region backward compatibility
// TODO: remove it asap
Expand All @@ -4321,7 +4322,21 @@ public async Task<IEnumerable<TransactionOperation>> GetTransactions(Block block
}
#endregion

var sql = new SqlBuilder(@"SELECT o.*, b.""Hash"" FROM ""TransactionOps"" AS o INNER JOIN ""Blocks"" as b ON b.""Level"" = o.""Level""")
var query = includeStorage
? $@"
SELECT o.*, b.""Hash"", s.""{((int)format < 2 ? "JsonValue" : "RawValue")}""
FROM ""TransactionOps"" AS o
INNER JOIN ""Blocks"" as b
ON b.""Level"" = o.""Level""
LEFT JOIN ""Storages"" as s
ON s.""Id"" = o.""StorageId"""
: @"
SELECT o.*, b.""Hash""
FROM ""TransactionOps"" AS o
INNER JOIN ""Blocks"" as b
ON b.""Level"" = o.""Level""";

var sql = new SqlBuilder(query)
.Filter(anyof, x => x == "sender" ? "SenderId" : x == "target" ? "TargetId" : "InitiatorId")
.Filter("InitiatorId", initiator, x => "TargetId")
.Filter("SenderId", sender, x => "TargetId")
Expand Down Expand Up @@ -4384,6 +4399,14 @@ public async Task<IEnumerable<TransactionOperation>> GetTransactions(Block block
_ => throw new Exception("Invalid MichelineFormat value")
}
},
Storage = !includeStorage ? null : format switch
{
MichelineFormat.Json => row.JsonValue == null ? null : new JsonString(row.JsonValue),
MichelineFormat.JsonString => row.JsonValue,
MichelineFormat.Raw => row.RawValue == null ? null : new JsonString(Micheline.ToJson(row.RawValue)),
MichelineFormat.RawString => row.RawValue == null ? null : Micheline.ToJson(row.RawValue),
_ => throw new Exception("Invalid MichelineFormat value")
},
Status = StatusToString(row.Status),
Errors = row.Errors != null ? OperationErrorSerializer.Deserialize(row.Errors) : null,
HasInternals = row.InternalOperations > 0,
Expand Down
15 changes: 11 additions & 4 deletions Tzkt.Api/Utils/SqlBuilder.cs
Expand Up @@ -487,10 +487,17 @@ public SqlBuilder Filter(string column, JsonParameter json, Func<string, string>
{
foreach (var (path, value) in json.Null)
{
if (value && path.Length > 0)
AppendFilter($@"""{column}"" IS NOT NULL");

AppendFilter($@"""{column}""#>>'{{{path}}}' IS {(value ? "" : "NOT ")}NULL");
if (path.Length == 0)
{
AppendFilter($@"""{column}"" IS {(value ? "" : "NOT ")}NULL");
}
else
{
if (value)
AppendFilter($@"""{column}"" IS NOT NULL");

AppendFilter($@"""{column}""#>>'{{{path}}}' IS {(value ? "" : "NOT ")}NULL");
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tzkt.Api/Websocket/Processors/OperationsProcessor.cs
Expand Up @@ -113,7 +113,7 @@ public async Task OnStateChanged()
: Task.FromResult(Enumerable.Empty<Models.OriginationOperation>());

var transactions = ActiveOps.HasFlag(Operations.Transactions)
? Repo.GetTransactions(null, null, null, null, null, level, null, null, null, null, null, null, null, null, limit, MichelineFormat.Json, symbols)
? Repo.GetTransactions(null, null, null, null, null, level, null, null, null, null, null, null, null, null, limit, MichelineFormat.Json, symbols, true)
: Task.FromResult(Enumerable.Empty<Models.TransactionOperation>());

var reveals = ActiveOps.HasFlag(Operations.Reveals)
Expand Down

0 comments on commit 5c8db8e

Please sign in to comment.