1
1
package io .api .core .impl ;
2
2
3
- import com .sun .istack .internal .NotNull ;
4
3
import io .api .core .IAccountProvider ;
5
4
import io .api .error .EtherScanException ;
6
- import io .api .model .Balance ;
7
- import io .api .model .Block ;
8
- import io .api .model .Tx ;
9
- import io .api .model .temporary .BalanceResponseTO ;
10
- import io .api .model .temporary .StringResponseTO ;
11
- import io .api .model .temporary .TxResponseTO ;
5
+ import io .api .manager .IQueueManager ;
6
+ import io .api .model .*;
7
+ import io .api .model .temporary .*;
12
8
import io .api .util .BasicUtils ;
9
+ import org .jetbrains .annotations .NotNull ;
13
10
11
+ import java .util .ArrayList ;
14
12
import java .util .Collections ;
15
13
import java .util .List ;
16
14
import java .util .Map ;
@@ -45,9 +43,10 @@ public class AccountProvider extends BasicProvider implements IAccountProvider {
45
43
private static final String OFFSET_PARAM = "&offset=" ;
46
44
private static final String PAGE_PARAM = "&page=" ;
47
45
48
- public AccountProvider (final String baseUrl ,
46
+ public AccountProvider (final IQueueManager queueManager ,
47
+ final String baseUrl ,
49
48
final Map <String , String > header ) {
50
- super ("account" , baseUrl , header );
49
+ super (queueManager , "account" , baseUrl , header );
51
50
}
52
51
53
52
@ NotNull
@@ -56,28 +55,38 @@ public Balance balance(final String address) {
56
55
BasicUtils .validateAddress (address );
57
56
58
57
final String urlParams = BALANCE_ACTION + TAG_LATEST_PARAM + ADDRESS_PARAM + address ;
59
- final String response = getRequest (urlParams );
60
- final StringResponseTO converted = convert (response , StringResponseTO .class );
58
+ final StringResponseTO converted = getRequest (urlParams , StringResponseTO .class );
61
59
if (converted .getStatus () != 1 )
62
- throw new EtherScanException (converted .getMessage () + " with status " + converted .getStatus ());
60
+ throw new EtherScanException (converted .getMessage () + ", with status " + converted .getStatus ());
63
61
64
62
return new Balance (address , Long .valueOf (converted .getResult ()));
65
63
}
66
64
67
65
@ NotNull
68
66
@ Override
69
67
public List <Balance > balances (final List <String > addresses ) {
70
- BasicUtils .validateAddresses (addresses );
71
- if (addresses .isEmpty ())
68
+ if (BasicUtils .isEmpty (addresses ))
72
69
return Collections .emptyList ();
73
70
74
- final String urlParams = BALANCE_MULTI_ACTION + TAG_LATEST_PARAM + ADDRESS_PARAM + toAddressParam (addresses );
75
- final String response = getRequest ( urlParams );
76
- final BalanceResponseTO converted = convert ( response , BalanceResponseTO . class );
77
- if ( converted . getStatus () != 1 )
78
- throw new EtherScanException ( converted . getMessage () + " with status " + converted . getStatus () );
71
+ BasicUtils . validateAddresses (addresses );
72
+
73
+ // Maximum addresses in batch request - 20
74
+ final List < Balance > balances = new ArrayList <>();
75
+ final List < List < String >> addressesAsBatches = BasicUtils . partition ( addresses , 20 );
79
76
80
- return converted .getBalances ().stream ().map (Balance ::of ).collect (Collectors .toList ());
77
+ for (final List <String > batch : addressesAsBatches ) {
78
+ final String urlParams = BALANCE_MULTI_ACTION + TAG_LATEST_PARAM + ADDRESS_PARAM + toAddressParam (batch );
79
+ final BalanceResponseTO converted = getRequest (urlParams , BalanceResponseTO .class );
80
+ if (converted .getStatus () != 1 )
81
+ throw new EtherScanException (converted .getMessage () + ", with status " + converted .getStatus ());
82
+
83
+ if (!BasicUtils .isEmpty (converted .getBalances ()))
84
+ balances .addAll (converted .getBalances ().stream ()
85
+ .map (Balance ::of )
86
+ .collect (Collectors .toList ()));
87
+ }
88
+
89
+ return balances ;
81
90
}
82
91
83
92
private String toAddressParam (final List <String > addresses ) {
@@ -88,8 +97,7 @@ private String toAddressParam(final List<String> addresses) {
88
97
@ Override
89
98
public List <Tx > txs (final String address ) {
90
99
//TODO all txs implementations with pagination
91
-
92
- return txs (address , MIN_START_BLOCK , MAX_END_BLOCK );
100
+ return txs (address , MIN_START_BLOCK );
93
101
}
94
102
95
103
@ NotNull
@@ -105,8 +113,7 @@ public List<Tx> txs(final String address, final long startBlock, final long endB
105
113
106
114
final String blockParam = START_BLOCK_PARAM + startBlock + END_BLOCK_PARAM + endBlock ;
107
115
final String urlParams = TX_ACTION + ADDRESS_PARAM + address + blockParam + SORT_ASC_PARAM ;
108
- final String response = getRequest (urlParams );
109
- final TxResponseTO converted = convert (response , TxResponseTO .class );
116
+ final TxResponseTO converted = getRequest (urlParams , TxResponseTO .class );
110
117
if (converted .getStatus () != 1 )
111
118
throw new EtherScanException (converted .getMessage () + " with status " + converted .getStatus ());
112
119
@@ -117,49 +124,89 @@ public List<Tx> txs(final String address, final long startBlock, final long endB
117
124
118
125
@ NotNull
119
126
@ Override
120
- public List <Tx > txsInternal (final String address ) {
121
- return null ;
127
+ public List <TxInternal > txsInternal (final String address ) {
128
+ //TODO all txs implementations with pagination
129
+ return txsInternal (address , MIN_START_BLOCK );
122
130
}
123
131
124
132
@ NotNull
125
133
@ Override
126
- public List <Tx > txsInternal (final String address , final long startBlock ) {
134
+ public List <TxInternal > txsInternal (final String address , final long startBlock ) {
127
135
return txsInternal (address , startBlock , MAX_END_BLOCK );
128
136
}
129
137
130
138
@ NotNull
131
139
@ Override
132
- public List <Tx > txsInternal (final String address , final long startBlock , final long endBlock ) {
133
- return null ;
140
+ public List <TxInternal > txsInternal (final String address , final long startBlock , final long endBlock ) {
141
+ BasicUtils .validateAddress (address );
142
+
143
+ final String blockParam = START_BLOCK_PARAM + startBlock + END_BLOCK_PARAM + endBlock ;
144
+ final String urlParams = TX_INTERNAL_ACTION + ADDRESS_PARAM + address + blockParam + SORT_ASC_PARAM ;
145
+ final TxInternalResponseTO converted = getRequest (urlParams , TxInternalResponseTO .class );
146
+ if (converted .getStatus () != 1 )
147
+ throw new EtherScanException (converted .getMessage () + " with status " + converted .getStatus ());
148
+
149
+ return (converted .getResult () == null )
150
+ ? Collections .emptyList ()
151
+ : converted .getResult ();
134
152
}
135
153
136
154
@ NotNull
137
155
@ Override
138
- public List <Tx > txsInternalByHash (String txhash ) {
139
- return null ;
156
+ public List <TxInternal > txsInternalByHash (final String txhash ) {
157
+ BasicUtils .validateTxHash (txhash );
158
+
159
+ final String urlParams = TX_INTERNAL_ACTION + TXHASH_PARAM + txhash ;
160
+ final TxInternalResponseTO converted = getRequest (urlParams , TxInternalResponseTO .class );
161
+ if (converted .getStatus () != 1 )
162
+ throw new EtherScanException (converted .getMessage () + " with status " + converted .getStatus ());
163
+
164
+ return (converted .getResult () == null )
165
+ ? Collections .emptyList ()
166
+ : converted .getResult ();
140
167
}
141
168
142
169
@ NotNull
143
170
@ Override
144
- public List <Tx > txsToken (final String address ) {
145
- return null ;
171
+ public List <TxToken > txsToken (final String address ) {
172
+ //TODO all txs implementations with pagination
173
+ return txsToken (address , MIN_START_BLOCK );
146
174
}
147
175
148
176
@ NotNull
149
177
@ Override
150
- public List <Tx > txsToken (final String address , final long startBlock ) {
178
+ public List <TxToken > txsToken (final String address , final long startBlock ) {
151
179
return txsToken (address , startBlock , MAX_END_BLOCK );
152
180
}
153
181
154
182
@ NotNull
155
183
@ Override
156
- public List <Tx > txsToken (final String address , final long startBlock , final long endBlock ) {
157
- return null ;
184
+ public List <TxToken > txsToken (final String address , final long startBlock , final long endBlock ) {
185
+ BasicUtils .validateAddress (address );
186
+
187
+ final String blockParam = START_BLOCK_PARAM + startBlock + END_BLOCK_PARAM + endBlock ;
188
+ final String urlParams = TX_TOKEN_ACTION + ADDRESS_PARAM + address + blockParam + SORT_ASC_PARAM ;
189
+ final TxTokenResponseTO converted = getRequest (urlParams , TxTokenResponseTO .class );
190
+ if (converted .getStatus () != 1 )
191
+ throw new EtherScanException (converted .getMessage () + " with status " + converted .getStatus ());
192
+
193
+ return (converted .getResult () == null )
194
+ ? Collections .emptyList ()
195
+ : converted .getResult ();
158
196
}
159
197
160
198
@ NotNull
161
199
@ Override
162
200
public List <Block > minedBlocks (final String address ) {
163
- return null ;
201
+ BasicUtils .validateAddress (address );
202
+
203
+ final String urlParams = MINED_ACTION + BLOCK_TYPE_PARAM + ADDRESS_PARAM + address ;
204
+ final BlockResponseTO converted = getRequest (urlParams , BlockResponseTO .class );
205
+ if (converted .getStatus () != 1 )
206
+ throw new EtherScanException (converted .getMessage () + " with status " + converted .getStatus ());
207
+
208
+ return (converted .getResult () == null )
209
+ ? Collections .emptyList ()
210
+ : converted .getResult ();
164
211
}
165
212
}
0 commit comments