Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support pagination for topholders and topram #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ taking the data (such as `eos`, `telos`, `wax` etc.).
balance. Zero is returned if the token is not present or does not
exist.

* `http://apihost.domain/api/topholders/CHAIN/CONTRACT/TOKEN/NUM` returns
* `http://apihost.domain/api/topholders/CHAIN/CONTRACT/TOKEN/NUM[/MARKER]` returns
top NUM holders of a specified token in a JSON array containing arrays
of (account, amount) pairs. NUM must not be less than 10 or more than
1000.
1000. MARKER is token integer as pagination offset.

* `http://apihost.domain/api/holdercount/CHAIN/CONTRACT/TOKEN` returns the
total count of token holders as plain text. The result is "0" if the
Expand All @@ -50,9 +50,10 @@ taking the data (such as `eos`, `telos`, `wax` etc.).
* `http://apihost.domain/api/usercount/CHAIN`
returns a plain text with total number of accounts in the network.

* `http://apihost.domain/api/topram/CHAIN/NUM` returns top NUM RAM buyers
* `http://apihost.domain/api/topram/CHAIN/NUM[/MARKER]` returns top NUM RAM buyers
in a JSON array containing arrays of (account, bytes) pairs. NUM must
not be less than 10 or more than 1000.
not be less than 10 or more than 1000. MARKER is bytes number as pagination
offset.

* `http://apihost.domain/api/topstake/CHAIN/NUM` returns top NUM stake
holders by sum of CPU and Net stakes, in a JSON array containing
Expand Down
51 changes: 37 additions & 14 deletions api/lightapi.psgi
Original file line number Diff line number Diff line change
Expand Up @@ -929,27 +929,39 @@ $builder->mount
my $req = Plack::Request->new($env);
my $path_info = $req->path_info;

if ( $path_info !~ /^\/(\w+)\/([a-z1-5.]{1,13})\/([A-Z]{1,7})\/(\d+)$/ ) {
return(error($req, 'Expected network name, contract, token name, count in URL path'));
if ( $path_info !~ /^\/(\w+)\/([a-z1-5.]{1,13})\/([A-Z]{1,7})\/(\d+)(\/(\d+))?$/ ) {
return(error($req, 'Expected network name, contract, token name, count, marker in URL path'));
}

my $network = $1;
my $contract = $2;
my $currency = $3;
my $count = $4;
my $marker = $6;

if( $count < 10 or $count > 1000 )
{
return(error($req, 'Invalid count: ' . $count));
}

check_dbserver();
my $sth_topholders = $dbh->prepare
('SELECT account_name, CAST(amount AS DECIMAL(48,24)) AS amt, decimals ' .
'FROM ' . $network . '_CURRENCY_BAL ' .
'WHERE contract=? AND currency=? ' .
'ORDER BY amount DESC LIMIT ?');
$sth_topholders->execute($contract, $currency, $count);

my $sth_topholders;
if ( $marker ) {
$sth_topholders = $dbh->prepare
('SELECT account_name, CAST(amount AS DECIMAL(48,24)) AS amt, decimals ' .
'FROM ' . $network . '_CURRENCY_BAL ' .
'WHERE contract=? AND currency=? AND amount < ? ' .
'ORDER BY amount DESC LIMIT ?');
$sth_topholders->execute($contract, $currency, $marker, $count);
} else {
$sth_topholders = $dbh->prepare
('SELECT account_name, CAST(amount AS DECIMAL(48,24)) AS amt, decimals ' .
'FROM ' . $network . '_CURRENCY_BAL ' .
'WHERE contract=? AND currency=? ' .
'ORDER BY amount DESC LIMIT ?');
$sth_topholders->execute($contract, $currency, $count);
}
my $all = $sth_topholders->fetchall_arrayref({});
my $result = [];
foreach my $r (@{$all})
Expand Down Expand Up @@ -1135,24 +1147,35 @@ $builder->mount
my $req = Plack::Request->new($env);
my $path_info = $req->path_info;

if ( $path_info !~ /^\/(\w+)\/(\d+)$/ ) {
return(error($req, 'Expected a network name and count in URL path'));
if ( $path_info !~ /^\/(\w+)\/(\d+)(\/(\d+))?$/ ) {
return(error($req, 'Expected a network name, count and marker in URL path'));
}

my $network = $1;
my $count = $2;
my $marker = $4;

if( $count < 10 or $count > 1000 )
{
return(error($req, 'Invalid count: ' . $count));
}

check_dbserver();
my $sth_topram = $dbh->prepare
('SELECT account_name, ram_bytes FROM ' . $network . '_USERRES ' .
'ORDER BY ram_bytes DESC LIMIT ?');

$sth_topram->execute($count);
my $sth_topram;
if ( $marker ) {
$sth_topram = $dbh->prepare
('SELECT account_name, ram_bytes FROM ' . $network . '_USERRES ' .
'WHERE ram_bytes < ? ' .
'ORDER BY ram_bytes DESC LIMIT ?');
$sth_topram->execute($marker, $count);
} else {
$sth_topram = $dbh->prepare
('SELECT account_name, ram_bytes FROM ' . $network . '_USERRES ' .
'ORDER BY ram_bytes DESC LIMIT ?');
$sth_topram->execute($count);
}

my $all = $sth_topram->fetchall_arrayref({});
my $result = [];
foreach my $r (@{$all})
Expand Down