Skip to content

Commit

Permalink
Contract interface endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus authored and Groxan committed Apr 4, 2021
1 parent a983e5b commit bf2751e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.backup
bin/
Makefile
docker-compose.yml
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ migration:

sync:
export $$(cat .env | xargs) && dotnet run -p Tzkt.Sync -v normal

api-image:
docker build -t bakingbad/tzkt-api:latest -f ./Tzkt.Api/Dockerfile .

sync-image:
docker build -t bakingbad/tzkt-sync:latest -f ./Tzkt.Sync/Dockerfile .
16 changes: 14 additions & 2 deletions Tzkt.Api/Controllers/ContractsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ public async Task<object> GetCode([Address] string address, [Range(0, 2)] int fo
return await Accounts.GetByteCode(address);
}

/// <summary>
/// Get JSON Schema [2020-12] interface for the contract
/// </summary>
/// <remarks>
/// Returns standard JSON Schema for contract storage, entrypoints, and Big_map entries.
/// </remarks>
/// <param name="address">Contract address</param>
/// <returns></returns>
[HttpGet("{address}/interface")]
public Task<ContractInterface> GetInterface([Address] string address)
{
return Accounts.GetContractInterface(address);
}

/// <summary>
/// Get contract entrypoints
/// </summary>
Expand Down Expand Up @@ -350,8 +364,6 @@ public Task<IMicheline> GetRawStorage([Address] string address, [Min(0)] int lev
[HttpGet("{address}/storage/raw/schema")]
public Task<IMicheline> GetRawStorageSchema([Address] string address, [Min(0)] int level = 0)
{
if (level == 0)
return Accounts.GetRawStorageSchema(address);
return Accounts.GetRawStorageSchema(address, level);
}

Expand Down
62 changes: 62 additions & 0 deletions Tzkt.Api/Models/Accounts/ContractInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Tzkt.Api.Models
{
public class BigMapInterface
{
/// <summary>
/// Full path to the Big_map in the contract storage
/// </summary>
public string Path { get; set; }

/// <summary>
/// Big_map name, if exists (field annotation)
/// </summary>
public string Name { get; set; }

/// <summary>
/// JSON Schema of the Big_map key in humanified format (as returned by API)
/// </summary>
public JsonString KeySchema { get; set; }

/// <summary>
/// JSON Schema of the Big_map value in humanified format (as returned by API)
/// </summary>
public JsonString ValueSchema { get; set; }
}

public class EntrypointInterface
{
/// <summary>
/// Entrypoint name
/// </summary>
public string Name { get; set; }

/// <summary>
/// JSON Schema of the entrypoint parameter in humanified format (as returned by API)
/// </summary>
public JsonString ParameterSchema { get; set; }
}

public class ContractInterface
{
/// <summary>
/// JSON Schema of the contract storage in humanified format (as returned by API)
/// </summary>
public JsonString StorageSchema { get; set; }

/// <summary>
/// List of terminal entrypoints
/// </summary>
public List<EntrypointInterface> Entrypoints { get; set; }

/// <summary>
/// List of currently available Big_maps
/// </summary>
public List<BigMapInterface> BigMaps { get; set; }
}
}
53 changes: 53 additions & 0 deletions Tzkt.Api/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Dapper;
Expand Down Expand Up @@ -2264,6 +2266,57 @@ public async Task<string> GetMichelsonCode(string address)
return code.ToMichelson();
}

public async Task<ContractInterface> GetContractInterface(string address)
{
var rawAccount = await Accounts.GetAsync(address);
if (rawAccount is not RawContract contract) return null;

ContractParameter param;
ContractStorage storage;

if (contract.Kind == 0)
{
param = Data.Models.Script.ManagerTz.Parameter;
storage = Data.Models.Script.ManagerTz.Storage;
}
else
{
using var db = GetConnection();
var script = await db.QueryFirstOrDefaultAsync($@"
SELECT ""StorageSchema"", ""ParameterSchema""
FROM ""Scripts""
WHERE ""ContractId"" = {contract.Id} AND ""Current"" = true
LIMIT 1"
);
if (script == null) return null;
param = new ContractParameter(Micheline.FromBytes(script.ParameterSchema));
storage = new ContractStorage(Micheline.FromBytes(script.StorageSchema));
}

var rawStorage = await GetRawStorageValue(address);
var storageTreeView = storage.Schema.ToTreeView(rawStorage);

return new ContractInterface{
StorageSchema = storage.GetJsonSchema(),
Entrypoints = param.Entrypoints
.Where(x => param.IsEntrypointUseful(x.Key))
.Select(x => new EntrypointInterface{
Name = x.Key,
ParameterSchema = x.Value.GetJsonSchema()
})
.ToList(),
BigMaps = storageTreeView.Nodes()
.Where(x => x.Schema is BigMapSchema)
.Select(x => new BigMapInterface{
Name = x.Name,
Path = x.Path,
KeySchema = (x.Schema as BigMapSchema).Key.GetJsonSchema(),
ValueSchema = (x.Schema as BigMapSchema).Value.GetJsonSchema()
})
.ToList()
};
}

public async Task<IMicheline> BuildEntrypointParameters(string address, string name, object value)
{
var rawAccount = await Accounts.GetAsync(address);
Expand Down

0 comments on commit bf2751e

Please sign in to comment.