Skip to content

Commit 9c09f3b

Browse files
committed
getAddressInfo added
1 parent f7689ad commit 9c09f3b

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

api/controller.php

+51-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class ethplorerController {
1919
protected $db;
2020
protected $command;
2121
protected $params = array();
22-
protected $apiCommands = array('getTxInfo', 'getTokenHistory', 'getAddressInfo', 'getTokenInfo');
22+
protected $apiCommands = array('getTxInfo', 'getTokenHistory', 'getAddressInfo', 'getTokenInfo', 'getAddressHistory');
23+
protected $defaults;
2324

2425
public function __construct($es){
2526
if(!($es instanceof Ethplorer)){
@@ -76,9 +77,11 @@ public function sendError($code, $message){
7677
public function run(){
7778
$command = $this->getCommand();
7879
if($command && in_array($command, $this->apiCommands) && method_exists($this, $command)){
79-
if(!$this->db->checkAPIkey($this->getRequest('apiKey', FALSE))){
80+
$key = $this->getRequest('apiKey', FALSE);
81+
if(!$key || !$this->db->checkAPIkey($key)){
8082
$this->sendError(1, 'Invalid API key');
8183
}
84+
$this->defaults = $this->db->getAPIKeyDefaults($key, $this->getCommand());
8285
$result = call_user_func(array($this, $this->getCommand()));
8386
$this->sendResult($result);
8487
}
@@ -231,10 +234,11 @@ public function getTokenHistory(){
231234
if((FALSE !== $address) && (!$this->db->isValidAddress($address))){
232235
$this->sendError(104, 'Invalid address format');
233236
}
237+
$maxLimit = is_array($this->defaults) && isset($this->defaults['limit']) ? $this->defaults['limit'] : 10;
234238
$options = array(
235239
'address' => $this->getParam(0, FALSE),
236240
'type' => $this->getRequest('type', FALSE),
237-
'limit' => min(abs((int)$this->getRequest('limit', 10)), 10),
241+
'limit' => min(abs((int)$this->getRequest('limit', 10)), $maxLimit),
238242
'timestamp' => (int)$this->getRequest('tsAfter', 0)
239243
);
240244
$operations = $this->db->getLastTransfers($options);
@@ -261,6 +265,50 @@ public function getTokenHistory(){
261265
return $result;
262266
}
263267

268+
// @todo: remove copypaste
269+
public function getAddressHistory(){
270+
$result = array(
271+
'operations' => array()
272+
);
273+
$address = $this->getParam(0, FALSE);
274+
if($address){
275+
$address = strtolower($address);
276+
}
277+
if((FALSE !== $address) && (!$this->db->isValidAddress($address))){
278+
$this->sendError(104, 'Invalid address format');
279+
}
280+
$maxLimit = is_array($this->defaults) && isset($this->defaults['limit']) ? $this->defaults['limit'] : 10;
281+
$options = array(
282+
'address' => $this->getParam(0, FALSE),
283+
'type' => $this->getRequest('type', FALSE),
284+
'limit' => min(abs((int)$this->getRequest('limit', 10)), $maxLimit),
285+
'timestamp' => (int)$this->getRequest('tsAfter', 0),
286+
'history' => TRUE
287+
);
288+
$operations = $this->db->getLastTransfers($options);
289+
if(is_array($operations) && count($operations)){
290+
for($i = 0; $i < count($operations); $i++){
291+
$operation = $operations[$i];
292+
$res = array(
293+
'timestamp' => $operation['timestamp'],
294+
'transactionHash' => $operation['transactionHash'],
295+
'tokenInfo' => $operation['token'],
296+
'type' => $operation['type'],
297+
'value' => $operation['value'],
298+
);
299+
if(isset($operation['address'])){
300+
$res['address'] = $operation['address'];
301+
}
302+
if(isset($operation['from'])){
303+
$res['from'] = $operation['from'];
304+
$res['to'] = $operation['to'];
305+
}
306+
$result['operations'][] = $res;
307+
}
308+
}
309+
return $result;
310+
}
311+
264312
protected function _bn2float($aNumber){
265313
$res = 0;
266314
if(isset($aNumber['c']) && !empty($aNumber['c'])){

service/lib/ethplorer.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,13 @@ public function getLastTransfers(array $options = array()){
489489
$search['type'] = $options['type'];
490490
}
491491
}
492-
if(isset($options['address'])){
492+
if(isset($options['address']) && !isset($options['history'])){
493493
$search['contract'] = $options['address'];
494494
}
495+
if(isset($options['address']) && isset($options['history'])){
496+
$search['$or'] = array(array('from' => $options['address']), array('to' => $options['address']));
497+
}
498+
495499
$sort = array("timestamp" => -1);
496500

497501
if(isset($options['timestamp']) && ($options['timestamp'] > 0)){
@@ -555,7 +559,21 @@ public function getAddressOperations($address, $limit = 10){
555559
}
556560

557561
public function checkAPIKey($key){
558-
return isset($this->aSettings['apiKeys']) && (FALSE !== array_search($key, $this->aSettings['apiKeys']));
562+
return isset($this->aSettings['apiKeys']) && isset($this->aSettings['apiKeys'][$key]);
563+
}
564+
565+
public function getAPIKeyDefaults($key, $option = FALSE){
566+
$res = FALSE;
567+
if($this->checkAPIKey($key)){
568+
if(is_array($this->aSettings['apiKeys'][$key])){
569+
if(FALSE === $option){
570+
$res = $this->aSettings['apiKeys'][$key];
571+
}else if(isset($this->aSettings['apiKeys'][$key][$option])){
572+
$res = $this->aSettings['apiKeys'][$key][$option];
573+
}
574+
}
575+
}
576+
return $res;
559577
}
560578

561579
/**

0 commit comments

Comments
 (0)