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: adjust aex9 balance history endpoint #1997

Merged
merged 2 commits into from
Nov 4, 2024
Merged
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
62 changes: 45 additions & 17 deletions lib/ae_mdw/aex9.ex
Original file line number Diff line number Diff line change
Expand Up @@ -284,25 +284,54 @@ defmodule AeMdw.Aex9 do
nil -> {0, DbUtil.last_gen!(state)}
end

streamer = fn
:forward when first_gen <= cursor and cursor <= last_gen -> cursor..last_gen
:backward when cursor == nil -> last_gen..first_gen
:backward when first_gen <= cursor and cursor <= last_gen -> cursor..first_gen
_dir -> first_gen..last_gen
end
streamer =
balance_history_streamer(%{
account_pk: account_pk,
contract_pk: contract_pk,
range: {first_gen, last_gen},
cursor: cursor
})

paginated_history =
Collection.paginate(
streamer,
pagination,
&render_balance_history_item(contract_pk, account_pk, &1),
& &1
&render_balance_history_item/1,
&serialize_history_cursor/1
)

{:ok, paginated_history}
end
end

defp balance_history_streamer(%{
account_pk: account_pk,
contract_pk: contract_pk,
range: {first_gen, last_gen},
cursor: cursor
}) do
fn direction ->
direction
|> case do
:forward when first_gen <= cursor and cursor <= last_gen -> cursor..last_gen
:backward when cursor == nil -> last_gen..first_gen
:backward when first_gen <= cursor and cursor <= last_gen -> cursor..first_gen
_dir -> first_gen..last_gen
end
|> Stream.flat_map(fn gen ->
type_height_hash = {:key, gen, DbUtil.height_hash(gen)}

case Db.aex9_balance(contract_pk, account_pk, type_height_hash) do
{:ok, {amount_or_nil, _height_hash}} ->
[{contract_pk, account_pk, gen, amount_or_nil}]

_invalid ->
[]
end
end)
end
end

defp serialize_account_balance_cursor(cursor),
do: cursor |> :erlang.term_to_binary() |> Base.encode64(padding: false)

Expand Down Expand Up @@ -335,15 +364,10 @@ defmodule AeMdw.Aex9 do
}
end

defp render_balance_history_item(contract_pk, account_pk, gen) do
type_height_hash = {:key, gen, DbUtil.height_hash(gen)}

{:ok, {amount_or_nil, _height_hash}} =
Db.aex9_balance(contract_pk, account_pk, type_height_hash)

balance = render_balance(contract_pk, {:address, account_pk}, amount_or_nil)

Map.put(balance, :height, gen)
defp render_balance_history_item({contract_pk, account_pk, gen, amount_or_nil}) do
contract_pk
|> render_balance({:address, account_pk}, amount_or_nil)
|> Map.put(:height, gen)
end

defp render_account_balance(state, type_height_hash, {account_pk, contract_pk}) do
Expand Down Expand Up @@ -385,6 +409,10 @@ defmodule AeMdw.Aex9 do
end
end

defp serialize_history_cursor({_contract_pk, _account_pk, gen, _amount_or_nil}) do
gen
end

defp serialize_event_balances_cursor({_contract_pk, account_pk}), do: encode_account(account_pk)

defp deserialize_event_balances_cursor(nil), do: {:ok, nil}
Expand Down
Loading