Skip to content

Commit

Permalink
chore: add allowance and approval events
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeshe committed Sep 5, 2023
1 parent dc0aecc commit 15558c6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 35 deletions.
44 changes: 28 additions & 16 deletions lib/ae_mdw/node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ defmodule AeMdw.Node do
@opaque aect_call :: tuple()

@type aexn_event_type ::
:burn
:allowance
| :approval
| :approval_for_all
| :burn
| :mint
| :swap
| :edition_limit
Expand Down Expand Up @@ -84,21 +87,30 @@ defmodule AeMdw.Node do

@spec aexn_event_hash_types() :: %{Contracts.event_hash() => aexn_event_type()}
def aexn_event_hash_types() do
%{
:aec_hash.blake2b_256_hash("Burn") => :burn,
:aec_hash.blake2b_256_hash("Mint") => :mint,
:aec_hash.blake2b_256_hash("Swap") => :swap,
:aec_hash.blake2b_256_hash("EditionLimit") => :edition_limit,
:aec_hash.blake2b_256_hash("EditionLimitDecrease") => :edition_limit_decrease,
:aec_hash.blake2b_256_hash("TemplateCreation") => :template_creation,
:aec_hash.blake2b_256_hash("TemplateDeletion") => :template_deletion,
:aec_hash.blake2b_256_hash("TemplateMint") => :template_mint,
:aec_hash.blake2b_256_hash("TemplateLimit") => :template_limit,
:aec_hash.blake2b_256_hash("TemplateLimitDecrease") => :template_limit_decrease,
:aec_hash.blake2b_256_hash("TokenLimit") => :token_limit,
:aec_hash.blake2b_256_hash("TokenLimitDecrease") => :token_limit_decrease,
:aec_hash.blake2b_256_hash("Transfer") => :transfer
}
Map.new(
[
:allowance,
:approval,
:approval_for_all,
:burn,
:mint,
:swap,
:edition_limit,
:edition_limit_decrease,
:template_creation,
:template_deletion,
:template_mint,
:template_limit,
:template_limit_decrease,
:token_limit,
:token_limit_decrease,
:transfer
],
fn event_type ->
event_name = event_type |> to_string() |> Macro.camelize()
{:aec_hash.blake2b_256_hash(event_name), event_type}
end
)
end

@spec aexn_event_names() :: %{Contracts.event_hash() => AexnContracts.event_name()}
Expand Down
14 changes: 14 additions & 0 deletions lib/ae_mdw_web/views/logs_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ defmodule AeMdwWeb.LogsView do
if String.valid?(data), do: data, else: Base.encode64(data)
end

defp format_args("Allowance", [account1, account2, <<amount::256>>], %{aexn_args: true}) do
[encode_account(account1), encode_account(account2), amount]
end

defp format_args("Approval", [account1, account2, <<token_id::256>>, enable], %{aexn_args: true})
when enable in ["true", "false"] do
[encode_account(account1), encode_account(account2), token_id, enable]
end

defp format_args("ApprovalForAll", [account1, account2, enable], %{aexn_args: true})
when enable in ["true", "false"] do
[encode_account(account1), encode_account(account2), enable]
end

defp format_args(event_name, [account, <<token_id::256>>], %{aexn_args: true})
when event_name in ["Burn", "Mint", "Swap"] do
[encode_account(account), token_id]
Expand Down
6 changes: 6 additions & 0 deletions test/ae_mdw_web/views/logs_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ defmodule AeMdwWeb.LogsViewTest do
account2_pk = :crypto.strong_rand_bytes(32)

tokens = %{
"Allowance" => Enum.random(100..999),
"Approval" => Enum.random(100..999),
"ApprovalForAll" => Enum.random(100..999),
"Burn" => Enum.random(100..999),
"Mint" => Enum.random(100..999),
"Swap" => Enum.random(100..999),
Expand All @@ -37,6 +40,7 @@ defmodule AeMdwWeb.LogsViewTest do
}

aex9_event_args = %{
allowance: [account1_pk, account2_pk, <<tokens["Allowance"]::256>>],
burn: [account1_pk, <<tokens["Burn"]::256>>],
mint: [account1_pk, <<tokens["Mint"]::256>>],
swap: [account1_pk, <<tokens["Swap"]::256>>],
Expand All @@ -57,6 +61,8 @@ defmodule AeMdwWeb.LogsViewTest do
}

aex141_event_args = %{
approval: [account1_pk, account2_pk, <<tokens["Approval"]::256>>, "true"],
approval_for_all: [account1_pk, account2_pk, "true"],
burn: [account1_pk, <<tokens["Burn"]::256>>],
mint: [account1_pk, <<tokens["Mint"]::256>>],
transfer: [account1_pk, account2_pk, <<tokens["Transfer"]::256>>],
Expand Down
44 changes: 25 additions & 19 deletions test/support/ae_mdw/node/aexn_event_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@ defmodule AeMdw.Node.AexnEventFixtures do
@type aexn_event_type :: AeMdw.Node.aexn_event_type()
@type event_hash :: AeMdw.Node.event_hash()

@spec aexn_event_hash(aexn_event_type()) :: [event_hash()]
def aexn_event_hash(:burn), do: :aec_hash.blake2b_256_hash("Burn")
def aexn_event_hash(:edition_limit), do: :aec_hash.blake2b_256_hash("EditionLimit")

def aexn_event_hash(:edition_limit_decrease),
do: :aec_hash.blake2b_256_hash("EditionLimitDecrease")

def aexn_event_hash(:mint), do: :aec_hash.blake2b_256_hash("Mint")
def aexn_event_hash(:swap), do: :aec_hash.blake2b_256_hash("Swap")
def aexn_event_hash(:transfer), do: :aec_hash.blake2b_256_hash("Transfer")
def aexn_event_hash(:template_creation), do: :aec_hash.blake2b_256_hash("TemplateCreation")
def aexn_event_hash(:template_deletion), do: :aec_hash.blake2b_256_hash("TemplateDeletion")
def aexn_event_hash(:template_mint), do: :aec_hash.blake2b_256_hash("TemplateMint")
def aexn_event_hash(:template_limit), do: :aec_hash.blake2b_256_hash("TemplateLimit")
@event_types [
:allowance,
:approval,
:approval_for_all,
:burn,
:mint,
:swap,
:edition_limit,
:edition_limit_decrease,
:template_creation,
:template_deletion,
:template_mint,
:template_limit,
:template_limit_decrease,
:token_limit,
:token_limit_decrease,
:transfer
]

def aexn_event_hash(:template_limit_decrease),
do: :aec_hash.blake2b_256_hash("TemplateLimitDecrease")

def aexn_event_hash(:token_limit), do: :aec_hash.blake2b_256_hash("TokenLimit")
def aexn_event_hash(:token_limit_decrease), do: :aec_hash.blake2b_256_hash("TokenLimitDecrease")
@spec aexn_event_hash(aexn_event_type()) :: [event_hash()]
def aexn_event_hash(event_type) when event_type in @event_types do
event_type
|> to_string()
|> Macro.camelize()
|> :aec_hash.blake2b_256_hash()
end
end

0 comments on commit 15558c6

Please sign in to comment.