Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from 1bazinga25/master
Browse files Browse the repository at this point in the history
init kucoin api test demo
  • Loading branch information
Ive20 authored Apr 9, 2020
2 parents 036d1e1 + 277efa6 commit 00e76c3
Show file tree
Hide file tree
Showing 20 changed files with 2,213 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/config_dev.json
/config_prod.json
/.idea/
174 changes: 173 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,173 @@
# kucoin-api-demo
# Test Tool for KuCoin API

## Choose environment

| Environment | BaseUri |
| ------------ | ------------------------------------------------------------ |
| *Production* | `https://api.kucoin.com(DEFAULT)` `https://openapi-v2.kucoin.com` `https://api.kcs.top` |
| *Sandbox* | `https://openapi-sandbox.kucoin.com` |

## Example

* First,you should wirte your api information into config.json.

```json
{
"ApiBaseURI":"",
"ApiKey": "",
"ApiSecret": "",
"ApiPassphrase": "",
"ApiSkipVerifyTls": false
}
```

* If not, you can run this script and follow the guide,then input your api information, such as:

```shell
# please input api base URI,such as:https://api.kucoin.com
% https://api.kucoin.com
# please input your api key...
% input your api key
# please input your api secret...
% input your api secret
# please input your api passphrase...
% input your api passphrase
```

* input the api name and parameter information to choose api which you want to test.

```shell
# please input the api name which you want to test
% Accounts
# please input the api parameters,use ',' to spilt parameters.
# if it is no parameter api,just enter to skip...
% BTC,main
```

* Then,it will show the request and response body to you.

## Api map list

<details>
<summary>Account</summary>

| API Name | Request | Parameter | Parameter Example | Description |
| -------- | -------- | -------- | -------- | -------- |
| CreateAccount | POST /api/v1/accounts | type, currency | main,BTC | https://docs.kucoin.com/#create-an-account |
| Accounts | GET /api/v1/accounts | currency,type | BTC,main | https://docs.kucoin.com/#list-accounts |
| Account | GET /api/v1/accounts/{accountId} | accountId | {account ID} | https://docs.kucoin.com/#get-an-account |
| SubAccountUsers | GET /api/v1/sub/user | | | https://docs.kucoin.com/#get-user-info-of-all-sub-accounts |
| SubAccounts | GET /api/v1/sub-accounts | | | https://docs.kucoin.com/#get-the-aggregated-balance-of-all-sub-accounts |
| SubAccount | GET /api/v1/sub-accounts/{subUserId} | subUserId |{sub-account ID} | https://docs.kucoin.com/#get-account-balance-of-a-sub-account |
| AccountLedgers | GET /api/v1/accounts/{accountId}/ledgers | accountId |{account ID} | https://docs.kucoin.com/#get-account-ledgers |
| AccountHolds | GET /api/v1/accounts/{accountId}/holds | accountId | {account ID} | https://docs.kucoin.com/#get-holds |
| InnerTransferV2 | POST /api/v2/accounts/inner-transfer | Currency,from,to,amount | KCS,main,trade,2 | https://docs.kucoin.com/#inner-transfer |

</details>

<details>
<summary>Deposit</summary>

| API Name | Request | Parameter | Parameter Example | Description |
| -------- | -------- | -------- | -------- | -------- |
| CreateDepositAddress | POST /api/v1/deposit-addresses | currency | BTC | https://docs.kucoin.com/#create-deposit-address |
| DepositAddresses | GET /api/v1/deposit-addresses | currency | BTC | https://docs.kucoin.com/#get-deposit-address |
| V1Deposits | GET /api/v1/hist-deposits | currency | KCS | https://docs.kucoin.com/#get-v1-historical-deposits-list |
| Deposits | GET /api/v1/deposits | currency | KCS | https://docs.kucoin.com/#get-deposit-list |

</details>

<details>
<summary>Fill</summary>

| API Name | Request | Parameter | Parameter Example | Description |
| -------- | -------- | -------- | -------- | -------- |
| Fills | GET /api/v1/fills | tradeType | TRADE |https://docs.kucoin.com/#list-fills|
| RecentFills | GET /api/v1/limit/fills | | |https://docs.kucoin.com/#recent-fills|

