Skip to content

Commit 6861ccf

Browse files
committed
getTopTokens method added
1 parent 63b8c79 commit 6861ccf

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

api/controller.php

Lines changed: 18 additions & 3 deletions
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', 'getTopTokens');
2323
protected $defaults;
2424

2525
public function __construct($es){
@@ -87,8 +87,8 @@ public function run(){
8787
if(!$key || !$this->db->checkAPIkey($key)){
8888
$this->sendError(1, 'Invalid API key');
8989
}
90-
$this->defaults = $this->db->getAPIKeyDefaults($key, $this->getCommand());
91-
$result = call_user_func(array($this, $this->getCommand()));
90+
$this->defaults = $this->db->getAPIKeyDefaults($key, $command);
91+
$result = call_user_func(array($this, $command));
9292
}
9393
return $result;
9494
}
@@ -262,6 +262,21 @@ public function getAddressHistory(){
262262
return $this->_getHistory(TRUE);
263263
}
264264

265+
/**
266+
* /getTopTokens method implementation.
267+
*
268+
* @undocumented
269+
* @return array
270+
*/
271+
public function getTopTokens(){
272+
$maxLimit = is_array($this->defaults) && isset($this->defaults['maxLimit']) ? $this->defaults['maxLimit'] : 50;
273+
$maxPeriod = is_array($this->defaults) && isset($this->defaults['maxPeriod']) ? $this->defaults['maxPeriod'] : 90;
274+
$limit = min(abs((int)$this->getRequest('limit', 10)), $maxLimit);
275+
$period = min(abs((int)$this->getRequest('limit', 10)), $maxPeriod);
276+
$result = array('tokens' => $this->db->getTopTokens($limit, $period));
277+
$this->sendResult($result);
278+
}
279+
265280
/**
266281
*
267282
* Common method to get token and address operation history.

service/lib/ethplorer.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,44 @@ public function getAddressOperations($address, $limit = 10){
596596
return $result;
597597
}
598598

599+
/**
600+
* Returns top tokens list.
601+
*
602+
* @param int $limit Maximum records number
603+
* @param int $period Days from now
604+
* @return array
605+
*/
606+
public function getTopTokens($limit = 10, $period = 30){
607+
$cache = 'top_tokens-' . $period . '-' . $limit;
608+
$result = $this->oCache->get($cache, false, true, 24 * 3600);
609+
if(FALSE === $result){
610+
$result = array();
611+
$dbData = $this->dbs['operations']->aggregate(
612+
array(
613+
array('$match' => array("timestamp" => array('$gt' => time() - $period * 24 * 3600))),
614+
array(
615+
'$group' => array(
616+
"_id" => '$contract',
617+
'cnt' => array('$sum' => 1)
618+
)
619+
),
620+
array('$sort' => array('cnt' => -1)),
621+
array('$limit' => $limit)
622+
)
623+
);
624+
if(is_array($dbData) && !empty($dbData['result'])){
625+
foreach($dbData['result'] as $token){
626+
$oToken = $this->getToken($token['_id']);
627+
$oToken['opCount'] = $token['cnt'];
628+
unset($oToken['checked']);
629+
$result[] = $oToken;
630+
}
631+
$this->oCache->save($cache, $result);
632+
}
633+
}
634+
return $result;
635+
}
636+
599637
public function checkAPIKey($key){
600638
return isset($this->aSettings['apiKeys']) && isset($this->aSettings['apiKeys'][$key]);
601639
}

0 commit comments

Comments
 (0)