11package io .api .core .impl ;
22
3- import com .sun .istack .internal .NotNull ;
43import io .api .core .IAccountProvider ;
54import 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 .*;
128import io .api .util .BasicUtils ;
9+ import org .jetbrains .annotations .NotNull ;
1310
11+ import java .util .ArrayList ;
1412import java .util .Collections ;
1513import java .util .List ;
1614import java .util .Map ;
@@ -45,9 +43,10 @@ public class AccountProvider extends BasicProvider implements IAccountProvider {
4543private static final String OFFSET_PARAM ="&offset=" ;
4644private static final String PAGE_PARAM ="&page=" ;
4745
48- public AccountProvider (final String baseUrl ,
46+ public AccountProvider (final IQueueManager queueManager ,
47+ final String baseUrl ,
4948final Map <String ,String >header ) {
50- super ("account" ,baseUrl ,header );
49+ super (queueManager , "account" ,baseUrl ,header );
5150 }
5251
5352@ NotNull
@@ -56,28 +55,38 @@ public Balance balance(final String address) {
5655BasicUtils .validateAddress (address );
5756
5857final 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 );
6159if (converted .getStatus () !=1 )
62- throw new EtherScanException (converted .getMessage () +" with status " +converted .getStatus ());
60+ throw new EtherScanException (converted .getMessage () +", with status " +converted .getStatus ());
6361
6462return new Balance (address ,Long .valueOf (converted .getResult ()));
6563 }
6664
6765@ NotNull
6866@ Override
6967public List <Balance >balances (final List <String >addresses ) {
70- BasicUtils .validateAddresses (addresses );
71- if (addresses .isEmpty ())
68+ if (BasicUtils .isEmpty (addresses ))
7269return Collections .emptyList ();
7370
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 );
7976
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 ;
8190 }
8291
8392private String toAddressParam (final List <String >addresses ) {
@@ -88,8 +97,7 @@ private String toAddressParam(final List<String> addresses) {
8897@ Override
8998public List <Tx >txs (final String address ) {
9099//TODO all txs implementations with pagination
91-
92- return txs (address ,MIN_START_BLOCK ,MAX_END_BLOCK );
100+ return txs (address ,MIN_START_BLOCK );
93101 }
94102
95103@ NotNull
@@ -105,8 +113,7 @@ public List<Tx> txs(final String address, final long startBlock, final long endB
105113
106114final String blockParam =START_BLOCK_PARAM +startBlock +END_BLOCK_PARAM +endBlock ;
107115final 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 );
110117if (converted .getStatus () !=1 )
111118throw new EtherScanException (converted .getMessage () +" with status " +converted .getStatus ());
112119
@@ -117,49 +124,89 @@ public List<Tx> txs(final String address, final long startBlock, final long endB
117124
118125@ NotNull
119126@ 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 );
122130 }
123131
124132@ NotNull
125133@ Override
126- public List <Tx >txsInternal (final String address ,final long startBlock ) {
134+ public List <TxInternal >txsInternal (final String address ,final long startBlock ) {
127135return txsInternal (address ,startBlock ,MAX_END_BLOCK );
128136 }
129137
130138@ NotNull
131139@ 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 ();
134152 }
135153
136154@ NotNull
137155@ 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 ();
140167 }
141168
142169@ NotNull
143170@ 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 );
146174 }
147175
148176@ NotNull
149177@ Override
150- public List <Tx >txsToken (final String address ,final long startBlock ) {
178+ public List <TxToken >txsToken (final String address ,final long startBlock ) {
151179return txsToken (address ,startBlock ,MAX_END_BLOCK );
152180 }
153181
154182@ NotNull
155183@ 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 ();
158196 }
159197
160198@ NotNull
161199@ Override
162200public 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 ();
164211 }
165212}