项目作者: cryptomkt

项目描述 :
CryptoMarket GoLang SDK
高级语言: Go
项目地址: git://github.com/cryptomkt/cryptomkt-go.git
创建时间: 2020-02-10T15:05:25Z
项目社区:https://github.com/cryptomkt/cryptomkt-go

开源协议:Apache License 2.0

下载


CryptoMarket-go

main page

sign up in CryptoMarket.

Installation

To install the Cryptomarket client

  1. go get github.com/cryptomkt/cryptomkt-go/v3

Documentation

This sdk makes use of the api version 3 of cryptomarket

Quick Start

rest client

  1. import (
  2. "context"
  3. "github.com/cryptomkt/cryptomkt-go/v3/args"
  4. "github.com/cryptomkt/cryptomkt-go/v3/rest"
  5. )
  6. // instance a client
  7. let apiKey="AB32B3201"
  8. let apiSecret="21b12401"
  9. // default window
  10. let window = 0
  11. // or some other window in miliseconds
  12. window = 25000
  13. client := rest.NewClient(apiKey, apiSecret, window)
  14. ctx := context.Background()
  15. // get currencies
  16. currencies, err := client.GetCurrencies(ctx)
  17. // get order books
  18. orderBook, err := client.GetOrderBook(ctx, args.Symbol("EOSETH"))
  19. // get your wallet balances
  20. accountBalanceList, err := client.GetWalletBalances(ctx)
  21. // get your trading balances
  22. tradingBalanceList, err := client.GetSpotTradingBalances(ctx)
  23. // move balance from wallet to spot trading
  24. result, err := client.TransferMoneyFromAccountBalanceToTradingBalance(
  25. ctx,
  26. args.Currency("ETH"),
  27. args.Amount("3.2"),
  28. args.Source(args.AccountWallet),
  29. args.Destination(args.AccountSpot),
  30. )
  31. // get your active orders
  32. ordersList, _ := client.GetAllActiveSpotOrders(ctx, args.Symbol("EOSETH"))
  33. // create a new order
  34. order, err := client.CreateSpotOrder(ctx, args.Symbol("EOSETH"), args.Side(args.SideTypeBuy), args.Quantity("10"), args.Price("10"))

websocket clients

there are three websocket clients, MarketDataClient, the SpotTradingClient and the WalletManagementClient. The MarketDataClient is public, while the others require authentication to be used.

websocket subscriptions make use of notification channels. To close the notification channel of a subscription, remember to call the corrensponding Unsubscription.

MarketDataClient

Unsubscription for the MarketDataClient are called from the subscription structure, as seen in the examples. Keep in mind that this stop the client from processing the messages, but the server will continue to send them. To completely stop recieving messages is recomended to close the MarketDataClient.

  1. // instance a client
  2. client, err := NewMarketDataClient()
  3. // close the client
  4. defer client.Close()
  5. // subscribe to public trades
  6. subscription, err := client.SubscribeToTrades(
  7. args.Symbols([]string{"EOSETH", "ETHBTC"}),
  8. args.Limit(10),
  9. )
  10. subscribedSymbols := subscription.Symbols
  11. go func() {
  12. for notification := range subscription.NotificationCh {
  13. notificationType := notification.NotificationType
  14. if notificationType == args.NotificationSnapshot {
  15. fmt.Println('is a snapshot')
  16. }
  17. if notificationType == args.NotificationUpdate {
  18. fmt.Println('is an update')
  19. }
  20. for _, tradeList := range notification.Data {
  21. for _, trade := range tradeList {
  22. fmt.Println(trade)
  23. }
  24. }
  25. }
  26. }()
  27. // unsubscribe
  28. UnsubscribeTo(subscription.NotificationChannel)
  29. // subscribe to symbol tickers
  30. subscription, err = client.SubscribeToTicker(
  31. args.Symbols([]string{"EOSETH"}),
  32. args.TickerSpeed(args.TickerSpeed1s),
  33. )
  34. go func() {
  35. for notification := range subscription.NotificationCh {
  36. notificationType := notification.NotificationType
  37. if notificationType == args.NotificationData {
  38. fmt.Println('is always a data notification')
  39. }
  40. fmt.Println("tickers")
  41. for symbol, ticker := range notification.Data {
  42. fmt.Println("["+symbol+"]="+ ticker)
  43. }
  44. }
  45. }()
  46. // unsubscribe
  47. UnsubscribeTo(subscription.NotificationChannel)

SpotTradingClient

  1. // instance a client with default window of 10 seconds
  2. client, err := NewSpotTradingClient(APIKey, APISecret, 0)
  3. // close the client
  4. defer client.Close()
  5. // subscribe to order reports
  6. notificationCh, err := client.SubscribeToReports()
  7. go func() {
  8. for notification := range notificationCh {
  9. for _, report := range notification.Data {
  10. fmt.Println(report)
  11. }
  12. }
  13. }()
  14. // unsubscribe from order reports
  15. client.UnsubscribeToReports()
  16. clientOrderID := fmt.Sprint(time.Now().Unix())
  17. // create an order
  18. client.CreateSpotOrder(
  19. context.Background(),
  20. args.Symbol("EOSETH"),
  21. args.Side(args.SideSell),
  22. args.Price("1000"),
  23. args.Quantity("0.01"),
  24. args.ClientOrderID(clientOrderID),
  25. )
  26. // candel an order
  27. client.CancelSpotOrder(
  28. context.Background(),
  29. args.ClientOrderID(clientOrderID),
  30. )

WalletManagementClient

  1. // instance a client with 20 seconds of window
  2. client, err := NewWalletManagementClient(APIKey, APISecret, 20_000)
  3. // close the client
  4. defer client.Close()
  5. // subscribe to wallet transactions
  6. notificationCh, err := client.SubscribeToTransactions()
  7. go func() {
  8. for notification := range notificationCh {
  9. transaction := notification.Data
  10. fmt.Println(transaction)
  11. }
  12. }()
  13. // unsubscribe from wallet transactions
  14. err = client.UnsubscribeToTransactions()
  15. // get wallet balances
  16. balances, err := client.GetWalletBalances(context.Background())
  17. for _, balance := range balances {
  18. fmt.Println(balance)
  19. }
  20. // transfer assets from wallet account and spot account
  21. restClient.TransferBetweenWalletAndExchange(
  22. context.Background(),
  23. args.Amount("0.2"),
  24. args.Currency("EOS"),
  25. args.Source(args.AccountWallet),
  26. args.Destination(args.AccountSpot),
  27. )

arguments and constants of interest

all the arguments for the clients are in the args package, as well as the custom types for the arguments. check the package documentation, and the method documentation of the clients for more info.

Checkout our other SDKs

python sdk

nodejs sdk

java sdk

ruby sdk