22
33import com .sun .istack .internal .NotNull ;
44import io .api .core .IAccountProvider ;
5+ import io .api .error .EtherScanException ;
56import io .api .model .Balance ;
67import io .api .model .Block ;
7- import io .api .model .Transaction ;
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 ;
12+ import io .api .util .BasicUtils ;
813
14+ import java .util .Collections ;
915import java .util .List ;
1016import java .util .Map ;
17+ import java .util .stream .Collectors ;
1118
1219/**
1320 * ! NO DESCRIPTION !
@@ -20,12 +27,21 @@ public class AccountProvider extends BasicProvider implements IAccountProvider {
2027private static final int MAX_END_BLOCK =999999999 ;
2128private static final int MIN_START_BLOCK =0 ;
2229
30+ private static final String BALANCE_ACTION =ACTION_PARAM +"balance" ;
31+ private static final String BALANCE_MULTI_ACTION =ACTION_PARAM +"balancemulti" ;
32+ private static final String TX_ACTION =ACTION_PARAM +"txlist" ;
33+ private static final String TX_INTERNAL_ACTION =ACTION_PARAM +"txlistinternal" ;
34+ private static final String TX_TOKEN_ACTION =ACTION_PARAM +"tokentx" ;
35+ private static final String MINED_ACTION =ACTION_PARAM +"getminedblocks" ;
36+
2337private static final String BLOCK_TYPE_PARAM ="&blocktype=blocks" ;
24- private static final String START_BLOCK_PARAM ="&endblock=" ;
25- private static final String END_BLOCK_PARAM ="&startblock=" ;
38+ private static final String TAG_LATEST_PARAM ="&tag=latest" ;
39+ private static final String START_BLOCK_PARAM ="&startblock=" ;
40+ private static final String END_BLOCK_PARAM ="&endblock=" ;
2641private static final String SORT_DESC_PARAM ="&sort=desc" ;
2742private static final String SORT_ASC_PARAM ="&sort=asc" ;
2843private static final String ADDRESS_PARAM ="&address=" ;
44+ private static final String TXHASH_PARAM ="&txhash=" ;
2945private static final String OFFSET_PARAM ="&offset=" ;
3046private static final String PAGE_PARAM ="&page=" ;
3147
@@ -37,66 +53,107 @@ public AccountProvider(final String baseUrl,
3753@ NotNull
3854@ Override
3955public Balance balance (final String address ) {
40- return null ;
56+ BasicUtils .validateAddress (address );
57+
58+ 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 );
61+ if (converted .getStatus () !=1 )
62+ throw new EtherScanException (converted .getMessage () +" with status " +converted .getStatus ());
63+
64+ return new Balance (address ,Long .valueOf (converted .getResult ()));
4165 }
4266
4367@ NotNull
4468@ Override
4569public List <Balance >balances (final List <String >addresses ) {
46- return null ;
70+ BasicUtils .validateAddresses (addresses );
71+ if (addresses .isEmpty ())
72+ return Collections .emptyList ();
73+
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 ());
79+
80+ return converted .getBalances ().stream ().map (Balance ::of ).collect (Collectors .toList ());
81+ }
82+
83+ private String toAddressParam (final List <String >addresses ) {
84+ return addresses .stream ().collect (Collectors .joining ("," ));
4785 }
4886
4987@ NotNull
5088@ Override
51- public List <Transaction >txs (final String address ) {
52- return null ;
89+ public List <Tx >txs (final String address ) {
90+ //TODO all txs implementations with pagination
91+
92+ return txs (address ,MIN_START_BLOCK ,MAX_END_BLOCK );
5393 }
5494
5595@ NotNull
5696@ Override
57- public List <Transaction >txs (final String address ,final int startBlock ) {
97+ public List <Tx >txs (final String address ,final long startBlock ) {
5898return txs (address ,startBlock ,MAX_END_BLOCK );
5999 }
60100
61101@ NotNull
62102@ Override
63- public List <Transaction >txs (final String address ,final int startBlock ,final int endBlock ) {
64- return null ;
103+ public List <Tx >txs (final String address ,final long startBlock ,final long endBlock ) {
104+ BasicUtils .validateAddress (address );
105+
106+ final String blockParam =START_BLOCK_PARAM +startBlock +END_BLOCK_PARAM +endBlock ;
107+ 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 );
110+ if (converted .getStatus () !=1 )
111+ throw new EtherScanException (converted .getMessage () +" with status " +converted .getStatus ());
112+
113+ return (converted .getResult () ==null )
114+ ?Collections .emptyList ()
115+ :converted .getResult ();
65116 }
66117
67118@ NotNull
68119@ Override
69- public List <Transaction >txsInternal (final String address ) {
120+ public List <Tx >txsInternal (final String address ) {
70121return null ;
71122 }
72123
73124@ NotNull
74125@ Override
75- public List <Transaction >txsInternal (final String address ,final int startBlock ) {
126+ public List <Tx >txsInternal (final String address ,final long startBlock ) {
76127return txsInternal (address ,startBlock ,MAX_END_BLOCK );
77128 }
78129
79130@ NotNull
80131@ Override
81- public List <Transaction >txsInternal (final String address ,final int startBlock ,final int endBlock ) {
132+ public List <Tx >txsInternal (final String address ,final long startBlock ,final long endBlock ) {
133+ return null ;
134+ }
135+
136+ @ NotNull
137+ @ Override
138+ public List <Tx >txsInternalByHash (String txhash ) {
82139return null ;
83140 }
84141
85142@ NotNull
86143@ Override
87- public List <Transaction >txsToken (final String address ) {
144+ public List <Tx >txsToken (final String address ) {
88145return null ;
89146 }
90147
91148@ NotNull
92149@ Override
93- public List <Transaction >txsToken (final String address ,final int startBlock ) {
150+ public List <Tx >txsToken (final String address ,final long startBlock ) {
94151return txsToken (address ,startBlock ,MAX_END_BLOCK );
95152 }
96153
97154@ NotNull
98155@ Override
99- public List <Transaction >txsToken (final String address ,final int startBlock ,final int endBlock ) {
156+ public List <Tx >txsToken (final String address ,final long startBlock ,final long endBlock ) {
100157return null ;
101158 }
102159