Created
December 15, 2016 02:50
-
-
Save elias19r/614ffd1e8ca4f56efe9295fb00b1f1f4 to your computer and use it in GitHub Desktop.
Converting BRL to BTC and BTC to BRL using the public orderbook from FOXBIT Exchange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Converting BRL to BTC and BTC to BRL using the public orderbook from FOXBIT Exchange | |
* | |
* NOTE | |
* - use CURL to get the orderbook | |
* - use BCMath for arithmetic | |
*/ | |
function getOrderbook() | |
{ | |
// Foxbit public orderbook | |
$orderbookPublicUrl = 'https://api.blinktrade.com/api/v1/BRL/orderbook'; | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_URL, $orderbookPublicUrl); | |
// JSON to array | |
$orderbook = json_decode(curl_exec($curl), true); | |
curl_close($curl); | |
return $orderbook; | |
} | |
function convertBrlToBtc($brlValue) | |
{ | |
// BCMath precision | |
bcscale(8); | |
// Assert using string because of BCMath | |
$brlValue = strval($brlValue); | |
// Copy $brlValue to calculate average later | |
$brlValueCopy = $brlValue; | |
$btcAmount = "0"; | |
$orderbook = getOrderbook(); | |
$len = sizeof($orderbook['asks']); | |
$i = 0; | |
while (bccomp($brlValue, "0") === 1 && $i < $len) { | |
$unitPrice = strval($orderbook['asks'][$i][0]); | |
$amount = strval($orderbook['asks'][$i][1]); | |
$portion = bcmul($unitPrice, $amount); | |
if (bccomp($brlValue, $portion) === -1) { | |
$btcAmount = bcadd($btcAmount, bcdiv($brlValue, $unitPrice)); | |
$brlValue = "0"; | |
} else { | |
$brlValue = bcsub($brlValue, $portion); | |
$btcAmount = bcadd($btcAmount, $amount); | |
} | |
$i++; | |
} | |
return array( | |
'amount' => $btcAmount, | |
'left' => $brlValue, | |
'average' => bcdiv(bcsub($brlValueCopy, $brlValue), $btcAmount) | |
); | |
} | |
function convertBtcToBrl($btcValue) | |
{ | |
// BCMath precision | |
bcscale(8); | |
// Assert using string because of BCMath | |
$btcValue = strval($btcValue); | |
// Copy $btcValue to calculate average later | |
$btcValueCopy = $btcValue; | |
$brlAmount = "0"; | |
$orderbook = getOrderbook(); | |
$len = sizeof($orderbook['bids']); | |
$i = 0; | |
while (bccomp($btcValue, "0") === 1 && $i < $len) { | |
$unitPrice = strval($orderbook['bids'][$i][0]); | |
$amount = strval($orderbook['bids'][$i][1]); | |
$portion = bcmul($unitPrice, $amount); | |
if (bccomp($btcValue, $amount) === -1) { | |
$brlAmount = bcadd($brlAmount, bcmul($unitPrice, $btcValue)); | |
$btcValue = "0"; | |
} else { | |
$btcValue = bcsub($btcValue, $amount); | |
$brlAmount = bcadd($brlAmount, $portion); | |
} | |
$i++; | |
} | |
return array( | |
'amount' => $brlAmount, | |
'left' => $btcValue, | |
'average' => bcdiv($brlAmount, bcsub($btcValueCopy, $btcValue)) | |
); | |
} | |
$brlValue = "1234.89"; | |
echo "Converting R$ ".$brlValue." to BTC:\n"; | |
print_r(convertBrlToBtc($brlValue)); | |
echo "\n"; | |
$btcValue = "0.87983321"; | |
echo "Converting ".$btcValue." BTC to R$:\n"; | |
print_r(convertBtcToBrl($btcValue)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment