From 9d2b613f870e365a5a5aeeb006f36241c7f56bd7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A3=89=E8=A2=AB=E8=9F=B2?=
<37621229+Penny13692018@users.noreply.github.com>
Date: Thu, 12 Dec 2019 19:05:05 +0800
Subject: [PATCH 1/2] Add ledgers Function
Can get 25 of account history, while the get_balances() is changed. Use "funding" wallet for margin funding
---
bitfinex.v2.class.php | 92 +++++++++++++++++++++++++++++++++----------
1 file changed, 72 insertions(+), 20 deletions(-)
diff --git a/bitfinex.v2.class.php b/bitfinex.v2.class.php
index 0da43ad..892529e 100644
--- a/bitfinex.v2.class.php
+++ b/bitfinex.v2.class.php
@@ -1,16 +1,16 @@
api_key=$api_key;
$this->api_secret=$api_secret;
}
@@ -18,10 +18,10 @@ public function __construct($api_key, $api_secret) {
/* Core api request functions */
/* Build BFX Headers for v2 API */
- private function headers($path)
+ private function headers($path, $postdata)
{
$nonce =(string) number_format(round(microtime(true) * 100000), 0, ".", "");
- $body ="{}";
+ $body =$postdata;
$signature ="/api/v2".$path["request"].$nonce.$body;
$h =hash_hmac("sha384", utf8_encode($signature), utf8_encode($this->api_secret));
@@ -35,23 +35,42 @@ private function headers($path)
}
/* Authenticated Endpoints Request */
- private function send_auth_endpoint_request($data) {
+ private function send_auth_endpoint_request($data, $postdata) {
$ch=curl_init();
$url=self::API_URL.$data["request"];
- $headers=$this->headers($data);
-
+ $headers=$this->headers($data, $postdata);
+
+ if($postdata==false)
+ {
+ $postdata="{}";
+ //echo 'Error to Here 1';
+ $postdata2 ='{}';
+ }
+ else if($postdata=="{}")
+ {
+ $postdata="{}";
+ //echo 'Error to Here 2';
+ $postdata2 ='{}';
+ }
+ else
+ {
+ $postdata2 = http_build_query($postdata);
+ //echo $postdata2."
";
+ }
+
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::API_TIMEOUT);
- curl_setopt($ch, CURLOPT_POSTFIELDS, "{}");
-
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata2);
+
if(!$result=curl_exec($ch)) {
return $this->curl_error($ch);
} else {
- return $this->output($result, $this->is_bitfinex_error($ch), $data);
+ //下方有一個output函數有修改過
+ return $this->output($result, $this->is_bitfinex_error($ch), $data, $postdata);
}
}
@@ -101,18 +120,23 @@ private function is_bitfinex_error($ch) {
}
/* Retrieve CURL response, if API err or RATE LIMIT hit, recall routine. Need to implement max retries. */
- private function output($result, $is_error=false, $command) {
+ private function output($result, $is_error=false, $command, $postdata) {
$response=json_decode($result, true);
if ($response[0]=="error" or $response["error"]=="ERR_RATE_LIMIT") {
if (!is_array($command)) {
- //echo "Retrying... '".$command."' in 10 seconds.\n";
+ //echo "Retrying... '".$command."' in 10 seconds.
";
+ //echo "First Type Error:".$response["error"]."
";
+ //echo "First Type Error:".$response[0]."
";
sleep(10);
return $this->send_public_endpoint_request($command);
} else {
- //echo "Retrying... '".$command["request"]."' in 10 seconds.\n";
+ //echo "Retrying... '".$command["request"]."' in 10 seconds.
";
+ //echo "Second Type Error:".$response["error"]."
";
+ //echo "Second Type Error:".$response[0]."
";
sleep(10);
- return $this->send_auth_endpoint_request($command);
+ //return $this->send_auth_endpoint_request($command, $postdata);
+ return $this->send_auth_endpoint_request($command,"{}");
}
} else {
if ($is_error) {
@@ -168,7 +192,7 @@ public function get_orders() {
$request=$this->build_url_path("auth/r/orders");
$data=array("request" => $request);
- $orders=$this->send_auth_endpoint_request($data);
+ $orders=$this->send_auth_endpoint_request($data,"{}");
$o=array();
for ($z=0; $zbuild_url_path("auth/r/wallets");
$data=array("request" => $request);
-
- $balances=$this->send_auth_endpoint_request($data);
+
+ $balances=$this->send_auth_endpoint_request($data,'{}');
$b=array();
$count=0;
for ($z=0; $zbuild_url_path("auth/r/orders/".$symbol."/hist");
$data=array("request" => $request);
- $balances=$this->send_auth_endpoint_request($data);
+ $balances=$this->send_auth_endpoint_request($data,'{}');
//format data
$b=array();
$count=0;
@@ -239,5 +263,33 @@ public function get_orderhist($symbol){
}
return $b;
}
+
+ /* API: Get Ledgers - by symbol */
+ public function get_ledgers($symbol){
+ $request=$this->build_url_path("auth/r/ledgers/".$symbol."/hist");
+ //$request=$this->build_url_path("auth/r/ledgers/hist");
+ $data=array("request" => $request);
+ $limit = 30;
+
+ $ledgerDetails = array('limit' => $limit);
+
+ $balances=$this->send_auth_endpoint_request($data,$ledgerDetails);
+ //format data
+ $b=array();
+ $count=0;
+ for ($z=0; $z
From d02d978cb600f2fa949e6b632e8f087a76afe289 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A3=89=E8=A2=AB=E8=9F=B2?=
<37621229+Penny13692018@users.noreply.github.com>
Date: Thu, 12 Dec 2019 19:15:17 +0800
Subject: [PATCH 2/2] Trimmed to be closer to original
Add ledger function, while unable to get more balance history.
Please help improve it, thanks.
---
bitfinex.v2.class.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/bitfinex.v2.class.php b/bitfinex.v2.class.php
index 892529e..37e0d91 100644
--- a/bitfinex.v2.class.php
+++ b/bitfinex.v2.class.php
@@ -1,7 +1,7 @@
api_key=$api_key;
$this->api_secret=$api_secret;
}