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: fix issue 801 account udts return with native and bridge token #831

Merged
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
2 changes: 2 additions & 0 deletions lib/godwoken_explorer/account/current_bridged_udt_balance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ defmodule GodwokenExplorer.Account.CurrentBridgedUDTBalance do
belongs_to(:account, GodwokenExplorer.Account, foreign_key: :account_id, references: :id)
belongs_to(:udt, GodwokenExplorer.UDT, foreign_key: :udt_id, references: :id)

field :uniq_id, :integer, virtual: true

timestamps(type: :utc_datetime_usec)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/godwoken_explorer/account/current_udt_balance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ defmodule GodwokenExplorer.Account.CurrentUDTBalance do
belongs_to(:account, GodwokenExplorer.Account, foreign_key: :account_id, references: :id)
belongs_to(:udt, GodwokenExplorer.UDT, foreign_key: :udt_id, references: :id)

field :uniq_id, :integer, virtual: true

timestamps(type: :utc_datetime_usec)
end

Expand Down
60 changes: 51 additions & 9 deletions lib/godwoken_explorer/graphql/resolovers/account_udt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ defmodule GodwokenExplorer.Graphql.Resolvers.AccountUDT do
from(cu in CurrentUDTBalance)
|> join(:inner, [cu], a1 in subquery(squery), on: cu.address_hash == a1.eth_address)
|> join(:inner, [cu], u in UDT,
on:
u.contract_address_hash == cu.token_contract_address_hash and not is_nil(u.name) and
cu.value != 0
on: u.contract_address_hash == cu.token_contract_address_hash
)
|> where(
[cu, _a1, u],
not is_nil(u.name) and
cu.value != 0
)
|> select_merge([_cu, _a1, u], %{udt_id: u.id, uniq_id: u.id})
|> order_by([cu], desc: cu.updated_at)

if is_nil(token_contract_address_hash) do
Expand Down Expand Up @@ -97,9 +101,12 @@ defmodule GodwokenExplorer.Graphql.Resolvers.AccountUDT do
from(cbu in CurrentBridgedUDTBalance)
|> join(:inner, [cbu], a1 in subquery(squery), on: cbu.address_hash == a1.eth_address)
|> join(:inner, [cbu], a2 in Account, on: cbu.udt_script_hash == a2.script_hash)
|> join(:inner, [cbu, _a1, a2], u in UDT,
on: u.id == a2.id and not is_nil(u.bridge_account_id) and cbu.value != 0
|> join(:inner, [cbu, _a1, a2], u in UDT, on: u.id == a2.id)
|> where(
[cbu, _a1, _a2, u],
not is_nil(u.bridge_account_id) and cbu.value != 0
)
|> select_merge([cbu, _a1, _a2, u], %{udt_id: u.id, uniq_id: u.bridge_account_id})
|> order_by([cbu], desc: cbu.updated_at)

if is_nil(udt_script_hash) do
Expand Down Expand Up @@ -161,6 +168,8 @@ defmodule GodwokenExplorer.Graphql.Resolvers.AccountUDT do
end
end

def account(%{token_contract_address_hash: nil}, _, _), do: {:ok, nil}

def account(
%CurrentUDTBalance{token_contract_address_hash: token_contract_address_hash} = _parent,
_args,
Expand Down Expand Up @@ -303,8 +312,7 @@ defmodule GodwokenExplorer.Graphql.Resolvers.AccountUDT do

result =
(cbus ++ cus)
|> Enum.sort_by(&Map.fetch(&1, :updated_at), :desc)
|> Enum.uniq_by(&Map.fetch(&1, :address_hash))
|> process_cus_cbus_balance()

{:ok, result}
else
Expand All @@ -318,6 +326,7 @@ defmodule GodwokenExplorer.Graphql.Resolvers.AccountUDT do
on: u.contract_address_hash == cu.token_contract_address_hash
)
|> where([cu, u], cu.value != 0 and not is_nil(u.name))
|> select_merge([_cu, u], %{udt_id: u.id, uniq_id: u.id})
|> Repo.all()

cbus =
Expand All @@ -328,15 +337,48 @@ defmodule GodwokenExplorer.Graphql.Resolvers.AccountUDT do
)
|> join(:inner, [cbu], u in UDT, on: cbu.udt_id == u.id)
|> where([cbu, u], cbu.value != 0 and not is_nil(u.bridge_account_id))
|> select_merge([_cu, u], %{udt_id: u.id, uniq_id: u.bridge_account_id})
|> Repo.all()

