Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. <?php
  2. /***************
  3. Decoded by JM for Freelancer (Uganda) Demo
  4.  
  5. *************
  6. */
  7. namespace App\Adapters\Coin;
  8.  
  9. use Exceptions\AdapterException;
  10. use GuzzleHttp\Client;
  11. use HolluwaTosin360\BitGoPHP\Coin;
  12. use Resources\Address;
  13. use Resources\Transaction;
  14. use Resources\Wallet;
  15.  
  16. class BitcoinAdapter extends CoinAdapter
  17. {
  18. protected $bitgo = null;
  19. protected $dollarPrice = null;
  20. const NAME = "Bitcoin";
  21. const IDENTIFIER = "btc";
  22. const BASE_UNIT = 100000000;
  23. const PRECISION = 8;
  24. const SYMBOL = "BTC";
  25. const SYMBOL_FIRST = true;
  26. const COLOR = "#c27000";
  27. public function __construct()
  28. {
  29. $this->init();
  30. }
  31. private function init()
  32. {
  33. $bitgo = resolve("HolluwaTosin360\\BitGoPHP\\BitGo");
  34. if( config()->get("bitgo.env") == "prod" )
  35. {
  36. $bitgo->setCoin(Coin::BITCOIN);
  37. }
  38. else
  39. {
  40. $bitgo->setCoin(Coin::TEST_BITCOIN);
  41. }
  42. $this->bitgo = $bitgo;
  43. }
  44. public function __sleep()
  45. {
  46. return [];
  47. }
  48. public function __wakeup()
  49. {
  50. $this->init();
  51. }
  52. public function getName() : string
  53. {
  54. return NAME;
  55. }
  56. public function getIdentifier() : string
  57. {
  58. return IDENTIFIER;
  59. }
  60. public function getBaseUnit() : int
  61. {
  62. return BASE_UNIT;
  63. }
  64. public function getPrecision() : int
  65. {
  66. return PRECISION;
  67. }
  68. public function getSymbol() : string
  69. {
  70. return SYMBOL;
  71. }
  72. public function showSymbolFirst() : bool
  73. {
  74. return SYMBOL_FIRST;
  75. }
  76. public function getColor() : string
  77. {
  78. return COLOR;
  79. }
  80. public function createWallet($label, $passphrase) : Wallet
  81. {
  82. $response = $this->bitgo->generateWallet($label, $passphrase);
  83. $data = collect($response);
  84. return new Wallet(["id" => $data->get("id"), "data" => $data->toArray()]);
  85. }
  86. public function createWalletAddress(Wallet $wallet, $label = "Default") : Address
  87. {
  88. $this->bitgo->setWalletId($wallet->getId());
  89. $response = $this->bitgo->createWalletAddress(false, 0, null, $label);
  90. $data = collect($response);
  91. return new Address(["id" => $data->get("id"), "label" => $label, "address" => $data->get("address"), "data" => $data->toArray()]);
  92. }
  93. public function send(Wallet $wallet, $address, $amount, $passphrase) : Transaction
  94. {
  95. $this->bitgo->setWalletId($wallet->getId());
  96. $response = $this->bitgo->sendTransaction($address, $amount, $passphrase);
  97. if( !isset($response["transfer"]) )
  98. {
  99. throw new AdapterException("Invalid transaction data!");
  100. }
  101. $data = collect($response["transfer"]);
  102. return new Transaction(["id" => $data->get("id"), "value" => (int)$data->get("value", $data->get("valueString")), "hash" => $data->get("txid"), "input" => $this->parseAddress($data->get("inputs", [])), "output" => $this->parseAddress($data->get("outputs", [])), "confirmations" => (int)$data->get("confirmations"), "type" => $data->get("type"), "date" => $data->get("date")]);
  103. }
  104. public function getWalletTransaction(Wallet $wallet, $id) : Transaction
  105. {
  106. $this->bitgo->setWalletId($wallet->getId());
  107. $response = $this->bitgo->getWalletTransfer($id);
  108. $data = collect($response);
  109. return new Transaction(["id" => $data->get("id"), "value" => (int)$data->get("value", $data->get("valueString")), "hash" => $data->get("txid"), "input" => $this->parseAddress($data->get("inputs", [])), "output" => $this->parseAddress($data->get("outputs", [])), "confirmations" => (int)$data->get("confirmations"), "type" => $data->get("type"), "date" => $data->get("date")]);
  110. }
  111. public function handleTransactionWebhook(Wallet $wallet, $payload) : ?Transaction
  112. {
  113. $body = collect($payload);
  114. if( $body->get("type") !== "transfer" )
  115. {
  116. return null;
  117. }
  118. if( $body->get("wallet") !== $wallet->getId() )
  119. {
  120. return null;
  121. }
  122. if( !$body->get("hash") )
  123. {
  124. return null;
  125. }
  126. $this->bitgo->setWalletId($body->get("wallet"));
  127. $response = $this->bitgo->getWalletTransfer($body->get("hash"));
  128. $data = collect($response);
  129. return new Transaction(["id" => $data->get("id"), "value" => (int)$data->get("value", $data->get("valueString")), "hash" => $data->get("txid"), "input" => $this->parseAddress($data->get("inputs", [])), "output" => $this->parseAddress($data->get("outputs", [])), "confirmations" => (int)$data->get("confirmations"), "type" => $data->get("type"), "date" => $data->get("date")]);
  130. }
  131. protected function parseAddress($address)
  132. {
  133. if( !is_array($address) )
  134. {
  135. throw new AdapterException("Invalid address format.");
  136. }
  137. else
  138. {
  139. return collect($address)->map(function($object)
  140. {
  141. if( !is_array($object) )
  142. {
  143. throw new AdapterException("Address is expected to be an array of objects.");
  144. }
  145. if( !isset($object["value"]) && isset($object["valueString"]) )
  146. {
  147. $object["value"] = (int)$object["valueString"];
  148. }
  149. if( !array_has($object, ["address", "value"]) )
  150. {
  151. throw new AdapterException("Objects should contain address, value pairs.");
  152. }
  153. return array_only($object, ["address", "value"]);
  154. })->toArray();
  155. }
  156. }
  157. public function getDollarPrice() : float
  158. {
  159. if( !isset($this->dollarPrice) )
  160. {
  161. $client = new Client(["base_uri" => "https://min-api.cryptocompare.com/"]);
  162. $response = $client->get("data/price", ["query" => array_filter(["fsym" => strtoupper($this->getIdentifier()), "tsyms" => "USD", "api_key" => config("cryptocompare.key")])]);
  163. $price = json_decode($response->getBody(), true)["USD"];
  164. $this->dollarPrice = (double)$price;
  165. }
  166. return $this->dollarPrice;
  167. }
  168. public function setTransactionWebhook(Wallet $wallet, int $minConfirmations = 3)
  169. {
  170. $url = $this->transactionWebhookUrl();
  171. $this->bitgo->setWalletId($wallet->getId());
  172. if( $minConfirmations > 1 )
  173. {
  174. $this->bitgo->addWalletWebhook($url, "transfer", 0);
  175. $this->bitgo->addWalletWebhook($url, "transfer", 1);
  176. $this->bitgo->addWalletWebhook($url, "transfer", $minConfirmations);
  177. $this->bitgo->addWalletWebhook($url, "transfer", $minConfirmations + 1);
  178. }
  179. else
  180. {
  181. $this->bitgo->addWalletWebhook($url, "transfer", 0);
  182. $this->bitgo->addWalletWebhook($url, "transfer", 1);
  183. }
  184. }
  185. public function unsetTransactionWebhook(Wallet $wallet)
  186. {
  187. $this->bitgo->setWalletId($wallet->getId());
  188. $url = $this->transactionWebhookUrl();
  189. $this->bitgo->removeWalletWebhook($url, "transfer");
  190. }
  191. public function getFeePerByteEstimate()
  192. {
  193. return $this->bitgo->feeEstimate()["feePerKb"] / 1000;
  194. }
  195. public function calcTransactionFee(int $inputs, int $outputs, $amount = 0) : float
  196. {
  197. $feePerByte = $this->getFeePerByteEstimate();
  198. $bitgoFee = config("bitgo.fee_percent") * $amount;
  199. $multiplier = $inputs * 180 + (($outputs + 1) * 50) + 50 + $inputs;
  200. return $feePerByte * $multiplier + $bitgoFee;
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement