Skip to content

Commit

Permalink
fix: fix smart contracts transaction count
Browse files Browse the repository at this point in the history
  • Loading branch information
Naupio committed Feb 2, 2023
1 parent 3db4426 commit f0230bc
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
31 changes: 18 additions & 13 deletions lib/godwoken_explorer/graphql/resolovers/smart_contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +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, as: :account, on: sc.account_id == a.id)
|> select(
[smart_contract: sc],
[smart_contract: sc, account: a],
merge(sc, %{
name:
fragment(
Expand All @@ -62,18 +63,22 @@ 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
})
)

query =
from(s in SmartContract,
right_join: sq in subquery(sq),
as: :ckb_sorted,
as: :sc_processed,
on: s.id == sq.id,
inner_join: a in Account,
as: :account,
on: a.id == sq.account_id,
select: sq
)
|> smart_contracts_conditions(input)
Expand All @@ -95,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 @@ -117,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 @@ -145,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: 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
59 changes: 55 additions & 4 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,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 {
Expand Down

0 comments on commit f0230bc

Please sign in to comment.