go-gdax, 转到 Coinbase GDAX API的( golang ) 客户端

分享于 

5分钟阅读

GitHub

  繁體 雙語
Go (golang) Client for the Coinbase Exchange API https://exchange.coinbase.com
  • 源代码名称:go-gdax
  • 源代码网址:http://www.github.com/preichenberger/go-gdax
  • go-gdax源代码文档
  • go-gdax源代码下载
  • Git URL:
    git://www.github.com/preichenberger/go-gdax.git
    Git Clone代码到本地:
    git clone http://www.github.com/preichenberger/go-gdax
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/preichenberger/go-gdax
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    GDAX GoDocBuild Status

    摘要

    转到 GDAX的客户端

    安装

    go get github.com/preichenberger/go-gdax

    文档

    有关功能的完整详细信息,请参见 GoDoc 文档。

    设置

    如何创建客户端:

    import (
     "os" gdax "github.com/preichenberger/go-gdax")secret:= os.Getenv("COINBASE_SECRET")key:= os.Getenv("COINBASE_KEY")passphrase:= os.Getenv("COINBASE_PASSPHRASE")// or unsafe hardcode waysecret = "exposedsecret"key = "exposedkey"passphrase = "exposedpassphrase"client:= gdax.NewClient(secret, key, passphrase)

    HTTP设置

    import (
     "net/http""time")
    client.HttpClient = &http.Client {
     Timeout: 15 * time.Second,
    }

    光标

    这个库使用游标 Pattern,所以你不必跟踪分页。

    varorders []gdax.Ordercursor = client.ListOrders()for cursor.HasMore {
     iferr:= cursor.NextPage(&orders); err!= nil {
     println(err.Error())
     return }
     for_, o:=range orders {
     println(o.Id)
     }
    }

    web sockets

    监听 web socket消息

    import(
     ws "github.com/gorilla/websocket" )
     varwsDialer ws.DialerwsConn, _, err:= wsDialer.Dial("wss://ws-feed.gdax.com", nil)
     if err!= nil {
     println(err.Error())
     }
     subscribe:= gdax.Message{
     Type: "subscribe",
     Channels: []gdax.MessageChannel{
     gdax.MessageChannel{
     Name: "level2",
     ProductIds: []string{
     "BTC-USD",
     },
     },
     },
     }
     iferr:= wsConn.WriteJSON(subscribe); err!= nil {
     println(err.Error())
     }
     message:= gdax.Message{}
     fortrue {
     iferr:= wsConn.ReadJSON(&message); err!= nil {
     println(err.Error())
     break }
     if message.Type == "match" {
     println("Got a match")
     }
     }

    时间

    结果返回coinbase时间类型,它处理GDAX返回的不同类型的时间解析。 这将包装本机时间类型

    import(
     "time" gdax "github.com/preichenberger/go-gdax" )
     coinbaseTime:= gdax.Time{}
     println(time.Time(coinbaseTime).Day())

    示例

    这里库支持所有 public 和 private 端点

    获取帐户:

    accounts, err:= client.GetAccounts()
     if err!= nil {
     println(err.Error())
     }
     for_, a:=range accounts {
     println(a.Balance)
     }

    列出账户分类帐:

    varledger []gdax.LedgerEntryaccounts, err:= client.GetAccounts()
     if err!= nil {
     println(err.Error())
     }
     for_, a:=range accounts {
     cursor:= client.ListAccountLedger(a.Id)
     for cursor.HasMore {
     iferr:= cursor.NextPage(&ledger); err!= nil {
     println(err.Error())
     }
     for_, e:=range ledger {
     println(e.Amount)
     }
     }

    创建订单:

    order:= gdax.Order{
     Price: 1.00,
     Size: 1.00,
     Side: "buy",
     ProductId: "BTC-USD",
     }
     savedOrder, err:= client.CreateOrder(&order)
     if err!= nil {
     println(err.Error())
     }
     println(savedOrder.Id)

    转移资金:

    transfer:= gdax.Transfer {
     Type: "deposit",
     Amount: 1.00,
     }
     savedTransfer, err:= client.CreateTransfer(&transfer)
     if err!= nil {
     println(err.Error())
     }

    获取交易历史记录:

    vartrades []gdax.Tradecursor:= client.ListTrades("BTC-USD")
     for cursor.HasMore {
     iferr:= cursor.NextPage(&trades); err!= nil {
     for_, t:=range trades {
     println(trade.CoinbaseId)
     }
     }
     }

    测试

    使用coinbase沙箱的public 测试设置以下 环境变量:

    • TEST_COINBASE_SECRET
    • TEST_COINBASE_KEY
    • TEST_COINBASE_PASSPHRASE

    然后运行 go test

    TEST_COINBASE_SECRET=secret TEST_COINBASE_KEY=key TEST_COINBASE_PASSPHRASE=passphrase go test

    API  HTTP  Https  golang  Coinbase