diff --git a/Tzkt.Api/Controllers/TokensController.cs b/Tzkt.Api/Controllers/TokensController.cs index 68e0921d8..75a1ce47b 100644 --- a/Tzkt.Api/Controllers/TokensController.cs +++ b/Tzkt.Api/Controllers/TokensController.cs @@ -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; @@ -75,6 +76,24 @@ public async Task>> GetTokens( Rows = await Tokens.GetTokens(filter, pagination, selection.select.Fields ?? selection.select.Values) }); } + + /// + /// Get tokens by list of contract:tokenId + /// + /// + /// Returns a list of tokens. + /// + /// TokenIdList + /// + [HttpPost("")] + public async Task>> GetTokensBatch( + [FromBody] object value) + { + var ids = JsonConvert.DeserializeObject(value.ToString()).Ids. + Select(id => id.Split(":")); + var batch = await Tokens.GetTokensBatch(ids); + return Ok(batch); + } #endregion #region token balances diff --git a/Tzkt.Api/Models/Tokens/TokenIdList.cs b/Tzkt.Api/Models/Tokens/TokenIdList.cs new file mode 100644 index 000000000..cbe382984 --- /dev/null +++ b/Tzkt.Api/Models/Tokens/TokenIdList.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace Tzkt.Api.Models +{ + public class TokenIdList + { + public List Ids { get; set; } + } +} + diff --git a/Tzkt.Api/Repositories/TokensRepository.cs b/Tzkt.Api/Repositories/TokensRepository.cs index 9ea0e361c..ef51081e1 100644 --- a/Tzkt.Api/Repositories/TokensRepository.cs +++ b/Tzkt.Api/Repositories/TokensRepository.cs @@ -7,6 +7,7 @@ using Dapper; using Tzkt.Api.Models; using Tzkt.Api.Services.Cache; +using System; namespace Tzkt.Api.Repositories { @@ -234,6 +235,34 @@ public async Task GetTokens(TokenFilter filter, Pagination paginatio return result; } + + public async Task> GetTokensBatch(IEnumerable 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(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