Skip to content

Commit

Permalink
Merge pull request #7 from linkpoolio/feature/coinall-and-coss
Browse files Browse the repository at this point in the history
Add Coinall & COSS
  • Loading branch information
jleeh authored Feb 27, 2019
2 parents 94a25cd + c3ab9da commit c2c85e8
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 29 deletions.
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ External Adaptor for Chainlink which aggregates prices of crypto assets from mul
- Bitfinex
- ~~Bitstamp~~ (Disabled due to rate limit)
- Bittrex
- GDAX
- Coinall
- Coinbase Pro
- COSS
- HitBTC
- Huobi Pro
- ZB
Expand Down Expand Up @@ -57,15 +59,16 @@ Should return something similar to:
"base": "BTC",
"quote": "USD",
"id": "BTC-USD",
"price": "6754.1794331023375",
"volume": "195359536.70301655",
"price": "3836.4042305857843",
"volume": "131747894.87525243",
"usdPrice": "3836.4042305857843",
"exchanges": [
"GDAX",
"Bitfinex",
"HitBTC",
"Bitstamp"
"Bitfinex",
"Coinbase",
"COSS"
],
"errors": null
"warnings": null
},
"status": "",
"error": null,
Expand All @@ -84,13 +87,15 @@ curl -X POST -H 'Content-Type: application/json' -d '{ "jobRunId": "1234", "data
"base": "LINK",
"quote": "ETH",
"id": "LINK-ETH",
"price": "0.0004981579292115922",
"volume": "226.8355475505",
"price": "0.0031786459052877327",
"volume": "797.6642187877999",
"usdPrice": "0.43956635389465454",
"exchanges": [
"Binance",
"Huobi",
"Binance"
"COSS"
],
"errors": null
"warnings": null
},
"status": "",
"error": null,
Expand Down
8 changes: 4 additions & 4 deletions exchange/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ type Binance struct {

type BinanceProduct struct {
LastPrice string `json:"lastprice"`
Volume string `json:"quoteVolume"`
Volume string `json:"quoteVolume"`
}

type BinanceSymbol struct {
BaseAsset string `json:"baseAsset"`
BaseAsset string `json:"baseAsset"`
QuoteAsset string `json:"quoteAsset"`
}

Expand Down Expand Up @@ -50,7 +50,7 @@ func (exc *Binance) SetPairs() *Error {

func (exc *Binance) GetConfig() *Config {
return &Config{
Name: "Binance",
Name: "Binance",
BaseUrl: "https://www.binance.com/api/v1",
Pairs: exc.Pairs}
Pairs: exc.Pairs}
}
52 changes: 52 additions & 0 deletions exchange/coinall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package exchange

import (
"fmt"
)

type Coinall struct {
Exchange
Pairs []*Pair
}

type CoinallPair struct {
BaseCurrency string `json:"base_currency"`
QuoteCurrency string `json:"quote_currency"`
}

type CoinallTicker struct {
Last string `json:"last"`
QuoteVolume string `json:"quote_volume_24h"`
}

func (exc *Coinall) GetResponse(base, quote string) (*Response, *Error) {
var ticker CoinallTicker
config := exc.GetConfig()
err := HttpGet(config, fmt.Sprintf("/spot/v3/instruments/%s_%s/ticker", base, quote), &ticker)
if err != nil {
return nil, err
}
return &Response{config.Name, ToFloat64(ticker.Last), ToFloat64(ticker.QuoteVolume)}, nil
}

func (exc *Coinall) SetPairs() *Error {
var pairs []CoinallPair
config := exc.GetConfig()
err := HttpGet(config, "/spot/v3/instruments", &pairs)
if err != nil {
return err
}
for _, pair := range pairs {
exc.Pairs = append(exc.Pairs, &Pair{Base: pair.BaseCurrency, Quote: pair.QuoteCurrency})
}
return nil
}

func (exc *Coinall) GetConfig() *Config {
return &Config{
Name: "Coinall",
BaseUrl: "https://www.coinall.com/api",
Client: nil,
Pairs: exc.Pairs,
}
}
26 changes: 26 additions & 0 deletions exchange/coinall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package exchange

import (
"testing"
"github.com/stretchr/testify/assert"
"log"
)

func TestCoinall_SetPairs(t *testing.T) {
coinall := Coinall{}
coinall.SetPairs()
pairs := coinall.GetConfig().Pairs

assert.Contains(t, pairs, &Pair{"ETH", "USDT"})
assert.Contains(t, pairs, &Pair{"ETH", "BTC"})
}

func TestCoinall_GetResponse(t *testing.T) {
coinall := Coinall{}
price, err := coinall.GetResponse("ETH", "USDT")
if err != nil {
log.Fatal(err)
}
assert.True(t, price.Price > 0, "price from Coinall isn't greater than 0")
assert.True(t, price.Volume > 0, "volume from Coinall isn't greater than 0")
}
10 changes: 5 additions & 5 deletions exchange/gdax.go → exchange/coinbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"fmt"
)

