Giter VIP home page Giter VIP logo

go-coinbase-exchange's Introduction

Go Coinbase Exchange GoDoc Build Status

Summary

Go client for GDAX

Installation

go get github.com/preichenberger/go-coinbase-exchange

Documentation

For full details on functionality, see GoDoc documentation.

Setup

How to create a client:

import (
  "os"
  exchange "github.com/preichenberger/go-coinbase-exchange"
)

secret := os.Getenv("COINBASE_SECRET") 
key := os.Getenv("COINBASE_KEY") 
passphrase := os.Getenv("COINBASE_PASSPHRASE") 

// or unsafe hardcode way
secret = "exposedsecret"
key = "exposedkey"
passphrase = "exposedpassphrase"

client := exchange.NewClient(secret, key, passphrase)

Cursor

This library uses a cursor pattern so you don't have to keep track of pagination.

var orders []exchange.Order
cursor = client.ListOrders()

for cursor.HasMore {
  if err := cursor.NextPage(&orders); err != nil {
    println(err.Error())
    return
  }

  for _, o := range orders {
    println(o.Id) 
  }
}

Websockets

Listen for websocket messages

  import(
    ws "github.com/gorilla/websocket"
  )

  var wsDialer ws.Dialer
  wsConn, _, err := wsDialer.Dial("wss://ws-feed.gdax.com", nil)
  if err != nil {
    println(err.Error())
  }

  subscribe := map[string]string{
    "type": "subscribe",
    "product_id": "BTC-USD",
  }
  if err := wsConn.WriteJSON(subscribe); err != nil {
    println(err.Error())
  }

  message:= exchange.Message{}
  for true {
    if err := wsConn.ReadJSON(&message); err != nil {
      println(err.Error())
      break
    }

    if message.Type == "match" {
      println("Got a match")
    }
  }

Examples

This library supports all public and private endpoints

Get Accounts:

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

  for _, a := range accounts {
    println(a.Balance)
  }

List Account Ledger:

  var ledger []exchange.LedgerEntry

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

  for _, a := range accounts {
    cursor := client.ListAccountLedger(a.Id)
    for cursor.HasMore {
      if err := cursor.NextPage(&ledger); err != nil {
        println(err.Error())
      }

      for _, e := range ledger {
        println(e.Amount)
      }
  }

Create an Order:

  order := exchange.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 funds:

  transfer := exchange.Transfer {
    Type: "deposit",
    Amount: 1.00,   
  }

  savedTransfer, err := client.CreateTransfer(&transfer)
  if err != nil {
    println(err.Error())
  }

Get Trade history:

  var trades []exchange.Trade
  cursor := client.ListTrades("BTC-USD")

  for cursor.HasMore {
    if err := cursor.NextPage(&trades); err != nil {
      for _, t := range trades {
        println(trade.CoinbaseId)
      }
    } 
  }

Testing

To test with Coinbase's public sandbox set the following environment variables:

  • TEST_COINBASE_SECRET
  • TEST_COINBASE_KEY
  • TEST_COINBASE_PASSPHRASE

Then run go test

TEST_COINBASE_SECRET=secret TEST_COINBASE_KEY=key TEST_COINBASE_PASSPHRASE=passphrase go test

go-coinbase-exchange's People

Contributors

andlister avatar davimdo avatar elthariel avatar petesavitsky avatar shelnutt2 avatar tonypan avatar zarkan avatar jonapaulo avatar mbags avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.