Skip to content

Commit aee2694

Browse files
committed
API work in progress
1 parent ac39729 commit aee2694

File tree

2 files changed

+23
-44
lines changed

2 files changed

+23
-44
lines changed

api/controller.php

+19-44
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ethplorerController {
1919
protected $db;
2020
protected $command;
2121
protected $params = array();
22-
protected $apiCommands = array('getTxInfo', 'getTokenHistory', 'getAddressInfo', 'getTokenInfo', /*'getAddressHistory'*/);
22+
protected $apiCommands = array('getTxInfo', 'getTokenHistory', 'getAddressInfo', 'getTokenInfo', 'getAddressHistory');
2323
protected $defaults;
2424

2525
public function __construct($es){
@@ -224,67 +224,42 @@ public function getTxInfo(){
224224
}
225225

226226
public function getTokenHistory(){
227-
$result = array(
228-
'operations' => array()
229-
);
230-
$address = $this->getParam(0, FALSE);
231-
if($address){
232-
$address = strtolower($address);
233-
}
234-
if((FALSE !== $address) && (!$this->db->isValidAddress($address))){
235-
$this->sendError(104, 'Invalid address format');
236-
}
237-
$maxLimit = is_array($this->defaults) && isset($this->defaults['limit']) ? $this->defaults['limit'] : 10;
238-
$options = array(
239-
'address' => $this->getParam(0, FALSE),
240-
'type' => $this->getRequest('type', FALSE),
241-
'limit' => min(abs((int)$this->getRequest('limit', 10)), $maxLimit),
242-
'timestamp' => (int)$this->getRequest('tsAfter', 0)
243-
);
244-
$operations = $this->db->getLastTransfers($options);
245-
if(is_array($operations) && count($operations)){
246-
for($i = 0; $i < count($operations); $i++){
247-
$operation = $operations[$i];
248-
$res = array(
249-
'timestamp' => $operation['timestamp'],
250-
'transactionHash' => $operation['transactionHash'],
251-
'tokenInfo' => $operation['token'],
252-
'type' => $operation['type'],
253-
'value' => $operation['value'],
254-
);
255-
if(isset($operation['address'])){
256-
$res['address'] = $operation['address'];
257-
}
258-
if(isset($operation['from'])){
259-
$res['from'] = $operation['from'];
260-
$res['to'] = $operation['to'];
261-
}
262-
$result['operations'][] = $res;
263-
}
264-
}
265-
return $result;
227+
return $this->_getHistory();
266228
}
267229

268230
// @todo: remove copypaste
269231
public function getAddressHistory(){
232+
return $this->_getHistory(TRUE);
233+
}
234+
235+
protected function _getHistory($addressHistoryMode = FALSE){
270236
$result = array(
271237
'operations' => array()
272238
);
273239
$address = $this->getParam(0, FALSE);
274240
if($address){
275241
$address = strtolower($address);
276242
}
277-
if((FALSE !== $address) && (!$this->db->isValidAddress($address))){
243+
if((!$address && $addressHistoryMode) || ((FALSE !== $address) && (!$this->db->isValidAddress($address)))){
278244
$this->sendError(104, 'Invalid address format');
279245
}
280246
$maxLimit = is_array($this->defaults) && isset($this->defaults['limit']) ? $this->defaults['limit'] : 10;
281247
$options = array(
282-
'address' => $this->getParam(0, FALSE),
248+
'address' => $address,
283249
'type' => $this->getRequest('type', FALSE),
284250
'limit' => min(abs((int)$this->getRequest('limit', 10)), $maxLimit),
285-
'timestamp' => (int)$this->getRequest('tsAfter', 0),
286-
'history' => TRUE
287251
);
252+
if($addressHistoryMode){
253+
$token = $this->getRequest('token', FALSE);
254+
if(FALSE !== $token){
255+
$token = strtolower($token);
256+
if(!$this->db->isValidAddress($token)){
257+
$this->sendError(104, 'Invalid token address format');
258+
}
259+
$options['token'] = $token;
260+
}
261+
$options['history'] = TRUE;
262+
}
288263
$operations = $this->db->getLastTransfers($options);
289264
if(is_array($operations) && count($operations)){
290265
for($i = 0; $i < count($operations); $i++){

service/lib/ethplorer.php

+4
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ public function getLastTransfers(array $options = array()){
496496
$search['$or'] = array(array('from' => $options['address']), array('to' => $options['address']));
497497
}
498498

499+
if(isset($options['token']) && isset($options['history'])){
500+
$search['contract'] = $options['token'];
501+
}
502+
499503
$sort = array("timestamp" => -1);
500504

501505
if(isset($options['timestamp']) && ($options['timestamp'] > 0)){

0 commit comments

Comments
 (0)