Skip to content

Commit

Permalink
Add related contract creation time
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Jun 15, 2020
1 parent 8085ef2 commit 80baa4b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Tzkt.Api/Controllers/AccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public Task<Account> GetByAddress([Address] string address, bool metadata = fals
/// Returns a list of contracts created by (or related to) the specified account.
/// </remarks>
/// <param name="address">Account address (starting with tz or KT)</param>
/// <param name="sort">Sorts contracts by specified field. Supported fields: `id` (default, desc), `balance`.</param>
/// <param name="sort">Sorts contracts by specified field. Supported fields: `id` (default, desc), `balance`, `creationLevel`.</param>
/// <param name="offset">Specifies which or how many items should be skipped</param>
/// <param name="limit">Maximum number of items to return</param>
/// <returns></returns>
Expand All @@ -145,7 +145,7 @@ public Task<Account> GetByAddress([Address] string address, bool metadata = fals
[Range(0, 10000)] int limit = 100)
{
#region validate
if (sort != null && !sort.Validate("id", "balance"))
if (sort != null && !sort.Validate("id", "balance", "creationLevel"))
return new BadRequest($"{nameof(sort)}", "Sorting by the specified field is not allowed.");
#endregion

Expand Down
10 changes: 10 additions & 0 deletions Tzkt.Api/Models/Accounts/RelatedContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,15 @@ public class RelatedContract
/// Information about the current delegate of the contract. `null` if it's not delegated
/// </summary>
public DelegateInfo Delegate { get; set; }

/// <summary>
/// Height of the block where the contract was created
/// </summary>
public int CreationLevel { get; set; }

/// <summary>
/// Datetime of the block where the contract was created (ISO 8601, e.g. `2020-02-20T02:40:57Z`)
/// </summary>
public DateTime? CreationTime { get; set; }
}
}
19 changes: 13 additions & 6 deletions Tzkt.Api/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2193,9 +2193,14 @@ public async Task<int> GetContractsCount(ContractKindParameter kind)
if (account == null || account.ContractsCount == 0)
return Enumerable.Empty<RelatedContract>();

var sql = new SqlBuilder(@"SELECT ""Id"", ""Kind"", ""Address"", ""Balance"", ""DelegateId"" FROM ""Accounts""")
var sql = new SqlBuilder(@"SELECT ""Id"", ""Kind"", ""Address"", ""Balance"", ""DelegateId"", ""FirstLevel"" FROM ""Accounts""")
.Filter($@"(""CreatorId"" = {account.Id} OR ""ManagerId"" = {account.Id})")
.Take(sort ?? new SortParameter { Desc = "id" }, offset, limit, x => x == "balance" ? ("Balance", "Balance") : ("Id", "Id"));
.Take(sort ?? new SortParameter { Desc = "id" }, offset, limit, x => x switch
{
"balance" => ("Balance", "Balance"),
"creationLevel" => ("Id", "FirstLevel"),
_ => ("Id", "Id")
});

using var db = GetConnection();
var rows = await db.QueryAsync(sql.Query, sql.Params);
Expand All @@ -2204,8 +2209,8 @@ public async Task<int> GetContractsCount(ContractKindParameter kind)
{
var metadata = Accounts.GetMetadata((int)row.Id);
var delegat = row.DelegatId == null ? null
: Accounts.Get((int)row.DelegatId);
var delegat = row.DelegateId == null ? null
: Accounts.Get((int)row.DelegateId);
var delegatMetadata = delegat == null ? null
: Accounts.GetMetadata(delegat.Id);
Expand All @@ -2216,13 +2221,15 @@ public async Task<int> GetContractsCount(ContractKindParameter kind)
Alias = metadata?.Alias,
Address = row.Address,
Balance = row.Balance,
Delegate = row.DelegatId == null ? null
Delegate = row.DelegateId == null ? null
: new DelegateInfo
{
Alias = delegatMetadata?.Alias,
Address = delegat.Address,
Active = delegat.Staked
}
},
CreationLevel = row.FirstLevel,
CreationTime = Time[row.FirstLevel]
};
});
}
Expand Down

0 comments on commit 80baa4b

Please sign in to comment.