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

Add getGenesisAddress query to graphql #808

Merged
merged 15 commits into from
Jan 13, 2023
2 changes: 1 addition & 1 deletion lib/archethic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ defmodule Archethic do
TransactionChain.fetch_genesis_address_remotely(address)

genesis_address ->
genesis_address
{:ok, genesis_address}
end
end

Expand Down
11 changes: 11 additions & 0 deletions lib/archethic_web/graphql_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ defmodule ArchethicWeb.GraphQLSchema do
end)
end

@desc """
Query the network to find the genesis address of a transaction
"""
field :genesis_address, :address do
arg(:address, non_null(:address))

resolve(fn %{address: address}, _ ->
Resolver.get_genesis_address(address)
end)
end

@desc """
Query the network to find the last transaction from an address
"""
Expand Down
10 changes: 10 additions & 0 deletions lib/archethic_web/graphql_schema/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ defmodule ArchethicWeb.GraphQLSchema.Resolver do

@limit_page 10

def get_genesis_address(address) do
case Archethic.fetch_genesis_address_remotely(address) do
{:ok, genesis_address} ->
{:ok, genesis_address}

_ ->
{:ok, address}
end
end

def get_balance(address) do
case Archethic.get_balance(address) do
{:ok, %{uco: uco, token: token_balances}} ->
Expand Down
47 changes: 47 additions & 0 deletions test/archethic_web/graphql_schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,53 @@ defmodule ArchethicWeb.GraphQLSchemaTest do
end
end

describe "query: genesis_address" do
test "should return the genesis address", %{conn: conn} do
addr = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

genesis_addr = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

MockClient
|> stub(:send_message, fn _, %GetFirstAddress{}, _ ->
{:ok, %FirstAddress{address: genesis_addr}}
end)

conn =
post(conn, "/api", %{
"query" => "query {
genesisAddress(address: \"#{Base.encode16(addr)}\")
}"
})

assert %{
"data" => %{
"genesisAddress" => genesis
}
} = json_response(conn, 200)

assert genesis == Base.encode16(genesis_addr)
end

test "should return same address", %{conn: conn} do
addr = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

conn =
post(conn, "/api", %{
"query" => "query {
genesisAddress(address: \"#{Base.encode16(addr)}\")
}"
})

assert %{
"data" => %{
"genesisAddress" => genesis
}
} = json_response(conn, 200)

assert genesis == Base.encode16(addr)
end
end

describe "query: transaction_inputs" do
test "should return a list of ledger inputs", %{conn: conn} do
addr = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
Expand Down