Skip to content

Commit 6f26cd1

Browse files
fix: sync GraphQL language enum with SmartContract schema (blockscout#14109)
Co-authored-by: Victor Baranov <baranov.viktor.27@gmail.com>
1 parent e546c95 commit 6f26cd1

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

apps/block_scout_web/lib/block_scout_web/graphql/schema/types.ex

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ end
107107

108108
defmodule BlockScoutWeb.GraphQL.Schema.Types do
109109
@moduledoc false
110-
use Utils.CompileTimeEnvHelper, chain_type: [:explorer, :chain_type]
111110

112111
require BlockScoutWeb.GraphQL.Schema.{Transaction, SmartContracts}
113112

113+
alias Ecto.Enum
114+
114115
use Absinthe.Schema.Notation
115116
use Absinthe.Relay.Schema.Notation, :modern
116117

@@ -124,23 +125,9 @@ defmodule BlockScoutWeb.GraphQL.Schema.Types do
124125

125126
alias BlockScoutWeb.GraphQL.Schema.SmartContracts, as: SmartContractsSchema
126127
alias BlockScoutWeb.GraphQL.Schema.Transaction, as: TransactionSchema
128+
alias Explorer.Chain.SmartContract
127129

128-
# TODO: leverage `Ecto.Enum.values(SmartContract, :language)` to deduplicate
129-
# language definitions
130-
@default_languages ~w(solidity vyper yul)a
131-
132-
case @chain_type do
133-
:arbitrum ->
134-
@chain_type_languages ~w(stylus_rust)a
135-
136-
:zilliqa ->
137-
@chain_type_languages ~w(scilla)a
138-
139-
_ ->
140-
@chain_type_languages ~w()a
141-
end
142-
143-
enum(:language, values: @default_languages ++ @chain_type_languages)
130+
enum(:language, values: Enum.values(SmartContract, :language))
144131

145132
import_types(Absinthe.Type.Custom)
146133
import_types(BlockScoutWeb.GraphQL.Schema.Scalars)

apps/block_scout_web/test/block_scout_web/graphql/schema/query/address_test.exs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,39 @@ defmodule BlockScoutWeb.GraphQL.Schema.Query.AddressTest do
9797
}
9898
end
9999

100+
test "smart_contract language field returns all supported languages", %{conn: conn} do
101+
for language <- [:solidity, :vyper, :yul, :geas] do
102+
address = insert(:address, fetched_coin_balance: 100)
103+
insert(:smart_contract, address_hash: address.hash, contract_code_md5: "123", language: language)
104+
105+
query = """
106+
query ($hash: AddressHash!) {
107+
address(hash: $hash) {
108+
smart_contract {
109+
language
110+
}
111+
}
112+
}
113+
"""
114+
115+
variables = %{"hash" => to_string(address.hash)}
116+
117+
conn = get(conn, "/api/v1/graphql", query: query, variables: variables)
118+
119+
expected = language |> to_string() |> String.upcase()
120+
121+
assert %{
122+
"data" => %{
123+
"address" => %{
124+
"smart_contract" => %{
125+
"language" => ^expected
126+
}
127+
}
128+
}
129+
} = json_response(conn, 200)
130+
end
131+
end
132+
100133
test "errors for non-existent address hash", %{conn: conn} do
101134
address = build(:address)
102135

0 commit comments

Comments
 (0)