-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from linkpoolio/feature/coinall-and-coss
Add Coinall & COSS
- Loading branch information
Showing
10 changed files
with
216 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters