Skip to content

Commit 27d1901

Browse files
author
Valentin Atanasov
authored
feat: add dex action field (#1885)
1 parent bb44c54 commit 27d1901

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

config/config.exs

+11
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,15 @@ config :ae_mdw, :dex_factories, %{
8686
107, 208, 117, 185, 160, 135, 239, 247, 134, 40, 7, 80>>
8787
}
8888

89+
config :ae_mdw, :ae_token, %{
90+
# "ct_J3zBY8xxjsRr3QojETNw48Eb38fjvEuJKkQ6KzECvubvEcvCa"
91+
"ae_mainnet" =>
92+
<<38, 183, 172, 84, 94, 51, 242, 135, 60, 130, 44, 59, 52, 52, 110, 37, 13, 210, 14, 54, 144,
93+
24, 53, 177, 117, 211, 247, 18, 109, 117, 189, 41>>,
94+
# "ct_JDp175ruWd7mQggeHewSLS1PFXt9AzThCDaFedxon8mF8xTRF"
95+
"ae_uat" =>
96+
<<39, 26, 34, 124, 164, 250, 243, 90, 198, 12, 74, 70, 137, 147, 70, 150, 174, 68, 138, 188,
97+
64, 12, 26, 227, 206, 15, 221, 211, 50, 4, 47, 82>>
98+
}
99+
89100
import_config "#{Mix.env()}.exs"

docs/swagger_v3/dex.spec.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ schemas:
2323
amount1_out:
2424
type: integer
2525
example: 1000080
26+
action:
27+
type: string
28+
enum:
29+
- "SWAP"
30+
- "BUY"
31+
- "SELL"
32+
example: "SWAP"
2633
caller:
2734
type: string
2835
example: "ak_2t7TnocFw7oCYSS7g2yGutZMpGEJta6dq2DTX38SmuqmwtN6Ch"
@@ -67,6 +74,7 @@ schemas:
6774
example: 123456
6875
required:
6976
- amounts
77+
- action
7078
- caller
7179
- from_token
7280
- to_token

lib/ae_mdw/dex.ex

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ defmodule AeMdw.Dex do
2323
@contract_swaps_table Model.DexContractSwapTokens
2424
@swaps_table Model.DexSwapTokens
2525

26+
@ae_token_contract_pks Application.compile_env(:ae_mdw, :ae_token)
27+
2628
@typep encoded_pubkey() :: Encoding.encoded_hash()
2729
@typep amount() :: non_neg_integer()
2830
@typep swap() :: %{
@@ -241,6 +243,7 @@ defmodule AeMdw.Dex do
241243
} = rendered_amounts = render_amounts(amounts)
242244

243245
%{
246+
action: action(token1_pk, token2_pk),
244247
caller: Encoding.encode(:account_pubkey, caller_pk),
245248
to_account: Encoding.encode(:account_pubkey, to_pk),
246249
from_contract: Encoding.encode_contract(token1_pk),
@@ -259,6 +262,16 @@ defmodule AeMdw.Dex do
259262
}
260263
end
261264

265+
defp action(t1, t2) do
266+
ae_token = Map.get(@ae_token_contract_pks, :aec_governance.get_network_id())
267+
268+
case {t1, t2} do
269+
{^ae_token, _t2} -> "BUY"
270+
{_t1, ^ae_token} -> "SELL"
271+
{_t1, _t2} -> "SWAP"
272+
end
273+
end
274+
262275
defp convert_param({"token_symbol", token_symbol}) when is_binary(token_symbol) do
263276
case DexCache.get_token_pair_txi(token_symbol) do
264277
:not_found -> {:error, ErrInput.NotAex9.exception(value: token_symbol)}

test/ae_mdw_web/controllers/dex_controller_test.exs

+3-1
Original file line numberDiff line numberDiff line change
@@ -807,14 +807,16 @@ defmodule AeMdwWeb.DexControllerTest do
807807
"to_token" => token2_symbol,
808808
"tx_hash" => tx_hash,
809809
"log_idx" => log_idx,
810-
"amounts" => amounts
810+
"amounts" => amounts,
811+
"action" => action
811812
},
812813
exp_token1,
813814
exp_token2
814815
) do
815816
<<txi::256>> = Validate.id!(tx_hash)
816817

817818
caller_id in @accounts and token1_symbol == exp_token1 and
819+
action in ["SELL", "BUY", "SWAP"] and
818820
token2_symbol == exp_token2 and txi in 1_000_001..1_000_080 and log_idx == rem(txi, 2) and
819821
amounts == %{
820822
"amount0_in" => txi + 10,

0 commit comments

Comments
 (0)