Skip to content

Commit

Permalink
refactor for Currency and CurrencyPair
Browse files Browse the repository at this point in the history
  • Loading branch information
nntaoli committed Aug 10, 2017
1 parent 4cf19d9 commit 1dfd20b
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 704 deletions.
614 changes: 9 additions & 605 deletions Const.go

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions CurrencyPair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package goex

import "strings"

type Currency struct {
Symbol string
Desc string
}

func (c Currency) String() string {
return c.Symbol
}

// A->B(A兑换为B)
type CurrencyPair struct {
CurrencyA Currency
CurrencyB Currency
}

var (
UNKNOWN = Currency{"UNKNOWN", ""}
CNY = Currency{"CNY", "rmb (China Yuan)"}
USD = Currency{"USD", "USA dollar"}
BTC = Currency{"BTC", "bitcoin.org"}
BCC = Currency{"BCC", "bitcoin-abc"}
LTC = Currency{"LTC", "litecoin.org"}
ETH = Currency{"ETH", ""}
ETC = Currency{"ETC", ""}
EOS = Currency{"EOS", ""}
BTS = Currency{"BTS", ""}
QTUM = Currency{"QTUM", ""}
SC = Currency{"SC", "sia.tech"}
ANS = Currency{"ANS", "www.antshares.org"}
ZEC = Currency{"ZEC", ""}

//currency pair

BTC_CNY = CurrencyPair{BTC, CNY}
LTC_CNY = CurrencyPair{LTC, CNY}
BCC_CNY = CurrencyPair{BCC, CNY}
ETH_CNY = CurrencyPair{ETH, CNY}
ETC_CNY = CurrencyPair{ETC, CNY}
EOS_CNY = CurrencyPair{EOS, CNY}
BTS_CNY = CurrencyPair{BTS, CNY}
QTUM_CNY = CurrencyPair{QTUM, CNY}
SC_CNY = CurrencyPair{SC, CNY}
ANS_CNY = CurrencyPair{ANS, CNY}
ZEC_CNY = CurrencyPair{ZEC, CNY}

BTC_USD = CurrencyPair{BTC, USD}
LTC_USD = CurrencyPair{LTC, USD}
ETH_USD = CurrencyPair{ETH, USD}
ETC_USD = CurrencyPair{ETC, USD}

LTC_BTC = CurrencyPair{LTC, BTC}
ETH_BTC = CurrencyPair{ETH, BTC}
ETC_BTC = CurrencyPair{ETC, BTC}
BCC_BTC = CurrencyPair{BCC, BTC}

ETC_ETH = CurrencyPair{ETC, ETH}
EOS_ETH = CurrencyPair{EOS, ETH}
)

func (c CurrencyPair) String() string {
return c.ToSymbol("_")
}

func NewCurrency(symbol, desc string) Currency {
return Currency{symbol, desc}
}

func NewCurrencyPair(currencyA Currency, currencyB Currency) CurrencyPair {
return CurrencyPair{currencyA, currencyB}
}

func (pair CurrencyPair) ToSymbol(joinChar string) string {
return strings.Join([]string{pair.CurrencyA.Symbol, pair.CurrencyB.Symbol}, joinChar)
}

func (pair CurrencyPair) ToSymbol2(joinChar string) string {
return strings.Join([]string{pair.CurrencyB.Symbol, pair.CurrencyA.Symbol}, joinChar)
}
19 changes: 19 additions & 0 deletions CurrencyPair_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package goex

import (
"strings"
"testing"
)

func TestCurrency2_String(t *testing.T) {
btc := NewCurrency("btc", "bitcoin")
ltc := NewCurrency("ltc", "litecoin")
t.Log(btc)
t.Log(ltc)
}

func TestCurrencyPair2_String(t *testing.T) {
btccny := NewCurrencyPair(NewCurrency("btc", ""), NewCurrency("cny", ""))
t.Log(strings.ToUpper(btccny.String()))
t.Log(BTC_CNY)
}
47 changes: 18 additions & 29 deletions bitfinex/bitfinex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"errors"
"fmt"
. "github.com/nntaoli-project/GoEx"
"log"
"net/http"
"strings"
)