result =
(cbus ++ cus)
|> Enum.sort_by(&Map.fetch(&1, :updated_at), :desc)
|> Enum.uniq_by(&{Map.fetch(&1, :address_hash), Map.fetch(&1, :udt_id)})
|> process_cus_cbus_balance()

{:ok, result}
end
end
end

defp process_cus_cbus_balance(cus_cubs) do
cus_cubs
|> Enum.sort_by(&{&1.uniq_id, &1.updated_at}, :desc)
|> Enum.reduce({[], nil}, fn c_cb, {acc_list, base} ->
case base do
%{uniq_id: uniq_id} ->
if c_cb.uniq_id == uniq_id do
c_cb = %{c_cb | value: base.value}
{[c_cb | acc_list], c_cb}
else
{[c_cb | acc_list], c_cb}
end

nil ->
{[c_cb | acc_list], c_cb}
end
end)
|> (fn {a, _} -> a end).()
|> Enum.reverse()
|> Enum.map(fn e ->
if Enum.find(cus_cubs, fn x -> x.uniq_id == e.uniq_id and x.udt_id != e.udt_id end) do
e
else
if e.udt_id != e.uniq_id do
[e, %CurrentUDTBalance{address_hash: e.address_hash, value: e.value, udt_id: e.uniq_id}]
else
e
end
end
end)
|> List.flatten()
end
end
2 changes: 2 additions & 0 deletions lib/godwoken_explorer/graphql/types/account_udt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ defmodule GodwokenExplorer.Graphql.Types.AccountUDT do
field :token_contract_address_hash, :hash_address
field :udt_script_hash, :hash_full

field :uniq_id, :integer

field :udt, :udt do
resolve(&Resolvers.AccountUDT.udt/3)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/godwoken_explorer/graphql/types/udt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ defmodule GodwokenExplorer.Graphql.Types.UDT do
end

object :udt do
field :id, :string
field :id, :integer
field :decimal, :integer
field :name, :string
field :symbol, :string
Expand Down
6 changes: 3 additions & 3 deletions test/graphql/account_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ defmodule GodwokenExplorer.Graphql.AccountTest do
}
"""

native_udt_id = native_udt.id |> to_string()
ckb_udt_id = ckb_udt.id |> to_string()
native_udt_id = native_udt.id
ckb_udt_id = ckb_udt.id

conn =
post(conn, "/graphql", %{
Expand Down Expand Up @@ -128,7 +128,7 @@ defmodule GodwokenExplorer.Graphql.AccountTest do
"account" => %{
"type" => "UDT",
"bridged_udt" => %{
"id" => ckb_udt.id |> to_string(),
"id" => ckb_udt.id,
"name" => ckb_udt.name
}
}
Expand Down
52 changes: 51 additions & 1 deletion test/graphql/account_udt_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ defmodule GodwokenExplorer.Graphql.AccountUDTTest do

ckb_account = Factory.insert!(:ckb_account, script_hash: script_hash)

cub = Factory.insert!(:current_udt_balance, value: Enum.random(1..100_000))
cub =
Factory.insert!(:current_udt_balance,
token_contract_address_hash: native_udt.contract_address_hash,
value: Enum.random(1..100_000)
)

cbub =
Factory.insert!(:current_bridged_udt_balance,
address_hash: cub.address_hash,
value: Enum.random(1..100_000),
udt_id: ckb_udt.id,
udt_script_hash: ckb_udt.script_hash
Expand All @@ -29,6 +34,51 @@ defmodule GodwokenExplorer.Graphql.AccountUDTTest do
[native_udt: native_udt, ckb_udt: ckb_udt, ckb_account: ckb_account, cub: cub, cbub: cbub]
end

test "graphql: account_udts with native and bridge token", %{conn: conn, cbub: cbub} do
address = cbub.address_hash |> to_string()

query = """
query {
account_udts(
input: {
address_hashes: ["#{address}"],
}
) {
value
uniq_id
udt {
id
type
name
bridge_account_id
script_hash
decimal
value
}
account {
id
eth_address
script_hash
}
}
}
"""

conn =
post(conn, "/graphql", %{
"query" => query,
"variables" => %{}
})

%{
"data" => %{
"account_udts" => account_udts
}
} = json_response(conn, 200)

assert length(account_udts) == 2
end

test "graphql: account_current_udts ", %{conn: conn, cub: cub} do
address = cub.address_hash |> to_string()

Expand Down