@@ -3,9 +3,11 @@ defmodule AeMdwWeb.Aex9ControllerTest do
3
3
4
4
alias AeMdw.Database
5
5
alias AeMdw.Db.Model
6
+ alias AeMdw.Db.Store
6
7
alias AeMdw.Validate
7
8
8
- import AeMdwWeb.Helpers.AexnHelper , only: [ enc_ct: 1 ]
9
+ import AeMdwWeb.Helpers.AexnHelper , only: [ enc_ct: 1 , enc_id: 1 , enc_block: 2 ]
10
+ import Mock
9
11
10
12
require Model
11
13
@@ -27,7 +29,7 @@ defmodule AeMdwWeb.Aex9ControllerTest do
27
29
end
28
30
29
31
describe "by_name" do
30
- test "it gets aex9 tokens by name" , % { conn: conn } do
32
+ test "gets aex9 tokens by name" , % { conn: conn } do
31
33
assert aex9_tokens = conn |> get ( "/aex9/by_name" ) |> json_response ( 200 )
32
34
assert length ( aex9_tokens ) > 0
33
35
@@ -48,7 +50,7 @@ defmodule AeMdwWeb.Aex9ControllerTest do
48
50
end )
49
51
end
50
52
51
- test "it gets aex9 tokens filtered by name prefix" , % { conn: conn } do
53
+ test "gets aex9 tokens filtered by name prefix" , % { conn: conn } do
52
54
prefix = "some-AEX"
53
55
54
56
assert aex9_tokens = conn |> get ( "/aex9/by_name" , prefix: prefix ) |> json_response ( 200 )
@@ -63,7 +65,7 @@ defmodule AeMdwWeb.Aex9ControllerTest do
63
65
end
64
66
65
67
describe "by_symbol" do
66
- test "it gets aex9 tokens by symbol" , % { conn: conn } do
68
+ test "gets aex9 tokens by symbol" , % { conn: conn } do
67
69
assert aex9_tokens = conn |> get ( "/aex9/by_symbol" ) |> json_response ( 200 )
68
70
69
71
aex9_symbols = aex9_tokens |> Enum . map ( fn % { "symbol" => symbol } -> symbol end )
@@ -83,7 +85,7 @@ defmodule AeMdwWeb.Aex9ControllerTest do
83
85
end )
84
86
end
85
87
86
- test "it gets aex9 tokens filtered by symbol prefix" , % { conn: conn } do
88
+ test "gets aex9 tokens filtered by symbol prefix" , % { conn: conn } do
87
89
prefix = "SAEX9"
88
90
89
91
assert aex9_tokens = conn |> get ( "/aex9/by_symbol" , prefix: prefix ) |> json_response ( 200 )
@@ -96,9 +98,95 @@ defmodule AeMdwWeb.Aex9ControllerTest do
96
98
end
97
99
98
100
describe "by_contract" do
99
- test "it returns an aex9 token" , % { conn: conn } do
101
+ test "returns an aex9 token" , % { conn: conn } do
100
102
assert % { "data" => % { "contract_id" => @ aex9_token_id } } =
101
103
conn |> get ( "/aex9/by_contract/#{ @ aex9_token_id } " ) |> json_response ( 200 )
102
104
end
103
105
end
106
+
107
+ describe "balances" do
108
+ test "returns all account balances" , % { conn: conn , store: store } do
109
+ account_pk = :crypto . strong_rand_bytes ( 32 )
110
+
111
+ expected_balances =
112
+ for i <- 1 .. 10 do
113
+ txi = Enum . random ( 1_000_000 .. 10_000_000 )
114
+
115
+ % {
116
+ "block_hash" => enc_block ( :micro , :crypto . strong_rand_bytes ( 32 ) ) ,
117
+ "amount" => Enum . random ( 100_000_000 .. 999_000_000 ) ,
118
+ "contract_id" => enc_ct ( :crypto . strong_rand_bytes ( 32 ) ) ,
119
+ "height" => div ( txi , 1_000 ) ,
120
+ "token_name" => "name#{ i } " ,
121
+ "token_symbol" => "symbol#{ i } " ,
122
+ "tx_hash" => :aeser_api_encoder . encode ( :tx_hash , :crypto . strong_rand_bytes ( 32 ) ) ,
123
+ "tx_index" => txi ,
124
+ "tx_type" => "contract_call_tx"
125
+ }
126
+ end
127
+
128
+ store =
129
+ Enum . reduce ( expected_balances , store , fn % {
130
+ "amount" => amount ,
131
+ "contract_id" => contract_id ,
132
+ "height" => height ,
133
+ "token_name" => token_name ,
134
+ "token_symbol" => token_symbol ,
135
+ "tx_hash" => tx_hash ,
136
+ "tx_index" => txi
137
+ } ,
138
+ store_acc ->
139
+ contract_pk = Validate . id! ( contract_id )
140
+ tx_hash = Validate . id! ( tx_hash )
141
+
142
+ m_contract =
143
+ Model . aexn_contract (
144
+ index: { :aex9 , contract_pk } ,
145
+ meta_info: { token_name , token_symbol , 18 }
146
+ )
147
+
148
+ m_balance =
149
+ Model . aex9_balance (
150
+ index: { contract_pk , account_pk } ,
151
+ block_index: { height , 0 } ,
152
+ txi: txi ,
153
+ amount: amount
154
+ )
155
+
156
+ m_presence = Model . aex9_account_presence ( index: { account_pk , contract_pk } , txi: txi )
157
+
158
+ store_acc
159
+ |> Store . put ( Model.Tx , Model . tx ( index: txi , id: tx_hash , block_index: { height , 0 } ) )
160
+ |> Store . put ( Model.AexnContract , m_contract )
161
+ |> Store . put ( Model.Aex9Balance , m_balance )
162
+ |> Store . put ( Model.Aex9AccountPresence , m_presence )
163
+ end )
164
+
165
+ with_mocks [
166
+ {
167
+ AeMdw.Node.Db ,
168
+ [ ] ,
169
+ [
170
+ get_tx_data: fn tx_hash_bin ->
171
+ % { "block_hash" => block_hash } =
172
+ Enum . find ( expected_balances , fn % { "tx_hash" => tx_hash } ->
173
+ tx_hash_bin == Validate . id! ( tx_hash )
174
+ end )
175
+
176
+ { Validate . id! ( block_hash ) , :contract_call_tx , nil , nil }
177
+ end
178
+ ]
179
+ }
180
+ ] do
181
+ assert balances_data =
182
+ conn
183
+ |> with_store ( store )
184
+ |> get ( "/aex9/balances/account/#{ enc_id ( account_pk ) } " )
185
+ |> json_response ( 200 )
186
+
187
+ assert ^ balances_data = Enum . sort_by ( balances_data , & & 1 [ "tx_index" ] , :desc )
188
+ assert MapSet . new ( balances_data ) == MapSet . new ( expected_balances )
189
+ end
190
+ end
191
+ end
104
192
end
0 commit comments