Trim address and token names of whitespace (#849)

pull/845/head
Alex Garibay 6 years ago committed by GitHub
parent f5a0e1ae57
commit b3447d4376
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      apps/explorer/lib/explorer/chain/address/name.ex
  2. 12
      apps/explorer/lib/explorer/chain/token.ex
  3. 22
      apps/explorer/test/explorer/chain_test.exs

@ -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

@ -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,

@ -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)

Loading…
Cancel
Save