first commit
This commit is contained in:
141
NiceHash/Nicehashapi.pm
Normal file
141
NiceHash/Nicehashapi.pm
Normal file
@@ -0,0 +1,141 @@
|
||||
|
||||
package Nicehashapi;
|
||||
|
||||
use Moo;
|
||||
use strictures 2;
|
||||
use namespace::clean;
|
||||
use File::Slurp;
|
||||
use Data::Printer;
|
||||
use Mojo::UserAgent;
|
||||
use Digest::SHA qw(hmac_sha256_hex);;
|
||||
use UUID::Tiny ':std';
|
||||
use Time::HiRes qw(gettimeofday);
|
||||
use Encode qw(decode encode);
|
||||
use Data::Dumper;
|
||||
|
||||
has key => (
|
||||
is => 'ro',
|
||||
);
|
||||
|
||||
has secret => (
|
||||
is => 'ro',
|
||||
);
|
||||
|
||||
has organisation_id => (
|
||||
is => 'ro',
|
||||
);
|
||||
|
||||
# my $key = '8bcbda58-5a26-4ce4-8e61-3a0fd894cfbb';
|
||||
# my $secret = '59dc1ee5-a0e6-4ba6-9a9c-a043c350b88b84266786-2d14-4b9f-9ae5-d168ed8376b7';
|
||||
# my $organisation_id = '4c615374-bf85-42fd-860d-b1bf4568c01f';
|
||||
|
||||
sub get_payouts {
|
||||
my ( $self, $page, $size) = @_;
|
||||
return _request($self, 'GET', '/main/api/v2/mining/rigs/payouts', "page=$page&size=$size", undef);
|
||||
}
|
||||
|
||||
sub get_currencies {
|
||||
my ( $self) = @_;
|
||||
return _request($self, 'GET', '/main/api/v2/public/currencies/', '', undef);
|
||||
}
|
||||
|
||||
sub get_prices {
|
||||
my ( $self) = @_;
|
||||
return _request($self, 'GET', '/exchange/api/v2/info/prices', '', undef);
|
||||
}
|
||||
|
||||
sub get_hashpowerEarnings {
|
||||
my ( $self, $currencies) = @_;
|
||||
return _request($self, 'GET', '/main/api/v2/accounting/hashpowerEarnings/' . $currencies, '', undef);
|
||||
}
|
||||
|
||||
sub get_myOrders {
|
||||
my ( $self) = @_;
|
||||
return _request($self, 'GET', '/exchange/api/v2/info/myOrders', '', undef);
|
||||
}
|
||||
|
||||
sub get_accounts2 {
|
||||
my ( $self, $extendedResponse, $fiat) = @_;
|
||||
return _request($self, 'GET', '/main/api/v2/accounting/accounts2', "extendedResponse=$extendedResponse&fiat=$fiat", undef);
|
||||
}
|
||||
|
||||
sub get_accounting_activity {
|
||||
my ( $self, $currency, $type, $stage, $limit ) = @_;
|
||||
my $query;
|
||||
|
||||
if ( defined $limit ) {
|
||||
$query = "limit=$limit";
|
||||
}
|
||||
|
||||
if ( defined $type ) {
|
||||
$query .= "&type=$type";
|
||||
}
|
||||
|
||||
if ( defined $stage ) {
|
||||
$query .= "&stage=$stage";
|
||||
}
|
||||
|
||||
return _request($self, 'GET', '/main/api/v2/accounting/activity/'.$currency, $query, undef);
|
||||
# return _request($self, 'GET', '/main/api/v2/accounting/activity/'.$currency, '' , undef);
|
||||
}
|
||||
|
||||
sub _request {
|
||||
my ( $self, $method, $path, $query, $body) = @_;
|
||||
|
||||
my $xtime = int (gettimeofday * 1000);
|
||||
my $xnonce = create_uuid(UUID_V4);
|
||||
my $uid = uuid_to_string($xnonce);
|
||||
my $xnonce2 = create_uuid(UUID_V4);
|
||||
my $uid2 = uuid_to_string($xnonce);
|
||||
|
||||
my $message = encode('UTF-8', $self->key);
|
||||
$message .= encode('UTF-8', chr(0x00));
|
||||
$message .= encode('UTF-8',$xtime);
|
||||
$message .= encode('UTF-8',chr(0x00));
|
||||
$message .= encode('UTF-8',$uid);
|
||||
$message .= encode('UTF-8',chr(0x00));
|
||||
$message .= encode('UTF-8',chr(0x00));
|
||||
$message .= encode('UTF-8', $self->organisation_id);
|
||||
$message .= encode('UTF-8',chr(0x00));
|
||||
$message .= encode('UTF-8',chr(0x00));
|
||||
$message .= encode('UTF-8',$method);
|
||||
$message .= encode('UTF-8',chr(0x00));
|
||||
$message .= encode('UTF-8',$path);
|
||||
$message .= encode('UTF-8',chr(0x00));
|
||||
$message .= encode('UTF-8',$query);
|
||||
|
||||
if ($body) {
|
||||
my $body_json = Dumper($body);
|
||||
$message .= encode('UTF-8',chr(0x00));
|
||||
$message .= encode('UTF-8', $body_json);
|
||||
}
|
||||
|
||||
my $digest = hmac_sha256_hex($message, $self->secret);
|
||||
my $ua = Mojo::UserAgent->new();
|
||||
|
||||
my $headers = {
|
||||
'X-Time' => $xtime,
|
||||
'X-Nonce'=> $uid,
|
||||
'X-Auth'=> $self->key . ':' . $digest ,
|
||||
'Content-Type'=> 'application/json',
|
||||
'X-Organization-Id'=> $self->organisation_id,
|
||||
'X-Request-Id'=> $uid2
|
||||
};
|
||||
|
||||
if ( $query ) {
|
||||
$query = '?'.$query;
|
||||
}
|
||||
|
||||
my $data = $ua->get('https://api2.nicehash.com' . $path.$query => $headers )->res;
|
||||
|
||||
if ( $path =~ /activity/ ) {
|
||||
p $data;
|
||||
print $path.$query."\n";
|
||||
}
|
||||
|
||||
# my $data = $ua->get('https://api2.nicehash.com' . $path.$query => $headers )->res->json;
|
||||
|
||||
return $data->json;
|
||||
}
|
||||
|
||||
1;
|
||||
Reference in New Issue
Block a user