</details>

<details>
<summary>Order</summary>

| API Name | Request | Parameter | Parameter Example | Description |
| -------- | -------- | -------- | -------- | -------- |
| CreateOrder | POST /api/v1/orders | side,symbol,price,size | sell,BTC-USDT,7330,0.1 |https://docs.kucoin.com/#place-a-new-order|
| CreateMultiOrder | POST /api/v1/orders/multi | symbol,side,price,size | BTC-USDT,sell,7350,0.01 |https://docs.kucoin.com/#place-bulk-orders|
| CancelOrder | DELETE /api/v1/orders/{orderId} | orderId | {order ID} |https://docs.kucoin.com/#cancel-an-order|
| CancelOrders | DELETE /api/v1/orders | symbol | BTC-USDT |https://docs.kucoin.com/#cancel-all-orders|
| Orders | GET /api/v1/orders | symbol | BTC-USDT,sell |https://docs.kucoin.com/#list-orders|
| Order | GET /api/v1/orders/{order-id} | orderId | {order ID} |https://docs.kucoin.com/#get-an-order|
| RecentOrders | GET /api/v1/limit/orders | | |https://docs.kucoin.com/#recent-orders|

</details>

<details>
<summary>WebSocket Feed</summary>

| API Name | Request | Parameter | Parameter Example | Description |
| -------- | -------- | -------- | -------- | -------- |
| WebSocketPublicToken | POST /api/v1/bullet-public | | | https://docs.kucoin.com/#apply-connect-token |
| WebSocketPrivateToken | POST /api/v1/bullet-private | | | https://docs.kucoin.com/#apply-connect-token |
| NewWebSocketClient | | | | https://docs.kucoin.com/#websocket-feed |

</details>

<details>
<summary>Withdrawal</summary>

| API Name | Request | Parameter | Parameter Example | Description |
| -------- | -------- | -------- | -------- | -------- |
| WithdrawalQuotas | GET /api/v1/withdrawals/quotas | currency | BTC | https://docs.kucoin.com/#get-withdrawal-quotas |
| Withdrawals | GET /api/v1/withdrawals | | | https://docs.kucoin.com/#get-withdrawals-list |
| ApplyWithdrawal | POST /api/v1/withdrawals | currency, address,amount | KCS,{address},0.1 | https://docs.kucoin.com/#apply-withdraw-2 |
| CancelWithdrawal | DELETE /api/v1/withdrawals/{withdrawalId} | withdrawalId | {withdrawal ID} | https://docs.kucoin.com/#cancel-withdrawal |

</details>

<details>
<summary>Currency</summary>

| API Name | Request | Parameter | Parameter Example | Description |
| -------- | -------- | -------- | -------- | -------- |
| Currencies | GET /api/v1/currencies | | | https://docs.kucoin.com/#get-currencies |
| Currency | GET /api/v1/currencies/BTC | currency | BTC | https://docs.kucoin.com/#get-currency-detail |
| Prices | GET /api/v1/prices | base, currencies | USD,KCS | https://docs.kucoin.com/#get-fiat-price |

</details>

<details>
<summary>Symbol</summary>

