Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /***************
- Decoded by JM for Freelancer (Uganda) Demo
- *************
- */
- namespace App\Adapters\Coin;
- use Exceptions\AdapterException;
- use GuzzleHttp\Client;
- use HolluwaTosin360\BitGoPHP\Coin;
- use Resources\Address;
- use Resources\Transaction;
- use Resources\Wallet;
- class BitcoinAdapter extends CoinAdapter
- {
- protected $bitgo = null;
- protected $dollarPrice = null;
- const NAME = "Bitcoin";
- const IDENTIFIER = "btc";
- const BASE_UNIT = 100000000;
- const PRECISION = 8;
- const SYMBOL = "BTC";
- const SYMBOL_FIRST = true;
- const COLOR = "#c27000";
- public function __construct()
- {
- $this->init();
- }
- private function init()
- {
- $bitgo = resolve("HolluwaTosin360\\BitGoPHP\\BitGo");
- if( config()->get("bitgo.env") == "prod" )
- {
- $bitgo->setCoin(Coin::BITCOIN);
- }
- else
- {
- $bitgo->setCoin(Coin::TEST_BITCOIN);
- }
- $this->bitgo = $bitgo;
- }
- public function __sleep()
- {
- return [];
- }
- public function __wakeup()
- {
- $this->init();
- }
- public function getName() : string
- {
- return NAME;
- }
- public function getIdentifier() : string
- {
- return IDENTIFIER;
- }
- public function getBaseUnit() : int
- {
- return BASE_UNIT;
- }
- public function getPrecision() : int
- {
- return PRECISION;
- }
- public function getSymbol() : string
- {
- return SYMBOL;
- }
- public function showSymbolFirst() : bool
- {
- return SYMBOL_FIRST;
- }
- public function getColor() : string
- {
- return COLOR;
- }
- public function createWallet($label, $passphrase) : Wallet
- {
- $response = $this->bitgo->generateWallet($label, $passphrase);
- $data = collect($response);
- return new Wallet(["id" => $data->get("id"), "data" => $data->toArray()]);
- }
- public function createWalletAddress(Wallet $wallet, $label = "Default") : Address
- {
- $this->bitgo->setWalletId($wallet->getId());
- $response = $this->bitgo->createWalletAddress(false, 0, null, $label);
- $data = collect($response);
- return new Address(["id" => $data->get("id"), "label" => $label, "address" => $data->get("address"), "data" => $data->toArray()]);
- }
- public function send(Wallet $wallet, $address, $amount, $passphrase) : Transaction
- {
- $this->bitgo->setWalletId($wallet->getId());
- $response = $this->bitgo->sendTransaction($address, $amount, $passphrase);
- if( !isset($response["transfer"]) )
- {
- throw new AdapterException("Invalid transaction data!");
- }
- $data = collect($response["transfer"]);
- 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")]);
- }
- public function getWalletTransaction(Wallet $wallet, $id) : Transaction
- {
- $this->bitgo->setWalletId($wallet->getId());
- $response = $this->bitgo->getWalletTransfer($id);
- $data = collect($response);
- 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")]);
- }
- public function handleTransactionWebhook(Wallet $wallet, $payload) : ?Transaction
- {
- $body = collect($payload);
- if( $body->get("type") !== "transfer" )
- {
- return null;
- }
- if( $body->get("wallet") !== $wallet->getId() )
- {
- return null;
- }
- if( !$body->get("hash") )
- {
- return null;
- }
- $this->bitgo->setWalletId($body->get("wallet"));
- $response = $this->bitgo->getWalletTransfer($body->get("hash"));
- $data = collect($response);
- 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")]);
- }
- protected function parseAddress($address)
- {
- if( !is_array($address) )
- {
- throw new AdapterException("Invalid address format.");
- }
- else
- {
- return collect($address)->map(function($object)
- {
- if( !is_array($object) )
- {
- throw new AdapterException("Address is expected to be an array of objects.");
- }
- if( !isset($object["value"]) && isset($object["valueString"]) )
- {
- $object["value"] = (int)$object["valueString"];
- }
- if( !array_has($object, ["address", "value"]) )
- {
- throw new AdapterException("Objects should contain address, value pairs.");
- }
- return array_only($object, ["address", "value"]);
- })->toArray();
- }
- }
- public function getDollarPrice() : float
- {
- if( !isset($this->dollarPrice) )
- {
- $client = new Client(["base_uri" => "https://min-api.cryptocompare.com/"]);
- $response = $client->get("data/price", ["query" => array_filter(["fsym" => strtoupper($this->getIdentifier()), "tsyms" => "USD", "api_key" => config("cryptocompare.key")])]);
- $price = json_decode($response->getBody(), true)["USD"];
- $this->dollarPrice = (double)$price;
- }
- return $this->dollarPrice;
- }
- public function setTransactionWebhook(Wallet $wallet, int $minConfirmations = 3)
- {
- $url = $this->transactionWebhookUrl();
- $this->bitgo->setWalletId($wallet->getId());
- if( $minConfirmations > 1 )
- {
- $this->bitgo->addWalletWebhook($url, "transfer", 0);
- $this->bitgo->addWalletWebhook($url, "transfer", 1);
- $this->bitgo->addWalletWebhook($url, "transfer", $minConfirmations);
- $this->bitgo->addWalletWebhook($url, "transfer", $minConfirmations + 1);
- }
- else
- {
- $this->bitgo->addWalletWebhook($url, "transfer", 0);
- $this->bitgo->addWalletWebhook($url, "transfer", 1);
- }
- }
- public function unsetTransactionWebhook(Wallet $wallet)
- {
- $this->bitgo->setWalletId($wallet->getId());
- $url = $this->transactionWebhookUrl();
- $this->bitgo->removeWalletWebhook($url, "transfer");
- }
- public function getFeePerByteEstimate()
- {
- return $this->bitgo->feeEstimate()["feePerKb"] / 1000;
- }
- public function calcTransactionFee(int $inputs, int $outputs, $amount = 0) : float
- {
- $feePerByte = $this->getFeePerByteEstimate();
- $bitgoFee = config("bitgo.fee_percent") * $amount;
- $multiplier = $inputs * 180 + (($outputs + 1) * 50) + 50 + $inputs;
- return $feePerByte * $multiplier + $bitgoFee;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement