SHARE
TWEET

Untitled

a guest Sep 8th, 2011 500 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package MtGOX::API;
  2. use Modern::Perl;
  3. use Moose;
  4.  
  5. use LWP::UserAgent;
  6. use HTTP::Request;
  7.  
  8. use Time::HiRes qw/gettimeofday/;
  9. use Digest::SHA qw(hmac_sha512);
  10. use MIME::Base64;
  11. use JSON;
  12.  
  13. has 'key' => (
  14.         is => 'rw',
  15.         isa => 'Str'
  16. );
  17.  
  18. has 'secret' => (
  19.         is => 'rw',
  20.         isa => 'Str'
  21. );
  22.  
  23. has 'nonce' => (
  24.         is => 'ro',
  25.         isa => 'Int',
  26.         builder => '_nonce_builder'
  27. );
  28.  
  29. sub _nonce_builder {
  30.         my $self = shift;
  31.        
  32.         return join '', &gettimeofday;
  33. }
  34.  
  35. sub query {
  36.         my $self = shift;
  37.         my $op = shift;
  38.         my %vars = @_;
  39.        
  40.         $vars{'nonce'} = _nonce_builder;
  41.        
  42.         my $ua = LWP::UserAgent->new();
  43.         push @{ $ua->requests_redirectable }, 'POST';
  44.        
  45.         my $uri = URI->new('https://mtgox.com/api/0/'.$op.'.php');
  46.         $uri->query_form(\%vars);
  47.         say "query: ".$uri;
  48.         my $query = $uri->query();
  49.  
  50.         my $req = HTTP::Request->new('POST', $uri);
  51.        
  52.         $req->header('Content-Type' => 'application/x-www-form-urlencoded');
  53.         $req->header('User-Agent' => 'Mozilla/5.0 (compatible; bitcoin-bot v0.01)');
  54.         $req->header('Rest-Key' => $self->key);
  55.         $req->content($query);
  56.        
  57.         my $dec_secret = decode_base64($self->secret);
  58.         my $hmac = hmac_sha512($query, $dec_secret);
  59.         my $sign = encode_base64($hmac);
  60.         $req->header('Rest-Sign' => $sign);
  61. #       $req->header('Rest-Sign' => encode_base64(hmac_sha256($query, decode_base64($self->secret))));
  62.        
  63.         my $res = $ua->request($req);
  64.         if(!$res->is_success) {
  65.                 die 'failed to make API request: '.$res->as_string;
  66.         }
  67.        
  68.         my $json = decode_json($res->content);
  69.         if(!defined $json) {
  70.                 die 'failed to decode request json';
  71.         }
  72.        
  73.         return $json;
  74. }
  75.  
  76. sub getFunds {
  77.         return $_[0]->query('getFunds');
  78. }
  79.  
  80. sub info {
  81.         return $_[0]->query('info');
  82. }
  83.  
  84. # POST data: amount=#&price=#
  85. sub buy {
  86.         return shift->query('buyBTC', @_);
  87. }
  88.  
  89. # POST data: &amount=#&price=#
  90. sub sell {
  91.         return shift->query('sellBTC', @_);
  92. }
  93.  
  94. sub getOrders {
  95.         return $_[0]->query('getOrders');
  96. }
  97.  
  98. # POST data: oid=#&type=#
  99. sub cancelOrder {
  100.         return shift->query('cancelOrder', @_);
  101. }
  102.  
  103. # POST data: group1=BTC&btca=bitcoin_address_to_send_to&amount=#
  104. sub withdraw {
  105.         return shift->query('withdraw', @_);
  106. }
  107.  
  108. sub getDepth {
  109.         return shift->query('data/getDepth');
  110. }
  111.  
  112. sub getTrades {
  113.         return shift->query('data/getTrades', @_);
  114. }
  115.  
  116. sub ticker {
  117.         return shift->query('data/ticker');
  118. }
  119.  
  120. no Moose;
  121. __PACKAGE__->meta->make_immutable;
  122.  
  123. 1;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top