| API Name | Request | Parameter | Parameter Example | Description |
| -------- | -------- | -------- | -------- | -------- |
| Symbols | GET /api/v1/symbols | | | https://docs.kucoin.com/#get-symbols-list |
| TickerLevel1 | GET /api/v1/market/orderbook/level1 | symbol | BTC-USDT | https://docs.kucoin.com/#get-ticker |
| Tickers | GET /api/v1/market/allTickers | | | https://docs.kucoin.com/#get-all-tickers |
| AggregatedPartOrderBook | GET /api/v1/market/orderbook/level2_{depth} | symbol, depth | BTC-USDT,20 | https://docs.kucoin.com/#get-part-order-book-aggregated |
| AggregatedFullOrderBook | GET /api/v2/market/orderbook/level2 | symbol | BTC-USDT |https://docs.kucoin.com/#get-full-order-book-aggregated|
| AtomicFullOrderBook | GET GET /api/v1/market/orderbook/level3 | symbol | BTC-USDT | https://docs.kucoin.com/#get-full-order-book-atomic |
| TradeHistories | GET /api/v1/market/histories | symbol | BTC-USDT | https://docs.kucoin.com/#get-trade-histories |
| KLines | GET /api/v1/market/candles | symbol, type | BTC-USDT,30min | https://docs.kucoin.com/#get-klines |
| Stats24hr | GET /api/v1/market/stats | symbol | BTC-USDT | https://docs.kucoin.com/#get-24hr-stats |
| Markets | GET /api/v1/markets | | | https://docs.kucoin.com/#get-market-list |

</details>

<details>
<summary>Time</summary>

| API Name | Request | Parameter | Parameter Example | Description |
| -------- | -------- | -------- | -------- | -------- |
| ServerTime | GET /api/v1/timestamp | | | https://docs.kucoin.com/#server-time |

</details>

<details>
<summary>Service Status</summary>

| API Name | Request | Parameter | Parameter Example | Description |
| -------- | -------- | -------- | -------- | -------- |
| ServiceStatus | GET /api/v1/status | | | https://docs.kucoin.com/#service-status |

</details>
7 changes: 7 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ApiBaseURI":"",
"ApiKey": "",
"ApiSecret": "",
"ApiPassphrase": "",
"ApiSkipVerifyTls": false
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module kucoin-api-test

go 1.14

require (
github.com/Kucoin/kucoin-go-sdk v1.2.6
github.com/gorilla/websocket v1.4.0
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.4.1
)
195 changes: 195 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package main

import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"kucoin-api-test/sdk"
"log"
"os"
"reflect"
"strings"
)

var (
kucoin *sdk.ApiService
PATH = "./config.json.example"
)

func main() {
//1. get api authentication info from user input or configuration file.
var apiURI, apiKey, apiSecret, apiPassphrase string
var apiSkipVerifyTls bool

// read config from json file
file, err := ioutil.ReadFile(PATH)
if err != nil {
log.Fatal("error when read configuration from json file:", err)
return
}
var config ApiConfig
// unmarshal config data
err = json.Unmarshal(file, &config)
if err != nil {
log.Fatal("error when unmarshal config from json file:", err)
return
}
// if we have configuration from json file
if len(config.ApiBaseURI) > 0 {
apiURI = config.ApiBaseURI
} else {
log.Println("please input api base URI,such as:https://api.kumex.com")
fmt.Scanf("%s", &apiURI)
}

if len(config.ApiKey) > 0 {
apiURI = config.ApiKey
} else {
log.Println("please input your api key...")
fmt.Scanf("%s", &apiKey)
}

if len(config.ApiSecret) > 0 {
apiURI = config.ApiSecret
} else {
log.Println("please input your api secret...")
fmt.Scanf("%s", &apiSecret)
}

if len(config.ApiPassphrase) > 0 {
apiURI = config.ApiPassphrase
} else {
log.Println("please input your api passphrase...")
fmt.Scanf("%s", &apiPassphrase)
}

apiSkipVerifyTls = config.ApiSkipVerifyTls

initApiService(apiURI, apiKey, apiSecret, apiPassphrase, apiSkipVerifyTls)

// 2. api function map to user input.
apiMap := initApiMap()
// 3. get api info from user input.
var apiName string
log.Println("please input the api name which you want to test...")
fmt.Scanf("%s", &apiName)
log.Println("please input the api parameters,use ',' to spilt parameters.")
log.Println("if it is no parameter api,just enter to skip...")

reader := bufio.NewReader(os.Stdin)
inputs, err := reader.ReadString('\n')
if err != nil {
log.Fatal("input parameter error:", err)
}

var params []string
if len(strings.TrimSpace(inputs)) > 0 {
params = strings.Split(strings.TrimSpace(inputs), ",")
}
// 4. call api function which user choose to test.
call(apiMap, apiName, params)
}

