Skip to content

Commit

Permalink
Ethplorer CSV export.
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-m committed Mar 1, 2017
1 parent 3bb949b commit 0216ee5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"require": {
"litipk/php-bignumbers": "0.7.3"
},
"minimum-stability": "dev",
"prefer-stable" : false
}
29 changes: 22 additions & 7 deletions service/lib/ethplorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

require_once __DIR__ . '/cache.php';
require_once __DIR__ . '/profiler.php';
require_once __DIR__ . '/../../vendor/autoload.php';

use \Litipk\BigNumbers\Decimal as Decimal;

class Ethplorer {

Expand Down Expand Up @@ -810,7 +813,7 @@ public function getLastTransfers(array $options = array()){
* @param int $limit Maximum number of records
* @return array
*/
public function getAddressOperations($address, $limit = 10, $offset = FALSE){
public function getAddressOperations($address, $limit = 10, $offset = FALSE, array $aTypes = array('transfer', 'issuance', 'burn', 'mint')){
$search = array(
'$or' => array(
array("from" => $address),
Expand All @@ -833,7 +836,7 @@ public function getAddressOperations($address, $limit = 10, $offset = FALSE){
)
);
}
$search['type'] = array('$in' => array('transfer', 'issuance', 'burn', 'mint'));
$search['type'] = array('$in' => $aTypes);

$cursor = $this->dbs['operations']->find($search)->sort(array("timestamp" => -1));
if($offset){
Expand Down Expand Up @@ -878,26 +881,38 @@ public function getAddressOperationsCSV($address, $type = 'transfer'){
if($isContract){
$addTokenInfo = false;
}
$ten = Decimal::create(10);
$dec = false;
$isToken = $this->getToken($address);
if($isToken){
$operations = $this->getLastTransfers($options);
$dec = Decimal::create($isToken['decimals']);
}else{
$operations = $this->getAddressOperations($address, $limit);
$operations = $this->getAddressOperations($address, $limit, FALSE, array('transfer'));
}
$aTokenInfo = array();
foreach($operations as $record){
$date = date("Y-m-d H:i:s", $record['timestamp']);
$hash = $record['transactionHash'];
$from = isset($record['from']) ? $record['from'] : '';
$to = isset($record['to']) ? $record['to'] : '';
$tokenName = '';
$tokenAddress = '';
// TODO: add tokens info cache
if($addTokenInfo && isset($record['contract'])){
$token = $this->getToken($record['contract']);
$tokenName = isset($token['name']) ? $token['name'] : '';
$tokenAddress = isset($token['address']) ? $token['address'] : '';
$contract = $record['contract'];
$token = isset($aTokenInfo[$contract]) ? $aTokenInfo[$contract] : $this->getToken($contract);
if($token){
$tokenName = isset($token['name']) ? $token['name'] : '';
$tokenAddress = isset($token['address']) ? $token['address'] : '';
if(isset($token['decimals'])) $dec = Decimal::create($token['decimals']);
if(!isset($aTokenInfo[$contract])) $aTokenInfo[$contract] = $token;
}
}
$value = $record['value'];
if($dec){
$value = Decimal::create($record['value']);
$value = $value->div($ten->pow($dec), 4);
}
$result .= $date . $spl . $hash . $spl . $from . $spl . $to . $spl . $tokenName . $spl . $tokenAddress . $spl . $value . $cr;
}
}
Expand Down

0 comments on commit 0216ee5

Please sign in to comment.