Skip to content

Commit

Permalink
feat: return event name when it is aexn (#1090)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeshe committed Dec 21, 2022
1 parent 71f5b84 commit bc54cd6
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 29 deletions.
8 changes: 8 additions & 0 deletions lib/ae_mdw/aexn_contracts.ex
Expand Up @@ -6,11 +6,13 @@ defmodule AeMdw.AexnContracts do
alias AeMdw.Contract
alias AeMdw.Db.Model
alias AeMdw.DryRun.Runner
alias AeMdw.Node
alias AeMdw.Node.Db, as: NodeDb
alias AeMdw.Log

import AeMdw.Util.Encoding, only: [encode_contract: 1]

@type event_name() :: String.t()
@typep pubkey :: NodeDb.pubkey()
@typep height :: AeMdw.Blocks.height()

Expand Down Expand Up @@ -110,6 +112,12 @@ defmodule AeMdw.AexnContracts do
end
end

@spec event_name(AeMdw.Contracts.event_hash()) :: event_name() | nil
def event_name(event_hash) do
Node.aexn_event_names()
|> Map.get(event_hash)
end

#
# Private functions
#
Expand Down
1 change: 1 addition & 0 deletions lib/ae_mdw/application.ex
Expand Up @@ -155,6 +155,7 @@ defmodule AeMdw.Application do
aeo_tree_pos: field_pos_map.(aeo_state_tree_code, :oracle_tree),
aex9_signatures: [{[], AeMdw.Node.aex9_signatures()}],
aexn_event_hash_types: [{[], AeMdw.Node.aexn_event_hash_types()}],
aexn_event_names: [{[], AeMdw.Node.aexn_event_names()}],
aex141_signatures: [{[], AeMdw.Node.aex141_signatures()}],
previous_aex141_signatures: [{[], AeMdw.Node.previous_aex141_signatures()}],
height_proto: [{[], AeMdw.Node.height_proto()}],
Expand Down
3 changes: 1 addition & 2 deletions lib/ae_mdw/contract.ex
Expand Up @@ -18,15 +18,14 @@ defmodule AeMdw.Contract do
@type fname :: binary()

@typep tx_hash :: binary()
@typep event_name :: {:internal_call_tx, fname()}
# :aec_blocks.micro_block()
@typep micro_block :: term()
@typep event_info :: Node.aetx() | :error
# :aetx.tx_type()
@typep event_type :: atom()
@type event_data :: %{tx_hash: tx_hash(), type: event_type(), info: event_info()}

@type event :: {event_name(), event_data()}
@type event :: {{:internal_call_tx, fname()}, event_data()}
@type event_hash :: <<_::256>>

@type call :: tuple()
Expand Down
4 changes: 3 additions & 1 deletion lib/ae_mdw/contracts.ex
Expand Up @@ -3,6 +3,7 @@ defmodule AeMdw.Contracts do
Context module for dealing with Contracts.
"""

alias AeMdw.AexnContracts
alias AeMdw.Collection
alias AeMdw.Contract
alias AeMdw.Db.Format
Expand All @@ -29,7 +30,7 @@ defmodule AeMdw.Contracts do
@type query() :: %{binary() => binary()}
@type local_idx() :: non_neg_integer()
@type log_key() :: {create_txi(), local_idx()}
@type event_hash() :: binary()
@type event_hash() :: <<_::256>>
@type log_idx() :: non_neg_integer()
@type call_key() :: {create_txi(), txi(), event_hash(), log_idx()}

Expand Down Expand Up @@ -523,6 +524,7 @@ defmodule AeMdw.Contracts do
args: Enum.map(args, fn <<topic::256>> -> to_string(topic) end),
data: maybe_encode_base64(data),
event_hash: Base.hex_encode32(event_hash),
event_name: AexnContracts.event_name(event_hash),
height: height,
micro_index: micro_index,
block_hash: encode(:micro_block_hash, block_hash),
Expand Down
13 changes: 11 additions & 2 deletions lib/ae_mdw/node.ex
Expand Up @@ -8,7 +8,9 @@ defmodule AeMdw.Node do
all of these functions more explicit.
"""

alias AeMdw.AexnContracts
alias AeMdw.Contract
alias AeMdw.Contracts

@type tx_type ::
:spend_tx
Expand Down Expand Up @@ -44,7 +46,6 @@ defmodule AeMdw.Node do
@opaque tx() :: tuple()
@opaque aect_call :: tuple()

@type event_hash :: <<_::256>>
@type aexn_event_type ::
:burn
| :mint
Expand Down Expand Up @@ -98,7 +99,7 @@ defmodule AeMdw.Node do
|> Enum.into(%{}, fn {k, v} -> {Contract.function_hash(k), v} end)
end

@spec aexn_event_hash_types() :: %{event_hash() => aexn_event_type()}
@spec aexn_event_hash_types() :: %{Contracts.event_hash() => aexn_event_type()}
def aexn_event_hash_types() do
%{
:aec_hash.blake2b_256_hash("Burn") => :burn,
Expand All @@ -117,6 +118,14 @@ defmodule AeMdw.Node do
}
end

@spec aexn_event_names() :: %{Contracts.event_hash() => AexnContracts.event_name()}
def aexn_event_names() do
aexn_event_hash_types()
|> Enum.into(%{}, fn {hash, atom} ->
{hash, Macro.camelize("#{atom}")}
end)
end

@spec hdr_fields(:key | :micro) :: [atom()]
def hdr_fields(_arg) do
~w(height prev_hash)a
Expand Down

0 comments on commit bc54cd6

Please sign in to comment.