type GDAX struct {
type Coinbase struct {
Exchange
Pairs []*Pair
}

func (exc *GDAX) GetResponse(base, quote string) (*Response, *Error) {
func (exc *Coinbase) GetResponse(base, quote string) (*Response, *Error) {
clientInterface := exc.GetConfig().Client
client := clientInterface.(*gdax.Client)

Expand All @@ -22,7 +22,7 @@ func (exc *GDAX) GetResponse(base, quote string) (*Response, *Error) {
return &Response{exc.GetConfig().Name, ticker.Price, ticker.Volume * ticker.Price}, nil
}

func (exc *GDAX) SetPairs() *Error {
func (exc *Coinbase) SetPairs() *Error {
clientInterface := exc.GetConfig().Client
client := clientInterface.(*gdax.Client)

Expand All @@ -37,6 +37,6 @@ func (exc *GDAX) SetPairs() *Error {
return nil
}

func (exc *GDAX) GetConfig() *Config {
return &Config{Name: "GDAX", Client: gdax.NewClient("", "", ""), Pairs: exc.Pairs}
func (exc *Coinbase) GetConfig() *Config {
return &Config{Name: "Coinbase", Client: gdax.NewClient("", "", ""), Pairs: exc.Pairs}
}
12 changes: 6 additions & 6 deletions exchange/gdax_test.go → exchange/coinbase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import (
"log"
)

func TestGDAX_SetPairs(t *testing.T) {
gdax := GDAX{}
func TestCoinbase_SetPairs(t *testing.T) {
gdax := Coinbase{}
gdax.SetPairs()
pairs := gdax.GetConfig().Pairs

assert.Contains(t, pairs, &Pair{"BTC", "USD"})
assert.Contains(t, pairs, &Pair{"ETH", "EUR"})
}

func TestGDAX_GetResponse(t *testing.T) {
gdax := GDAX{}
func TestCoinbase_GetResponse(t *testing.T) {
gdax := Coinbase{}
price, err := gdax.GetResponse("BTC", "USD")
if err != nil {
log.Fatal(err)
}
assert.True(t, price.Price > 0, "price from GDAX isn't greater than 0")
assert.True(t, price.Volume > 0, "volume from GDAX isn't greater than 0")
assert.True(t, price.Price > 0, "price from Coinbase isn't greater than 0")
assert.True(t, price.Volume > 0, "volume from Coinbase isn't greater than 0")
}
75 changes: 75 additions & 0 deletions exchange/coss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package exchange

import (
"fmt"
"strings"
)

type COSS struct {
Exchange
Pairs []*Pair
}

type COSSPairs struct {
Symbols []COSSPair `json:"symbols"`
}

type COSSPair struct {
Symbol string `json:"symbol"`
}

type COSSTicker struct {
Results []*COSSResult `json:"result"`
}

type COSSResult struct {
MarketName string `json:"MarketName"`
Last float64 `json:"Last"`
BaseVolume float64 `json:"BaseVolume"`
}

func (exc *COSS) GetResponse(base, quote string) (*Response, *Error) {
var ticker COSSTicker
config := exc.GetConfig()
config.BaseUrl = "https://exchange.coss.io/api" // Different endpoint between their engine/exchange API
err := HttpGet(config, fmt.Sprintf("/getmarketsummaries"), &ticker)
if err != nil {
return nil, err
}
mn := fmt.Sprintf("%s-%s", base, quote)
for _, result := range ticker.Results {
if result.MarketName == mn {
return &Response{config.Name, result.Last, result.BaseVolume}, nil
}
}
return nil, &Error{
Exchange: config.Name,
Status: "400",
Message: "pair given wasn't found within market summaries",
}
}

func (exc *COSS) SetPairs() *Error {
var pairs COSSPairs
config := exc.GetConfig()
err := HttpGet(config, "/exchange-info", &pairs)
if err != nil {
return err
}
for _, pair := range pairs.Symbols {
pairArr := strings.Split(pair.Symbol, "_")
if len(pairArr) == 2 {
exc.Pairs = append(exc.Pairs, &Pair{Base: pairArr[0], Quote: pairArr[1]})
}
}
return nil
}

func (exc *COSS) GetConfig() *Config {
return &Config{
Name: "COSS",
BaseUrl: "https://trade.coss.io/c/api/v1",
Client: nil,
Pairs: exc.Pairs,
}
}
26 changes: 26 additions & 0 deletions exchange/coss_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package exchange

import (
"testing"
"github.com/stretchr/testify/assert"
"log"
)

func TestCOSS_SetPairs(t *testing.T) {
coss := COSS{}
coss.SetPairs()
pairs := coss.GetConfig().Pairs

assert.Contains(t, pairs, &Pair{"ETH", "USD"})
assert.Contains(t, pairs, &Pair{"ETH", "BTC"})
}

func TestCOSS_GetResponse(t *testing.T) {
coss := COSS{}
price, err := coss.GetResponse("ETH", "USD")
if err != nil {
log.Fatal(err)
}
assert.True(t, price.Price > 0, "price from COSS isn't greater than 0")
assert.True(t, price.Volume > 0, "volume from COSS isn't greater than 0")
}
7 changes: 5 additions & 2 deletions exchange/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ var (
&Bitfinex{},
//&Bitstamp{}, (Extreme rate limit)
&Bittrex{},
&GDAX{},
&Coinall{},
&COSS{},
&Coinbase{},
&Huobi{},
&HitBtc{},
&ZB{} }
&ZB{},
}
)

type Config struct {
Expand Down
2 changes: 1 addition & 1 deletion web/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Api() *rest.Api{

log.Print("starting trading pairs ticker")
StartPairsTicker()
log.Print("set trading pairs, starting API...")
log.Print("set trading pairs, starting api")

api.SetApp(router)
log.Print("api started")
Expand Down

0 comments on commit c2c85e8

Please sign in to comment.