From d8b22f69882a932a00aab0c31dca50ca58602ff6 Mon Sep 17 00:00:00 2001 From: Lucas Narciso Date: Fri, 14 Sep 2018 17:42:19 -0300 Subject: [PATCH] Handle large strings when fetching tokens --- apps/indexer/lib/indexer/token/fetcher.ex | 12 +++++++ .../test/indexer/token/fetcher_test.exs | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/apps/indexer/lib/indexer/token/fetcher.ex b/apps/indexer/lib/indexer/token/fetcher.ex index b33541d34a..865791b362 100644 --- a/apps/indexer/lib/indexer/token/fetcher.ex +++ b/apps/indexer/lib/indexer/token/fetcher.ex @@ -154,6 +154,7 @@ defmodule Indexer.Token.Fetcher do |> Map.put(:cataloged, true) |> Map.merge(token_contract_data) |> handle_invalid_strings() + |> handle_large_strings() end defp atomized_key("decimals"), do: :decimals @@ -191,4 +192,15 @@ defmodule Indexer.Token.Fetcher do |> Hash.to_string() |> String.slice(0, 6) end + + defp handle_large_strings(%{name: name, symbol: symbol, type: type} = token) do + [name, type, symbol] = Enum.map([name, type, symbol], &handle_large_string/1) + + %{token | name: name, symbol: symbol, type: type} + end + + defp handle_large_string(nil), do: nil + defp handle_large_string(string), do: handle_large_string(string, byte_size(string)) + defp handle_large_string(string, size) when size > 255, do: binary_part(string, 0, 255) + defp handle_large_string(string, _size), do: string end diff --git a/apps/indexer/test/indexer/token/fetcher_test.exs b/apps/indexer/test/indexer/token/fetcher_test.exs index 3c948e9838..f03949bf89 100644 --- a/apps/indexer/test/indexer/token/fetcher_test.exs +++ b/apps/indexer/test/indexer/token/fetcher_test.exs @@ -188,5 +188,37 @@ defmodule Indexer.Token.FetcherTest do assert {:ok, %Token{cataloged: true, name: nil}} = Chain.token_from_address_hash(contract_address_hash) end end + + test "shortens strings larger than 255 characters", %{json_rpc_named_arguments: json_rpc_named_arguments} do + long_token_name_shortened = + "