From 1cbcb03b35bd9b9207431aa70ffb33bea9a4b1f8 Mon Sep 17 00:00:00 2001 From: zhouaini528 <465382251@qq.com> Date: Wed, 20 Dec 2023 11:21:58 +0800 Subject: [PATCH] add Bybit --- src/Exchanges/Bybit.php | 145 ++++++++++------------------------------ tests/bybit.php | 20 +++--- 2 files changed, 44 insertions(+), 121 deletions(-) diff --git a/src/Exchanges/Bybit.php b/src/Exchanges/Bybit.php index 59eb890..2ad1bf9 100644 --- a/src/Exchanges/Bybit.php +++ b/src/Exchanges/Bybit.php @@ -5,9 +5,7 @@ namespace Lin\Exchange\Exchanges; - -use Lin\Bybit\BybitInverse; -use Lin\Bybit\BybitLinear; +use Lin\Bybit\BybitV5; use Lin\Exchange\Interfaces\AccountInterface; use Lin\Exchange\Interfaces\MarketInterface; use Lin\Exchange\Interfaces\TraderInterface; @@ -16,20 +14,14 @@ class BaseBybit { protected $platform_linear; protected $platform_inverse; - protected $host; - function __construct(BybitLinear $platform_linear=null,BybitInverse $platform_inverse=null,$host){ - $this->platform_linear=$platform_linear; - $this->platform_inverse=$platform_inverse; - $this->host=$host; - } + protected $platform_v5; - protected function checkType(){ - if(stripos($this->host,'bybitlinear')!==false){ - return 'bybitlinear'; - } + protected $host; - return 'bybitinverse'; + function __construct(BybitV5 $platform_v5,$host){ + $this->platform_v5=$platform_v5; + $this->host=$host; } } @@ -38,16 +30,7 @@ class AccountBybit extends BaseBybit implements AccountInterface /** * * */ - function get(array $data){ - switch ($this->checkType()){ - case 'bybitinverse':{ - return ; - } - case 'bybitlinear':{ - return ; - } - } - } + function get(array $data){} } class MarketBybit extends BaseBybit implements MarketInterface @@ -56,14 +39,6 @@ class MarketBybit extends BaseBybit implements MarketInterface * * */ function depth(array $data){ - switch ($this->checkType()){ - case 'bybitinverse':{ - return ; - } - case 'bybitlinear':{ - return ; - } - } } } @@ -73,84 +48,36 @@ class TraderBybit extends BaseBybit implements TraderInterface * * */ function sell(array $data){ - switch ($this->checkType()){ - case 'bybitinverse':{ - return ; - } - case 'bybitlinear':{ - return ; - } - } } /** * * */ function buy(array $data){ - switch ($this->checkType()){ - case 'bybitinverse':{ - return ; - } - case 'bybitlinear':{ - return ; - } - } } /** * * */ function cancel(array $data){ - switch ($this->checkType()){ - case 'bybitinverse':{ - return ; - } - case 'bybitlinear':{ - return ; - } - } } /** * * */ function update(array $data){ - switch ($this->checkType()){ - case 'bybitinverse':{ - return ; - } - case 'bybitlinear':{ - return ; - } - } } /** * * */ function show(array $data){ - switch ($this->checkType()){ - case 'bybitinverse':{ - return $this->platform_inverse->order()->get($data); - } - case 'bybitlinear':{ - return $this->platform_linear->order()->get($data); - } - } } /** * * */ function showAll(array $data){ - switch ($this->checkType()){ - case 'bybitinverse':{ - return ; - } - case 'bybitlinear':{ - return ; - } - } } } @@ -158,70 +85,66 @@ class Bybit { private $key; private $secret; - private $passphrase; private $host; protected $type; - protected $platform_inverse; - protected $platform_linear; + protected $platform=''; - function __construct($key,$secret,$passphrase,$host=''){ + protected $platform_v5; + + function __construct($key,$secret,$host=''){ $this->key=$key; $this->secret=$secret; - $this->passphrase=$passphrase; $this->host=empty($host) ? 'https://api.bybit.com' : $host ; $this->getPlatform(); } function account(){ - return new AccountBybit($this->platform_linear,$this->platform_inverse,$this->host); + return new AccountBybit($this->platform_v5,$this->host); } function market(){ - return new MarketBybit($this->platform_linear,$this->platform_inverse,$this->host); + return new MarketBybit($this->platform_v5,$this->host); } function trader(){ - return new TraderBybit($this->platform_linear,$this->platform_inverse,$this->host); + return new TraderBybit($this->platform_v5,$this->host); } function getPlatform(string $type=''){ $this->type=strtolower($type); switch ($this->type){ - case 'bybitinverse':{ - return $this->platform_inverse=new BybitInverse($this->key,$this->secret,$this->host); - } - case 'bybitlinear':{ - return $this->platform_linear=new BybitLinear($this->key,$this->secret,$this->host); - } default:{ - if(stripos($this->host,'bybitlinear')!==false){ - $this->type='bybitlinear'; - return $this->platform_linear=new BybitLinear($this->key,$this->secret,$this->host); - } - $this->type='bybitinverse'; - return $this->platform_inverse=new BybitInverse($this->key,$this->secret,$this->host); + return $this->platform_v5=new BybitV5($this->key,$this->secret,$this->host); } } } + /** + Set exchange transaction category, default "spot" transaction. Other options "spot" "margin" "future" "swap" + */ + public function setPlatform(string $platform=''){ + $this->platform=$platform ?? 'spot'; + return $this; + } + + /** + Set exchange API interface version. for example "v1" "v3" "v5" + */ + public function setVersion(string $version=''){ + $this->version=$version; + return $this; + } + + /** * Support for more request Settings * */ function setOptions(array $options=[]){ - - switch ($this->type){ - case 'bybitinverse':{ - $this->platform_inverse->setOptions($options); - break; - } - case 'bybitlinear':{ - $this->platform_linear->setOptions($options); - break; - } - } + $this->options=$options; + return $this; } } diff --git a/tests/bybit.php b/tests/bybit.php index 1ad73e5..aa88e00 100644 --- a/tests/bybit.php +++ b/tests/bybit.php @@ -22,16 +22,6 @@ //Set the request timeout to 60 seconds by default 'timeout'=>10, - //If you are developing locally and need an agent, you can set this - //'proxy'=>true, - //More flexible Settings - /* 'proxy'=>[ - 'http' => 'http://127.0.0.1:12333', - 'https' => 'http://127.0.0.1:12333', - 'no' => ['.cn'] - ], */ - //Close the certificate - //'verify'=>false, ]); $action=intval($_GET['action'] ?? 0);//http pattern @@ -473,6 +463,16 @@ } + case 2001:{ + $result=$exchanges->getPlatform()->position()->getList([ + 'category'=>'linear', + 'symbol'=>'BTCUSDT', + ]); + print_r($result); + break; + } + + default:{ echo 'nothing'; //exit;