Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix smart contracts api raise with nil sort type #1301

Merged
merged 2 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions lib/godwoken_explorer/graphql/resolovers/smart_contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 =
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/godwoken_explorer/graphql/types/smart_contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions lib/godwoken_explorer/graphql/types/udt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion lib/godwoken_explorer/smart_contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 57 additions & 5 deletions test/graphql/smart_contract_test.exs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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)
ckb_contract_account = insert(:ckb_contract_account)
_ = 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 =
Expand Down Expand Up @@ -123,14 +124,65 @@ 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 {
smart_contracts(
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 }
]
}
) {
Expand Down