diff --git a/lib/godwoken_explorer/graphql/resolovers/smart_contract.ex b/lib/godwoken_explorer/graphql/resolovers/smart_contract.ex index 1cc42b91..4a11bbbd 100644 --- a/lib/godwoken_explorer/graphql/resolovers/smart_contract.ex +++ b/lib/godwoken_explorer/graphql/resolovers/smart_contract.ex @@ -48,9 +48,9 @@ defmodule GodwokenExplorer.Graphql.Resolvers.SmartContract do def smart_contracts(_parent, %{input: input} = _args, _resolution) do sq = from(sc in SmartContract, as: :smart_contract) - |> join(:inner, [sc], a in Account, on: sc.account_id == a.id, as: :account) + |> join(:inner, [sc], a in Account, as: :account, on: sc.account_id == a.id) |> select( - [smart_contract: sc], + [smart_contract: sc, account: a], merge(sc, %{ name: fragment( @@ -63,18 +63,25 @@ defmodule GodwokenExplorer.Graphql.Resolvers.SmartContract do "CASE WHEN ? IS NULL THEN 0 ELSE ? END", sc.ckb_balance, sc.ckb_balance - ) + ), + transaction_count: + fragment( + "CASE WHEN ? IS NULL THEN 0 ELSE ? END", + a.transaction_count, + a.transaction_count + ), + eth_address: a.eth_address }) ) - |> smart_contracts_conditions(input) query = from(s in SmartContract, right_join: sq in subquery(sq), - as: :ckb_sorted, + as: :sc_processed, on: s.id == sq.id, select: sq ) + |> smart_contracts_conditions(input) |> smart_contracts_order_by(input) return = @@ -93,7 +100,7 @@ defmodule GodwokenExplorer.Graphql.Resolvers.SmartContract do case i do {:contract_addresses, addresses} -> if length(addresses) > 0 do - dynamic([account: a], ^acc and a.eth_address in ^addresses) + dynamic([sc_processed: sc], ^acc and sc.eth_address in ^addresses) else acc end @@ -115,13 +122,13 @@ defmodule GodwokenExplorer.Graphql.Resolvers.SmartContract do |> Enum.map(fn e -> case e do %{sort_type: st, sort_value: :ex_tx_count} -> - {st, dynamic([account: a], a.transaction_count)} + {st, dynamic([sc_processed: sc], sc.transaction_count)} %{sort_type: st, sort_value: :ckb_balance} -> - {st, dynamic([ckb_sorted: c], c.ckb_balance)} + {st, dynamic([sc_processed: sc], sc.ckb_balance)} %{sort_type: st, sort_value: :name} -> - {st, dynamic([ckb_sorted: c], c.name)} + {st, dynamic([sc_processed: sc], sc.name)} _ -> cursor_order_sorter(e, :order, @sorter_fields) @@ -143,13 +150,13 @@ defmodule GodwokenExplorer.Graphql.Resolvers.SmartContract do |> Enum.map(fn e -> case e do %{sort_type: st, sort_value: :ex_tx_count} -> - {{:account, :transaction_count}, st} + {{:sc_processed, :transaction_count}, st} %{sort_type: st, sort_value: :ckb_balance} -> - {{:ckb_sorted, :ckb_balance}, st} + {{:sc_processed, :ckb_balance}, st} %{sort_type: st, sort_value: :name} -> - {{:ckb_sorted, :name}, st} + {{:sc_processed, :name}, st} _ -> cursor_order_sorter(e, :cursor, @sorter_fields) diff --git a/lib/godwoken_explorer/graphql/types/smart_contract.ex b/lib/godwoken_explorer/graphql/types/smart_contract.ex index d453134f..3f3215f6 100644 --- a/lib/godwoken_explorer/graphql/types/smart_contract.ex +++ b/lib/godwoken_explorer/graphql/types/smart_contract.ex @@ -219,7 +219,7 @@ defmodule GodwokenExplorer.Graphql.Types.SmartContract do end input_object :smart_contracts_sorter_input do - field(:sort_type, :sort_type) - field(:sort_value, :smart_contracts_sorter) + field(:sort_type, non_null :sort_type) + field(:sort_value, non_null :smart_contracts_sorter) end end diff --git a/lib/godwoken_explorer/graphql/types/udt.ex b/lib/godwoken_explorer/graphql/types/udt.ex index 796288d9..b2160e45 100644 --- a/lib/godwoken_explorer/graphql/types/udt.ex +++ b/lib/godwoken_explorer/graphql/types/udt.ex @@ -1495,17 +1495,17 @@ defmodule GodwokenExplorer.Graphql.Types.UDT do end input_object :udts_sorter_input do - field(:sort_type, :sort_type) - field(:sort_value, :udts_sorter) + field(:sort_type, non_null :sort_type) + field(:sort_value, non_null :udts_sorter) end input_object :erc721_udts_sorter_input do - field(:sort_type, :sort_type) - field(:sort_value, :erc721_udts_sorter) + field(:sort_type, non_null :sort_type) + field(:sort_value, non_null :erc721_udts_sorter) end input_object :erc1155_udts_sorter_input do - field(:sort_type, :sort_type) - field(:sort_value, :erc1155_udts_sorter) + field(:sort_type, non_null :sort_type) + field(:sort_value, non_null :erc1155_udts_sorter) end end diff --git a/lib/godwoken_explorer/smart_contract.ex b/lib/godwoken_explorer/smart_contract.ex index a9c06161..4a303d51 100644 --- a/lib/godwoken_explorer/smart_contract.ex +++ b/lib/godwoken_explorer/smart_contract.ex @@ -59,7 +59,9 @@ defmodule GodwokenExplorer.SmartContract do field :compiler_file_format, :string field :other_info, :string field :ckb_balance, :decimal - field(:eth_address, :binary, virtual: true) + + field :transaction_count, :integer, virtual: true + field :eth_address, :binary, virtual: true timestamps() end diff --git a/test/graphql/smart_contract_test.exs b/test/graphql/smart_contract_test.exs index d18c0c91..8281721f 100644 --- a/test/graphql/smart_contract_test.exs +++ b/test/graphql/smart_contract_test.exs @@ -1,6 +1,6 @@ defmodule GodwokenExplorer.Graphql.SmartContractTest do use GodwokenExplorerWeb.ConnCase - import GodwokenExplorer.Factory, only: [insert!: 1, insert!: 2, insert: 1, insert: 2] + import GodwokenExplorer.Factory, only: [insert!: 2, insert: 1, insert: 2] setup do ckb_account = insert(:ckb_account) @@ -8,11 +8,12 @@ defmodule GodwokenExplorer.Graphql.SmartContractTest do _ = insert(:ckb_udt) _ = insert(:ckb_native_udt) - for _ <- 1..3 do - _smart_contract = insert(:smart_contract) + for index <- 1..3 do + polyjuice_contract_account = insert!(:polyjuice_contract_account, transaction_count: index) + _smart_contract = insert(:smart_contract, account: polyjuice_contract_account) end - polyjuice_contract_account = insert!(:polyjuice_contract_account) + polyjuice_contract_account = insert!(:polyjuice_contract_account, transaction_count: 4) smart_contract = insert!(:smart_contract, account: polyjuice_contract_account) _cub = @@ -123,6 +124,56 @@ defmodule GodwokenExplorer.Graphql.SmartContractTest do } = json_response(conn, 200) end + test "graphql: smart_contracts with EX_TX_COUNT sorter ", %{ + conn: conn + } do + query = """ + query { + smart_contracts( + input: { + limit: 2 + sorter: [ + { sort_type: DESC, sort_value: EX_TX_COUNT } + ] + } + ) { + entries { + name + account_id + account { + eth_address + transaction_count + } + ckb_balance + } + metadata { + total_count + after + before + } + } + } + """ + + conn = + post(conn, "/graphql", %{ + "query" => query, + "variables" => %{} + }) + + %{ + "data" => %{ + "smart_contracts" => %{ + "entries" => [%{"ckb_balance" => "10000", "account" => %{"transaction_count" => 4}} | _], + "metadata" => %{ + "total_count" => 4, + "after" => _after_value + } + } + } + } = json_response(conn, 200) + end + test "graphql: smart_contracts with ckb_balance_sorter", %{conn: conn} do query = """ query { @@ -130,7 +181,8 @@ defmodule GodwokenExplorer.Graphql.SmartContractTest do input: { limit: 2 sorter: [ - { sort_type: DESC, sort_value: CKB_BALANCE } + { sort_type: DESC, sort_value: CKB_BALANCE }, + { sort_type: DESC, sort_value: EX_TX_COUNT } ] } ) {