type Bitfinex struct {
Expand All @@ -20,15 +20,6 @@ const (
BASE_URL = "https://api.bitfinex.com/v1"
)

var CURRENCYPAIR_TO_SYMBOL = map[CurrencyPair]string{
BTC_USD: "btcusd",
LTC_USD: "ltcusd",
LTC_BTC: "ltcbtc",
ETH_BTC: "ethbtc",
ETC_BTC: "etcbtc",
ETC_USD: "etcusd",
}

func New(client *http.Client, accessKey, secretKey string) *Bitfinex {
return &Bitfinex{client, accessKey, secretKey}
}
Expand All @@ -37,15 +28,9 @@ func (bfx *Bitfinex) GetExchangeName() string {
return EXCHANGE_NAME
}

func (bfx *Bitfinex) GetTicker(currency CurrencyPair) (*Ticker, error) {
func (bfx *Bitfinex) GetTicker(currencyPair CurrencyPair) (*Ticker, error) {
//pubticker
cur := currency.DeleteUnderLineString()
if cur == "nil" {
log.Println("Unsupport The CurrencyPair")
return nil, errors.New("Unsupport The CurrencyPair")
}

apiUrl := fmt.Sprintf("%s/pubticker/%s", BASE_URL, cur)
apiUrl := fmt.Sprintf("%s/pubticker/%s", BASE_URL, bfx.currencyPairToSymbol(currencyPair))
resp, err := HttpGet(bfx.httpClient, apiUrl)
if err != nil {
return nil, err
Expand All @@ -70,8 +55,8 @@ func (bfx *Bitfinex) GetTicker(currency CurrencyPair) (*Ticker, error) {
return ticker, nil
}

func (bfx *Bitfinex) GetDepth(size int, currency CurrencyPair) (*Depth, error) {
apiUrl := fmt.Sprintf("%s/book/%s?limit_bids=%d&limit_asks=%d", BASE_URL, currency.DeleteUnderLineString(), size, size)
func (bfx *Bitfinex) GetDepth(size int, currencyPair CurrencyPair) (*Depth, error) {
apiUrl := fmt.Sprintf("%s/book/%s?limit_bids=%d&limit_asks=%d", BASE_URL, bfx.currencyPairToSymbol(currencyPair), size, size)
resp, err := HttpGet(bfx.httpClient, apiUrl)
if err != nil {
return nil, err
Expand Down Expand Up @@ -101,7 +86,7 @@ func (bfx *Bitfinex) GetDepth(size int, currency CurrencyPair) (*Depth, error) {
return depth, nil
}

func (bfx *Bitfinex) GetKlineRecords(currency CurrencyPair, period string, size, since int) ([]Kline, error) {
func (bfx *Bitfinex) GetKlineRecords(currencyPair CurrencyPair, period string, size, since int) ([]Kline, error) {
panic("not implement")
}

Expand All @@ -115,34 +100,38 @@ func (bfx *Bitfinex) GetAccount() (*Account, error) {
return nil, nil
}

func (bfx *Bitfinex) LimitBuy(amount, price string, currency CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) LimitBuy(amount, price string, currencyPair CurrencyPair) (*Order, error) {
return nil, nil
}

func (bfx *Bitfinex) LimitSell(amount, price string, currency CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) LimitSell(amount, price string, currencyPair CurrencyPair) (*Order, error) {
return nil, nil
}

func (bfx *Bitfinex) MarketBuy(amount, price string, currency CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) MarketBuy(amount, price string, currencyPair CurrencyPair) (*Order, error) {
panic("not implement.")
}

func (bfx *Bitfinex) MarketSell(amount, price string, currency CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) MarketSell(amount, price string, currencyPair CurrencyPair) (*Order, error) {
panic("not implement.")
}

func (bfx *Bitfinex) CancelOrder(orderId string, currency CurrencyPair) (bool, error) {
func (bfx *Bitfinex) CancelOrder(orderId string, currencyPair CurrencyPair) (bool, error) {
return false, nil
}

func (bfx *Bitfinex) GetOneOrder(orderId string, currency CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) GetOneOrder(orderId string, currencyPair CurrencyPair) (*Order, error) {
return nil, nil
}

func (bfx *Bitfinex) GetUnfinishOrders(currency CurrencyPair) ([]Order, error) {
func (bfx *Bitfinex) GetUnfinishOrders(currencyPair CurrencyPair) ([]Order, error) {
return nil, nil
}

func (bfx *Bitfinex) GetOrderHistorys(currency CurrencyPair, currentPage, pageSize int) ([]Order, error) {
func (bfx *Bitfinex) GetOrderHistorys(currencyPair CurrencyPair, currentPage, pageSize int) ([]Order, error) {
return nil, nil
}

func (bfx *Bitfinex) currencyPairToSymbol(currencyPair CurrencyPair) string {
return strings.ToLower(currencyPair.ToSymbol(""))
}
12 changes: 6 additions & 6 deletions chbtc/Chbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (chbtc *Chbtc) GetExchangeName() string {
}

func (chbtc *Chbtc) GetTicker(currency CurrencyPair) (*Ticker, error) {
resp, err := HttpGet(chbtc.httpClient, MARKET_URL+fmt.Sprintf(TICKER_API, CurrencyPairSymbol[currency]))
resp, err := HttpGet(chbtc.httpClient, MARKET_URL+fmt.Sprintf(TICKER_API, strings.ToLower(currency.ToSymbol("_"))))
if err != nil {
return nil, err
}
Expand All @@ -63,7 +63,7 @@ func (chbtc *Chbtc) GetTicker(currency CurrencyPair) (*Ticker, error) {
}

func (chbtc *Chbtc) GetDepth(size int, currency CurrencyPair) (*Depth, error) {
resp, err := HttpGet(chbtc.httpClient, MARKET_URL+fmt.Sprintf(DEPTH_API, CurrencyPairSymbol[currency], size))
resp, err := HttpGet(chbtc.httpClient, MARKET_URL+fmt.Sprintf(DEPTH_API, currency.ToSymbol("_"), size))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (chbtc *Chbtc) placeOrder(amount, price string, currency CurrencyPair, trad
params.Set("method", "order")
params.Set("price", price)
params.Set("amount", amount)
params.Set("currency", CurrencyPairSymbol[currency])
params.Set("currency", currency.ToSymbol("_"))
params.Set("tradeType", fmt.Sprintf("%d", tradeType))
chbtc.buildPostForm(&params)

Expand Down Expand Up @@ -268,7 +268,7 @@ func (chbtc *Chbtc) CancelOrder(orderId string, currency CurrencyPair) (bool, er
params := url.Values{}
params.Set("method", "cancelOrder")
params.Set("id", orderId)
params.Set("currency", CurrencyPairSymbol[currency])
params.Set("currency", currency.ToSymbol("_"))
chbtc.buildPostForm(&params)

resp, err := HttpPostForm(chbtc.httpClient, TRADE_URL+CANCEL_ORDER_API, params)
Expand Down Expand Up @@ -338,7 +338,7 @@ func (chbtc *Chbtc) GetOneOrder(orderId string, currency CurrencyPair) (*Order,
params := url.Values{}
params.Set("method", "getOrder")
params.Set("id", orderId)
params.Set("currency", CurrencyPairSymbol[currency])
params.Set("currency", currency.ToSymbol("_"))
chbtc.buildPostForm(&params)

resp, err := HttpPostForm(chbtc.httpClient, TRADE_URL+GET_ORDER_API, params)
Expand All @@ -365,7 +365,7 @@ func (chbtc *Chbtc) GetOneOrder(orderId string, currency CurrencyPair) (*Order,
func (chbtc *Chbtc) GetUnfinishOrders(currency CurrencyPair) ([]Order, error) {
params := url.Values{}
params.Set("method", "getUnfinishedOrdersIgnoreTradeType")
params.Set("currency", CurrencyPairSymbol[currency])
params.Set("currency", currency.ToSymbol("_"))
params.Set("pageIndex", "1")
params.Set("pageSize", "100")
chbtc.buildPostForm(&params)
Expand Down
11 changes: 11 additions & 0 deletions chbtc/Chbtc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chbtc

import (
"github.com/nntaoli-project/GoEx"
"net/http"
"testing"
)
Expand All @@ -15,3 +16,13 @@ func TestChbtc_GetAccount(t *testing.T) {
acc, _ := chbtc.GetAccount()
t.Log(acc)
}

func TestChbtc_GetTicker(t *testing.T) {
ticker, _ := chbtc.GetTicker(goex.BTC_CNY)
t.Log(ticker)
}

func TestChbtc_GetDepth(t *testing.T) {
dep, _ := chbtc.GetDepth(1, goex.ETH_CNY)
t.Log(dep)
}
4 changes: 2 additions & 2 deletions coincheck/Coincheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
var api = New(http.DefaultClient, "", "")

func TestCoincheck_GetTicker(t *testing.T) {
ticker, err := api.GetTicker(BTC_JPY)
ticker, err := api.GetTicker(CurrencyPair{BTC, Currency{"JPY", ""}})
assert.NoError(t, err)
t.Log(ticker)
}

func TestCoincheck_GetDepth(t *testing.T) {
depth, err := api.GetDepth(3, BTC_JPY)
depth, err := api.GetDepth(3, CurrencyPair{BTC, NewCurrency("JPY", "")})
assert.NoError(t, err)
t.Log(depth)
}
4 changes: 2 additions & 2 deletions haobtc/HaoBtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (ctx *HaoBtc) buildPostForm(postForm *url.Values) error {

func (ctx *HaoBtc) GetTicker(currency CurrencyPair) (*Ticker, error) {
if currency != BTC_CNY {
return nil, errors.New("The HaoBtc Unsupport " + CurrencyPairSymbol[currency]);
return nil, errors.New("The HaoBtc Unsupport " + currency.String());
}

var tickerMap map[string]interface{};
Expand Down Expand Up @@ -87,7 +87,7 @@ func (ctx *HaoBtc) GetDepth(size int, currency CurrencyPair) (*Depth, error) {
case BTC_CNY:
depthUri = API_BASE_URL + fmt.Sprintf(DEPTH_URI, size);
default:
return nil, errors.New("Unsupport The CurrencyPair " + CurrencyPairSymbol[currency]);
return nil, errors.New("Unsupport The CurrencyPair " + currency.ToSymbol("_"));
}

bodyDataMap, err := HttpGet(ctx.httpClient,depthUri);
Expand Down
2 changes: 1 addition & 1 deletion huobi/HuoBi.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func (hb *HuoBi) GetKlineRecords(currency CurrencyPair, period string, size, sin
case LTC_CNY:
klineUri = fmt.Sprintf(klineUri, "ltc", period, size)
default:
return nil, errors.New("Unsupport " + CurrencyPairSymbol[currency])
return nil, errors.New("Unsupport " + currency.String())
}
//println(klineUri)
resp, err := http.Get(klineUri)
Expand Down
Loading

0 comments on commit 1dfd20b

Please sign in to comment.