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

feat: additional fields to dex endpoints #1835

Merged
merged 5 commits into from
Jul 9, 2024
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
32 changes: 32 additions & 0 deletions docs/swagger_v3/dex.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ schemas:
tx_hash:
type: string
example: "th_2t7TnocFw7oCYSS7g2yGutZMpGEJta6dq2DTX38SmuqmwtN6Ch"
from_contract:
type: string
example: "ct_22NTeTHfqVXLChCMCy3eAAj3hGW2nuNUwHhQ1zRX3k4iZKq8Ru"
to_contract:
type: string
example: "ct_22NTeTHfqVXLChCMCy3eAAj3hGW2nuNUwHhQ1zRX3k4iZKq8Ru"
from_amount:
type: integer
example: 1000050
to_amount:
type: integer
example: 1000060
from_decimals:
type: integer
example: 18
to_decimals:
type: integer
example: 18
microtime:
type: integer
example: 1629820800000
height:
type: integer
example: 123456
required:
- amounts
- caller
Expand All @@ -44,6 +68,14 @@ schemas:
- log_idx
- to_account
- tx_hash
- from_contract
- to_contract
- from_amount
- to_amount
- from_decimals
- to_decimals
- microtime
- height
paths:
/dex/swaps:
get:
Expand Down
44 changes: 39 additions & 5 deletions lib/ae_mdw/dex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule AeMdw.Dex do
Search for DEX swaps.
"""

alias AeMdw.AexnContracts
alias AeMdw.Util.Encoding
alias AeMdw.Collection
alias AeMdw.Contracts
Expand Down Expand Up @@ -206,16 +207,43 @@ defmodule AeMdw.Dex do
Origin.tx_index!(state, {:contract, contract_pk})
end

Model.tx(id: _tx_hash, block_index: {height, _mbi} = block_index, time: time) =
State.fetch!(state, Model.Tx, create_txi)

Model.block(hash: hash) = State.fetch!(state, Model.Block, block_index)
contract_pk = Origin.pubkey!(state, {:contract, create_txi})
%{token1: token1_pk, token2: token2_pk} = DexCache.get_pair(contract_pk)

{:ok, {_name, _symbol, from_decimals}} = AexnContracts.call_meta_info(:aex9, token1_pk, hash)
{:ok, {_name, _symbol, to_decimals}} = AexnContracts.call_meta_info(:aex9, token2_pk, hash)

%{token1: token1_symbol, token2: token2_symbol} = DexCache.get_pair_symbols(create_txi)

%{token1: token1_pk, token2: token2_pk} = DexCache.get_pair(contract_pk)

%{
amount0_in: amount0_in,
amount1_in: amount1_in,
amount0_out: amount0_out,
amount1_out: amount1_out
} = rendered_amounts = render_amounts(amounts)

%{
caller: Encoding.encode(:account_pubkey, caller_pk),
to_account: Encoding.encode(:account_pubkey, to_pk),
from_contract: Encoding.encode_contract(token1_pk),
to_contract: Encoding.encode_contract(token2_pk),
from_token: token1_symbol,
to_token: token2_symbol,
from_amount: amount0_in + amount1_in,
to_amount: amount0_out + amount1_out,
from_decimals: from_decimals,
to_decimals: to_decimals,
tx_hash: Encoding.encode(:tx_hash, Txs.txi_to_hash(state, txi)),
log_idx: log_idx,
amounts: render_amounts(amounts)
amounts: rendered_amounts,
microtime: time,
height: height
}
end

Expand All @@ -228,12 +256,18 @@ defmodule AeMdw.Dex do

defp convert_param(other_param), do: {:error, ErrInput.Query.exception(value: other_param)}

@spec render_amounts(list(integer())) :: %{
amount0_in: integer(),
amount1_in: integer(),
amount0_out: integer(),
amount1_out: integer()
}
defp render_amounts([amount0_in, amount1_in, amount0_out, amount1_out]) do
%{
"amount0_in" => amount0_in,
"amount1_in" => amount1_in,
"amount0_out" => amount0_out,
"amount1_out" => amount1_out
amount0_in: amount0_in,
amount1_in: amount1_in,
amount0_out: amount0_out,
amount1_out: amount1_out
}
end

Expand Down
Loading
Loading