From 02e15de3091c79bb59d8003ed07549bdee425d9a Mon Sep 17 00:00:00 2001 From: Roman Novikov Date: Tue, 16 Aug 2022 17:03:23 +0300 Subject: [PATCH 1/4] Add token batch request --- Tzkt.Api/Controllers/TokensController.cs | 19 +++++++++++++++++++ Tzkt.Api/Repositories/TokensRepository.cs | 10 ++++++++++ 2 files changed, 29 insertions(+) diff --git a/Tzkt.Api/Controllers/TokensController.cs b/Tzkt.Api/Controllers/TokensController.cs index 68e0921d8..a12661b31 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/Repositories/TokensRepository.cs b/Tzkt.Api/Repositories/TokensRepository.cs index 9ea0e361c..50d8ac847 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,15 @@ 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})"); + return await db.QueryAsync(sql.Query); + } #endregion #region token balances From 3338559a2bbfa32ab421f12b4fba765ad29a0edd Mon Sep 17 00:00:00 2001 From: Roman Novikov Date: Wed, 17 Aug 2022 13:33:46 +0300 Subject: [PATCH 2/4] fix mapping --- Tzkt.Api/Repositories/TokensRepository.cs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Tzkt.Api/Repositories/TokensRepository.cs b/Tzkt.Api/Repositories/TokensRepository.cs index 50d8ac847..ef51081e1 100644 --- a/Tzkt.Api/Repositories/TokensRepository.cs +++ b/Tzkt.Api/Repositories/TokensRepository.cs @@ -242,7 +242,26 @@ public async Task> GetTokensBatch(IEnumerable ids) 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})"); - return await db.QueryAsync(sql.Query); + 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 From 8d7ea0793c3027ea9b84abc0171a4deb182393b1 Mon Sep 17 00:00:00 2001 From: Roman Novikov Date: Wed, 17 Aug 2022 14:29:20 +0300 Subject: [PATCH 3/4] add missed model --- Tzkt.Api/Models/Tokens/TokenIdList.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Tzkt.Api/Models/Tokens/TokenIdList.cs 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; } + } +} + From d44e382d00f00f4b1d28da84bfd5ad82916f5d58 Mon Sep 17 00:00:00 2001 From: Roman Novikov Date: Thu, 18 Aug 2022 17:39:42 +0300 Subject: [PATCH 4/4] fix return type --- Tzkt.Api/Controllers/TokensController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tzkt.Api/Controllers/TokensController.cs b/Tzkt.Api/Controllers/TokensController.cs index a12661b31..75a1ce47b 100644 --- a/Tzkt.Api/Controllers/TokensController.cs +++ b/Tzkt.Api/Controllers/TokensController.cs @@ -86,7 +86,7 @@ public async Task>> GetTokens( /// TokenIdList /// [HttpPost("")] - public async Task> GetTokensBatch( + public async Task>> GetTokensBatch( [FromBody] object value) { var ids = JsonConvert.DeserializeObject(value.ToString()).Ids.