Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add token batch request #123

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Tzkt.Api/Controllers/TokensController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Tzkt.Api.Models;
using Tzkt.Api.Repositories;
using Tzkt.Api.Services;
Expand Down Expand Up @@ -75,6 +76,24 @@ public async Task<ActionResult<IEnumerable<Token>>> GetTokens(
Rows = await Tokens.GetTokens(filter, pagination, selection.select.Fields ?? selection.select.Values)
});
}

/// <summary>
/// Get tokens by list of contract:tokenId
/// </summary>
/// <remarks>
/// Returns a list of tokens.
/// </remarks>
/// <param name="value">TokenIdList</param>
/// <returns></returns>
[HttpPost("")]
public async Task<ActionResult<IEnumerable<Token>>> GetTokensBatch(
[FromBody] object value)
{
var ids = JsonConvert.DeserializeObject<TokenIdList>(value.ToString()).Ids.
Select(id => id.Split(":"));
var batch = await Tokens.GetTokensBatch(ids);
return Ok(batch);
}
#endregion

#region token balances
Expand Down
10 changes: 10 additions & 0 deletions Tzkt.Api/Models/Tokens/TokenIdList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Tzkt.Api.Models
{
public class TokenIdList
{
public List<string> Ids { get; set; }
}
}

29 changes: 29 additions & 0 deletions Tzkt.Api/Repositories/TokensRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Dapper;
using Tzkt.Api.Models;
using Tzkt.Api.Services.Cache;
using System;

namespace Tzkt.Api.Repositories
{
Expand Down Expand Up @@ -234,6 +235,34 @@ public async Task<object[][]> GetTokens(TokenFilter filter, Pagination paginatio

return result;
}

public async Task<IEnumerable<Token>> GetTokensBatch(IEnumerable<string[]> ids)
{
using var db = GetConnection();
var cond = String.Join(",", ids.Select(x => $"('{Regex.Replace(x[0], @"\s+", "")}','{Regex.Replace(x[1], "[^0-9]", "")}')"));
var sql = new SqlBuilder($@"SELECT t.* FROM ""Tokens"" t left join ""Accounts"" a on t.""ContractId"" = a.""Id""
where(a.""Address"", t.""TokenId"") in ({cond})");
var rows = await db.QueryAsync<dynamic>(sql.Query);
var result = rows.Select(row => new Token
{
Contract = Accounts.GetAlias(row.ContractId),
Id = row.Id,
BalancesCount = row.BalancesCount,
FirstLevel = row.FirstLevel,
FirstTime = Times[row.FirstLevel],
HoldersCount = row.HoldersCount,
LastLevel = row.LastLevel,
LastTime = Times[row.LastLevel],
Standard = TokenStandards.ToString(row.Tags),
TokenId = row.TokenId,
TotalBurned = row.TotalBurned,
TotalMinted = row.TotalMinted,
TotalSupply = row.TotalSupply,
TransfersCount = row.TransfersCount,
Metadata = row.Metadata
});
return result;
}
#endregion

#region token balances
Expand Down