diff --git a/src/Api.php b/src/Api.php index 9837ec4..2f4abf9 100644 --- a/src/Api.php +++ b/src/Api.php @@ -16,7 +16,7 @@ abstract class Api /** * @var string SDK Version */ - const VERSION = '1.1.11'; + const VERSION = '1.1.12'; /** * @var string diff --git a/src/PrivateApi/Margin.php b/src/PrivateApi/Margin.php new file mode 100644 index 0000000..821edb4 --- /dev/null +++ b/src/PrivateApi/Margin.php @@ -0,0 +1,304 @@ +call(Request::METHOD_GET, sprintf('/api/v1/mark-price/%s/current', $symbol)); + return $response->getApiData(); + } + + /** + * Get Margin Configuration Info. + * + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getConfig() + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/config'); + return $response->getApiData(); + } + + + /** + * Get Margin Account + * + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getAccount() + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/account'); + return $response->getApiData(); + } + + /** + * Post Borrow Order. + * + * @param array $params + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function borrow(array $params) + { + $response = $this->call(Request::METHOD_POST, '/api/v1/margin/borrow', $params); + return $response->getApiData(); + } + + /** + * Get Borrow Order + * + * @param string $orderId + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getBorrow($orderId) + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/borrow', compact('orderId')); + return $response->getApiData(); + } + + /** + * Get Repay Record. + * + * @param string $currency Currency + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getOutstanding($currency) + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/borrow/outstanding', compact('currency')); + return $response->getApiData(); + } + + /** + * Get Repayment Record. + * + * @param string $currency Currency + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getRepayRecord($currency) + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/borrow/repaid', compact('currency')); + return $response->getApiData(); + } + + /** + * One-Click Repayment + * + * @param array $params Params + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function repayAll(array $params) + { + $response = $this->call(Request::METHOD_POST, '/api/v1/margin/repay/all', $params); + return $response->getApiData(); + } + + /** + * Repay a Single Order + * + * @param array $params Params + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function repaySingle(array $params) + { + $response = $this->call(Request::METHOD_POST, '/api/v1/margin/repay/single', $params); + return $response->getApiData(); + } + + /** + * Post Lend Order. + * + * @param array $params + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function lend(array $params) + { + $response = $this->call(Request::METHOD_POST, '/api/v1/margin/lend', $params); + return $response->getApiData(); + } + + /** + * Cancel Lend Order. + * + * @param string $orderId Lend order ID + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function cancelLend($orderId) + { + $response = $this->call(Request::METHOD_DELETE, '/api/v1/margin/lend/' . $orderId); + return $response->getApiData(); + } + + /** + * Set Auto-lend. + * + * @param array $params + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function setAutoLend(array $params) + { + $response = $this->call(Request::METHOD_POST, '/api/v1/margin/toggle-auto-lend', $params); + return $response->getApiData(); + } + + /** + * Get Active Order. + * + * @param array $params + * @param array $pagination + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getLendActive(array $params, array $pagination = []) + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/lend/active', $params + $pagination); + return $response->getApiData(); + } + + /** + * Get Lent History. + * + * @param array $params + * @param array $pagination + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getLendDone(array $params, array $pagination = []) + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/lend/done', $params + $pagination); + return $response->getApiData(); + } + + /** + * Get Active Lend Order List. + * + * @param array $params + * @param array $pagination + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getUnsettled(array $params, array $pagination = []) + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/lend/trade/unsettled', $params + $pagination); + return $response->getApiData(); + } + + /** + * Get Settled Lend Order History + * + * @param array $params + * @param array $pagination + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getSettled(array $params, array $pagination = []) + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/lend/trade/settled', $params + $pagination); + return $response->getApiData(); + } + + /** + * Get Account Lend Record + * + * @param string $currency + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getLendAssets($currency) + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/lend/assets', compact('currency')); + return $response->getApiData(); + } + + /** + * Lending Market Data. + * + * @param array $params + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getMarket(array $params) + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/market', $params); + return $response->getApiData(); + } + + /** + * Margin Trade Data + * + * @param string $currency + * @return mixed + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function getTradeLast($currency) + { + $response = $this->call(Request::METHOD_GET, '/api/v1/margin/trade/last', compact('currency')); + return $response->getApiData(); + } +} \ No newline at end of file diff --git a/tests/MarginTest.php b/tests/MarginTest.php new file mode 100644 index 0000000..5cde11e --- /dev/null +++ b/tests/MarginTest.php @@ -0,0 +1,375 @@ +getMarkPrice('USDT-BTC'); + $this->assertArrayHasKey('symbol', $data); + $this->assertArrayHasKey('granularity', $data); + $this->assertArrayHasKey('timePoint', $data); + $this->assertArrayHasKey('value', $data); + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetConfig(Margin $api) + { + $data = $api->getConfig(); + $this->assertArrayHasKey('currencyList', $data); + $this->assertArrayHasKey('warningDebtRatio', $data); + $this->assertArrayHasKey('liqDebtRatio', $data); + $this->assertArrayHasKey('maxLeverage', $data); + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetAccount(Margin $api) + { + $data = $api->getAccount(); + $this->assertArrayHasKey('accounts', $data); + $this->assertArrayHasKey('debtRatio', $data); + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testBorrow(Margin $api) + { + $params = [ + 'currency' => 'USDT', + 'type' => 'FOK', + 'size' => 1, + ]; + $data = $api->borrow($params); + $this->assertInternalType('array', $data); + $this->assertArrayHasKey('orderId', $data); + $this->assertArrayHasKey('currency', $data); + $orderId = $data['orderId']; + $data = $api->getBorrow($orderId); + $this->assertInternalType('array', $data); + $this->assertArrayHasKey('currency', $data); + $this->assertArrayHasKey('orderId', $data); + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetBorrow(Margin $api) + { +// $data = $api->getBorrow('123456789'); +// $this->assertInternalType('array', $data); +// $this->assertArrayHasKey('currency', $data); +// $this->assertArrayHasKey('orderId', $data); + $this->assertTrue(true); + } + + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetOutstanding(Margin $api) + { + $data = $api->getOutstanding('USDT'); + $this->assertInternalType('array', $data); + $this->assertArrayHasKey('currentPage', $data); + $this->assertArrayHasKey('items', $data); + } + + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetRepayRecord(Margin $api) + { + $data = $api->getRepayRecord('USDT'); + $this->assertInternalType('array', $data); + $this->assertArrayHasKey('currentPage', $data); + $this->assertArrayHasKey('items', $data); + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testRepayAll(Margin $api) + { + $params = [ + 'currency' => 'USDT', + 'sequence' => 'RECENTLY_EXPIRE_FIRST', + 'size' => 1, + ]; + $data = $api->repayAll($params); + $this->assertNotNull(1); + } + + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testRepaySingle(Margin $api) + { + $params = [ + 'currency' => 'USDT', + 'tradeId' => '5da5a4f0f943c040c2f8501e', + 'size' => 1, + ]; + $data = $api->repaySingle($params); + $this->assertNotNull(1); + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testLend(Margin $api) + { + $params = [ + 'currency' => 'USDT', + 'size' => 1, + 'dailyIntRate' => '0.002', + 'term' => 7, + ]; +// $data = $api->lend($params); + $this->assertTrue(true); + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testCancelLend(Margin $api) + { +// $orderId = 1; +// $data = $api->cancelLend($orderId); +// $this->assertArrayHasKey('orderId', $data); + $this->assertTrue(true); + } + + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testSetAutoLend(Margin $api) + { + $params = [ + 'currency' => 'USDT', + 'isEnable' => 1, + 'retainSize' => 1, + 'dailyIntRate' => '0.002', + 'term' => 7, + ]; + $data = $api->setAutoLend($params); + $this->assertNull($data); + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetLendActive(Margin $api) + { + $data = $api->getLendActive(['currency' => 'BTC'], ['currentPage' => 1, 'pageSize' => 10]); + $this->assertPagination($data); + foreach ($data['items'] as $item) { + $this->assertArrayHasKey('orderId', $item); + $this->assertArrayHasKey('currency', $item); + $this->assertArrayHasKey('size', $item); + $this->assertArrayHasKey('filledSize', $item); + $this->assertArrayHasKey('dailyIntRate', $item); + $this->assertArrayHasKey('term', $item); + } + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetLendDone(Margin $api) + { + $data = $api->getLendDone(['currency' => 'BTC'], ['currentPage' => 1, 'pageSize' => 10]); + $this->assertPagination($data); + foreach ($data['items'] as $item) { + $this->assertArrayHasKey('orderId', $item); + $this->assertArrayHasKey('currency', $item); + $this->assertArrayHasKey('size', $item); + $this->assertArrayHasKey('filledSize', $item); + $this->assertArrayHasKey('dailyIntRate', $item); + $this->assertArrayHasKey('term', $item); + $this->assertArrayHasKey('status', $item); + } + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetUnsettled(Margin $api) + { + $data = $api->getUnsettled(['currency' => 'BTC'], ['currentPage' => 1, 'pageSize' => 10]); + $this->assertPagination($data); + foreach ($data['items'] as $item) { + $this->assertArrayHasKey('tradeId', $item); + $this->assertArrayHasKey('currency', $item); + $this->assertArrayHasKey('size', $item); + $this->assertArrayHasKey('dailyIntRate', $item); + $this->assertArrayHasKey('term', $item); + } + } + + + /** + * @dataProvider apiProvider + * @param Margin $api + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetSettled(Margin $api) + { + $data = $api->getSettled(['currency' => 'BTC'], ['currentPage' => 1, 'pageSize' => 10]); + $this->assertPagination($data); + foreach ($data['items'] as $item) { + $this->assertArrayHasKey('tradeId', $item); + $this->assertArrayHasKey('currency', $item); + $this->assertArrayHasKey('size', $item); + $this->assertArrayHasKey('dailyIntRate', $item); + $this->assertArrayHasKey('term', $item); + } + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetLendAssets(Margin $api) + { + $data = $api->getLendAssets('USDT'); + if (!empty($data[0])) { + $this->assertArrayHasKey('currency', $data[0]); + $this->assertArrayHasKey('outstanding', $data[0]); + $this->assertArrayHasKey('filledSize', $data[0]); + } + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetMarket(Margin $api) + { + $params = [ + 'currency' => 'BTC', + 'term' => 7, + ]; + $data = $api->getMarket($params); + if (!empty($data[0])) { + $this->assertArrayHasKey('dailyIntRate', $data[0]); + $this->assertArrayHasKey('term', $data[0]); + $this->assertArrayHasKey('size', $data[0]); + } + } + + /** + * @dataProvider apiProvider + * @param Margin $api + * @return array|string + * @throws \KuCoin\SDK\Exceptions\BusinessException + * @throws \KuCoin\SDK\Exceptions\HttpException + * @throws \KuCoin\SDK\Exceptions\InvalidApiUriException + */ + public function testGetTradeLast(Margin $api) + { + $data = $api->getTradeLast('USDT'); + if (!empty($data[0])) { + $this->assertArrayHasKey('tradeId', $data[0]); + $this->assertArrayHasKey('currency', $data[0]); + $this->assertArrayHasKey('size', $data[0]); + $this->assertArrayHasKey('dailyIntRate', $data[0]); + } + } +}