Skip to content

Commit

Permalink
Expose smart rollup entrypoints via API
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed May 28, 2024
1 parent 7343c6a commit 6eae632
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Tzkt.Api/Controllers/SmartRollupsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,40 @@ public async Task<ActionResult<SmartRollup>> GetSmartRollup([Address] string add
return this.Bytes(res);
}

/// <summary>
/// Get smart rollup entrypoints
/// </summary>
/// <remarks>
/// Returns entrypoints of the specified smart rollup.
/// </remarks>
/// <param name="address">Smart rollup address (starting with sr1)</param>
/// <param name="all">If true, returns all entrypoints, including unused ones.
/// Unused means that the entrypoint can be normalized to a more specific one.
/// For example here `(or %entry1 (unit %entry2) (nat %entry3))` the `%entry1` is unused entrypoint
/// because it can be normalized to `%entry2` or `%entry3`</param>
/// <param name="json">Include parameters schema in human-readable JSON format</param>
/// <param name="micheline">Include parameters schema in micheline format</param>
/// <param name="michelson">Include parameters schema in michelson format</param>
/// <returns></returns>
[HttpGet("{address}/entrypoints")]
public async Task<ActionResult<IEnumerable<Entrypoint>>> GetEntrypoints(
[Required][Address] string address,
bool all = false,
bool json = true,
bool micheline = false,
bool michelson = false)
{
var query = ResponseCacheService.BuildKey(Request.Path.Value,
("all", all), ("json", json), ("micheline", micheline), ("michelson", michelson));

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

var res = await SmartRollups.GetSmartRollupEntrypoints(address, all, json, micheline, michelson);
cached = ResponseCache.Set(query, res);
return this.Bytes(cached);
}

/// <summary>
/// Get JSON Schema [2020-12] interface for the smart rollup
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions Tzkt.Api/Repositories/SmartRollupsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,42 @@ public async Task<SmartRollup> GetSmartRollup(string address)
};
}

public async Task<IEnumerable<Entrypoint>> GetSmartRollupEntrypoints(string address, bool all, bool json, bool micheline, bool michelson)
{
var rawAccount = await Accounts.GetAsync(address);
if (rawAccount is not RawSmartRollup rollup)
return Enumerable.Empty<Entrypoint>();

await using var db = await DataSource.OpenConnectionAsync();
var row = await db.QueryFirstOrDefaultAsync($@"SELECT ""ParameterSchema"" FROM ""Accounts"" WHERE ""Id"" = {rollup.Id}");
if (row == null)
return Enumerable.Empty<Entrypoint>();

var param = new ContractParameter(new MichelinePrim
{
Prim = PrimType.parameter,
Args = new List<IMicheline>
{
Micheline.FromBytes(row.ParameterSchema)
}
});

return param.Entrypoints
.Where(x => all || param.IsEntrypointUseful(x.Key))
.Select(x =>
{
var mich = micheline ? x.Value.ToMicheline() : null;
return new Entrypoint
{
Name = x.Key,
JsonParameters = json ? (RawJson)x.Value.Humanize() : null,
MichelineParameters = mich,
MichelsonParameters = michelson ? (mich ?? x.Value.ToMicheline()).ToMichelson() : null,
Unused = all && !param.IsEntrypointUseful(x.Key)
};
});
}

public async Task<RawJson> GetSmartRollupInterface(string address)
{
var rawAccount = await Accounts.GetAsync(address);
Expand Down

0 comments on commit 6eae632

Please sign in to comment.