Skip to content

Commit

Permalink
wip: expose endpoint to query smart rollup parameter json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Zaikin committed Oct 14, 2023
1 parent f6ed539 commit eaf6bb2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Tzkt.Api/Controllers/SmartRollupsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ public async Task<ActionResult<SmartRollup>> GetSmartRollup([Address] string add
return this.Bytes(res);
}

/// <summary>
/// Get JSON Schema [2020-12] interface for the smart rollup
/// </summary>
/// <remarks>
/// Returns standard JSON Schema for smart rollup parameter.
/// </remarks>
/// <param name="address">Smart rollup address</param>
/// <returns></returns>
[HttpGet("{address}/interface")]
public async Task<ActionResult<ContractInterface>> GetInterface([Required][Address] string address)
{
var query = ResponseCacheService.BuildKey(Request.Path.Value);

if (ResponseCache.TryGet(query, out var cached))
return this.Bytes(cached);

var res = await SmartRollups.GetSmartRollupInterface(address);
cached = ResponseCache.Set(query, res);
return this.Bytes(cached);
}

/// <summary>
/// Get smart rollup stakers
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions Tzkt.Api/Repositories/SmartRollupsRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Data;
using Dapper;
using Netezos.Encoding;
using Netezos.Contracts;
using Tzkt.Api.Models;
using Tzkt.Api.Services.Cache;

Expand Down Expand Up @@ -205,6 +206,26 @@ public async Task<SmartRollup> GetSmartRollup(string address)
};
}

public async Task<RawJson> GetSmartRollupInterface(string address)
{
var rawAccount = await Accounts.GetAsync(address);
if (rawAccount is not RawSmartRollup rollup)
return null;

using var db = GetConnection();

var parameterType = await db.QueryFirstOrDefaultAsync($@"
SELECT ""ParameterType""
FROM ""SmartRollupOriginateOps""
WHERE ""SmartRollupId"" = {rollup.Id}
LIMIT 1"
);
if (parameterType is not byte[] bytes)
return null;

return Schema.Create(Micheline.FromBytes(bytes) as MichelinePrim).GetJsonSchema();
}

public async Task<IEnumerable<SmartRollup>> GetSmartRollups(SrFilter filter, Pagination pagination)
{
var rows = await QuerySmartRollupsAsync(filter, pagination);
Expand Down

0 comments on commit eaf6bb2

Please sign in to comment.