From b3447d43766cc35da1abf9b968a839ac1326419b Mon Sep 17 00:00:00 2001 From: Alex Garibay Date: Tue, 2 Oct 2018 14:53:18 -0500 Subject: [PATCH] Trim address and token names of whitespace (#849) --- .../lib/explorer/chain/address/name.ex | 15 ++++++++++++- apps/explorer/lib/explorer/chain/token.ex | 12 ++++++++++ apps/explorer/test/explorer/chain_test.exs | 22 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain/address/name.ex b/apps/explorer/lib/explorer/chain/address/name.ex index 7a22f2bb27..ac094e8c45 100644 --- a/apps/explorer/lib/explorer/chain/address/name.ex +++ b/apps/explorer/lib/explorer/chain/address/name.ex @@ -3,8 +3,11 @@ defmodule Explorer.Chain.Address.Name do Represents a name for an Address. """ - use Explorer.Schema + use Ecto.Schema + import Ecto.Changeset + + alias Ecto.Changeset alias Explorer.Chain.{Address, Hash} @typedoc """ @@ -37,6 +40,16 @@ defmodule Explorer.Chain.Address.Name do struct |> cast(params, @allowed_fields) |> validate_required(@required_fields) + |> trim_name() |> foreign_key_constraint(:address_hash) end + + defp trim_name(%Changeset{valid?: false} = changeset), do: changeset + + defp trim_name(%Changeset{valid?: true} = changeset) do + case get_change(changeset, :name) do + nil -> changeset + name -> put_change(changeset, :name, String.trim(name)) + end + end end diff --git a/apps/explorer/lib/explorer/chain/token.ex b/apps/explorer/lib/explorer/chain/token.ex index 56c45f4d01..c9de6c4a34 100644 --- a/apps/explorer/lib/explorer/chain/token.ex +++ b/apps/explorer/lib/explorer/chain/token.ex @@ -20,6 +20,8 @@ defmodule Explorer.Chain.Token do use Ecto.Schema import Ecto.{Changeset, Query} + + alias Ecto.Changeset alias Explorer.Chain.{Address, Hash, Token, TokenTransfer} @typedoc """ @@ -72,9 +74,19 @@ defmodule Explorer.Chain.Token do |> cast(params, @required_attrs ++ @optional_attrs) |> validate_required(@required_attrs) |> foreign_key_constraint(:contract_address) + |> trim_name() |> unique_constraint(:contract_address_hash) end + defp trim_name(%Changeset{valid?: false} = changeset), do: changeset + + defp trim_name(%Changeset{valid?: true} = changeset) do + case get_change(changeset, :name) do + nil -> changeset + name -> put_change(changeset, :name, String.trim(name)) + end + end + def join_with_transfers(queryable \\ Token) do from( t in queryable, diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 1290fbeec1..581bff3f8b 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -1957,6 +1957,12 @@ defmodule Explorer.ChainTest do primary: true ) end + + test "trims whitespace from address name", %{valid_attrs: valid_attrs} do + attrs = %{valid_attrs | name: " SimpleStorage "} + assert {:ok, _} = Chain.create_smart_contract(attrs) + assert Repo.get_by(Address.Name, name: "SimpleStorage") + end end describe "stream_unfetched_balances/2" do @@ -2439,6 +2445,22 @@ defmodule Explorer.ChainTest do assert updated_token.cataloged end + test "trims names of whitespace" do + token = insert(:token, name: nil, symbol: nil, total_supply: nil, decimals: nil, cataloged: false) + + update_params = %{ + name: " Hodl Token ", + symbol: "HT", + total_supply: 10, + decimals: 1, + cataloged: true + } + + assert {:ok, updated_token} = Chain.update_token(token, update_params) + assert updated_token.name == "Hodl Token" + assert Repo.get_by(Address.Name, name: "Hodl Token") + end + test "inserts an address name record when token has a name in params" do token = insert(:token, name: nil, symbol: nil, total_supply: nil, decimals: nil, cataloged: false)