type ApiConfig struct {
ApiBaseURI string `json:ApiBaseURI`
ApiKey string `json:ApiKey`
ApiSecret string `json:ApiSecret`
ApiPassphrase string `json:ApiPassphrase`
ApiSkipVerifyTls bool `json:ApiSkipVerifyTls`
}

// initApiService init an api service
func initApiService(apiURI, apiKey, apiSecret, apiPassphrase string, apiSkipVerifyTls bool) {
kucoin = sdk.NewApiService(
sdk.ApiBaseURIOption(apiURI),
sdk.ApiKeyOption(apiKey),
sdk.ApiSecretOption(apiSecret),
sdk.ApiPassPhraseOption(apiPassphrase),
sdk.ApiSkipVerifyTlsOption(apiSkipVerifyTls),
)
sdk.DebugMode = true
}

// call according to string input to call function which named the input.
func call(m map[string]interface{}, name string, params []string) {
// if string input is a function
f := reflect.ValueOf(m[name])
if f.Kind() != reflect.Func {
log.Fatal("input error,your input is not a name of api.")
return
}

in := make([]reflect.Value, len(params))
// if the function has no parameters
if len(params) > 0 {
for k, v := range params {
fmt.Println(v)
in[k] = reflect.ValueOf(v)
}
} else {
in = nil
}

f.Call(in)
}

// initApiMap init api function mapping
func initApiMap() map[string]interface{} {
return map[string]interface{}{
"CreateAccount": kucoin.CreateAccount,
"Accounts": kucoin.Accounts,
"Account": kucoin.Account,
"SubAccountUsers": kucoin.SubAccountUsers,
"SubAccounts": kucoin.SubAccounts,
"SubAccount": kucoin.SubAccount,
"AccountLedgers": kucoin.AccountLedgers,
"AccountHolds": kucoin.AccountHolds,

"InnerTransferV2": kucoin.InnerTransferV2,
"SubTransfer": kucoin.SubTransfer,
"CreateDepositAddress": kucoin.CreateDepositAddress,
"DepositAddresses": kucoin.DepositAddresses,
"V1Deposits": kucoin.V1Deposits,
"Deposits": kucoin.Deposits,

"Fills": kucoin.Fills,
"RecentFills": kucoin.RecentFills,

"CreateOrder": kucoin.CreateOrder,
"CreateMultiOrder": kucoin.CreateMultiOrder,
"CancelOrder": kucoin.CancelOrder,
"CancelOrders": kucoin.CancelOrders,
"Orders": kucoin.Orders,
"Order": kucoin.Order,
"RecentOrders": kucoin.RecentOrders,

"WebSocketPublicToken": kucoin.WebSocketPublicToken,
"WebSocketPrivateToken": kucoin.WebSocketPrivateToken,
"NewWebSocketClient": kucoin.NewWebSocketClient,

"WithdrawalQuotas": kucoin.WithdrawalQuotas,
"Withdrawals": kucoin.Withdrawals,
"ApplyWithdrawal": kucoin.ApplyWithdrawal,
"CancelWithdrawal": kucoin.CancelWithdrawal,

"Currencies": kucoin.Currencies,
"Currency": kucoin.Currency,
"Prices": kucoin.Prices,

"Symbols": kucoin.Symbols,
"TickerLevel1": kucoin.TickerLevel1,
"Tickers": kucoin.Tickers,
"AggregatedPartOrderBook": kucoin.AggregatedPartOrderBook,
"AggregatedFullOrderBook": kucoin.AggregatedFullOrderBook,
"AtomicFullOrderBook": kucoin.AtomicFullOrderBook,
"TradeHistories": kucoin.TradeHistories,
"KLines": kucoin.KLines,
"Stats24hr": kucoin.Stats24hr,
"Markets": kucoin.Markets,

"ServerTime": kucoin.ServerTime,

"ServiceStatus": kucoin.ServiceStatus,
}
}
Loading

0 comments on commit 00e76c3

Please sign in to comment.