Guest User

Untitled

a guest
Oct 16th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. Address a;
  2. Uv = 0;
  3. Received = 0;
  4. Sent = 0;
  5. For each (output in blockchain):
  6. If (output is unlock-able by owner of address a):
  7. Received += output.amount;
  8. If (output has been spent):
  9. Sent += output.amount;
  10. Else:
  11. Uv += output.amount;
  12.  
  13. <?php
  14.  
  15. // Method: POST, PUT, GET etc
  16. // Data: array("param" => "value") ==> index.php?param=value
  17.  
  18. function CallAPI($method, $url, $data = false)
  19. {
  20. $curl = curl_init();
  21.  
  22. switch ($method)
  23. {
  24. case "POST":
  25. curl_setopt($curl, CURLOPT_POST, 1);
  26.  
  27. if ($data)
  28. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  29. break;
  30. case "PUT":
  31. curl_setopt($curl, CURLOPT_PUT, 1);
  32. break;
  33. default:
  34. if ($data)
  35. $url = sprintf("%s?%s", $url, http_build_query($data));
  36. }
  37.  
  38. // Optional Authentication:
  39. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  40. // curl_setopt($curl, CURLOPT_USERPWD, "username:password");
  41.  
  42. curl_setopt($curl, CURLOPT_URL, $url);
  43. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  44.  
  45. $result = curl_exec($curl);
  46.  
  47. curl_close($curl);
  48.  
  49. return $result;
  50. }
  51.  
  52. $address = "1EZK42jGEJVniyBn1wXrUx92wzyUkYB8kJ";
  53. $offset=0;
  54. $txs = array();
  55.  
  56. while (true)
  57. {
  58. $val = json_decode(CallAPI("GET", "https://blockchain.info/address/$address?format=json&limit=10&offset=$offset"), true);
  59. if (count($val["txs"]) == 0)
  60. break;
  61. else
  62. $txs = array_merge($txs, $val["txs"]);
  63.  
  64. $offset += 10;
  65. }
  66.  
  67. $uv = 0;
  68. $received = 0;
  69. $sent = 0;
  70. $nrefs = 0;
  71.  
  72. foreach ($txs as $tx)
  73. {
  74. $ininputs = false;
  75. $totalin = 0;
  76. foreach ($tx["inputs"] as $input)
  77. {
  78. if ($input["prev_out"]["addr"] == $address)
  79. {
  80. $ininputs = true;
  81. $nrefs += 1;
  82. $totalin += $input["prev_out"]["value"];
  83. }
  84. }
  85.  
  86. $inoutputs = false;
  87. $totalout = 0;
  88. foreach ($tx["out"] as $output)
  89. {
  90. if ($output["addr"] == $address)
  91. {
  92. $inoutputs = true;
  93. $nrefs += 1;
  94. $totalout += $output["value"];
  95. }
  96. }
  97.  
  98. $uv += ($totalout - $totalin);
  99.  
  100. // $received += $totalout;
  101. // $sent += $totalin;
  102.  
  103. if ($ininputs && $inoutputs && $totalout >= $totalin)
  104. {
  105. $received += $totalout - $totalin;
  106. }
  107. else if ($ininputs && $inoutputs && $totalout < $totalin)
  108. {
  109. $sent += $totalin - $totalout;
  110. }
  111. else if ($inoutputs)
  112. {
  113. $received += $totalout;
  114. }
  115. else if ($ininputs)
  116. {
  117. $sent += $totalin;
  118. }
  119. }
  120. echo "n";
  121.  
  122. echo "nrefs : " . $nrefs . "n";
  123. echo "uv : " . $uv . "n";
  124. echo "received : " . $received . "n";
  125. echo "sent : " . $sent . "n";
Add Comment
Please, Sign In to add comment