Skip to content

Commit

Permalink
Merge pull request #2 from solipsis/master
Browse files Browse the repository at this point in the history
Update shapeshift coin list. Additional CoinsAsList endpoint that doesn't require maintaining shapeshift_coins.go
  • Loading branch information
hunterlong committed Aug 21, 2018
2 parents 052ddb3 + 56d4e41 commit 75f7320
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 15 deletions.
42 changes: 41 additions & 1 deletion shapeshift.go
Expand Up @@ -3,6 +3,7 @@ package shapeshift
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
Expand Down Expand Up @@ -39,7 +40,7 @@ type LimitResponse struct {

type MarketInfoResponse struct {
Pair string `json:"pair,omitempty"`
Rate float64 `json:"rate,omitempty"`
Rate float64 `json:"rate,string,omitempty"`
Limit float64 `json:"limit,omitempty"`
Min float64 `json:"min,omitempty"`
MinerFee float64 `json:"minerFee,omitempty"`
Expand Down Expand Up @@ -204,6 +205,16 @@ func (p Pair) GetLimits() (float64, error) {
return ToFloat(g.Limit), err
}

func MarketInfo() ([]MarketInfoResponse, error) {
r, err := DoHttp("GET", "marketinfo", "")
if err != nil {
return nil, err
}
var arr []MarketInfoResponse
err = json.Unmarshal(r, &arr)
return arr, err
}

func (p Pair) GetInfo() (*MarketInfoResponse, error) {
r, err := DoHttp("GET", "marketinfo", p.Name)
if err != nil {
Expand Down Expand Up @@ -244,6 +255,32 @@ func TimeRemaining(addr string) (*TimeRemainingResponse, error) {
return &g, err
}

func CoinsAsList() ([]Coin, error) {
var coins []Coin
r, err := DoHttp("GET", "getcoins", "")
if err != nil {
return nil, err
}

// Use json.RawMessage to delay marshalling to support arbitrary top level keys
var coinmap map[string]*json.RawMessage
if err := json.Unmarshal(r, &coinmap); err != nil {
return coins, err
}

for _, coinJSON := range coinmap {
var c Coin
err := json.Unmarshal([]byte(*coinJSON), &c)
if err != nil {
//log.Println("Error unmarshalling coin:", err)
continue
}
coins = append(coins, c)
}

return coins, nil
}

func Coins() (*CoinsResponse, error) {
r, err := DoHttp("GET", "getcoins", "")
if err != nil {
Expand Down Expand Up @@ -332,6 +369,9 @@ func DoPostHttp(method string, apimethod string, data interface{}) ([]byte, erro
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New("There was an error creating your order, Please try again later")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body = bytes.TrimPrefix(body, []byte("\xef\xbb\xbf"))
Expand Down
118 changes: 104 additions & 14 deletions shapeshift_coins.go
Expand Up @@ -14,51 +14,96 @@ type Coin struct {
}

type CoinsResponse struct {
BTC struct {
FIRST struct {
Coin
} `json:"BTC"`
} `json:"1ST"`
ANT struct {
Coin
} `json:"ANT"`
BAT struct {
Coin
} `json:"BAT"`
BCH struct {
Coin
} `json:"BCH"`
BCY struct {
Coin
} `json:"BCY"`
BLK struct {
Coin
} `json:"BLK"`
BNT struct {
Coin
} `json:"BNT"`
BTC struct {
Coin
} `json:"BTC"`
BTCD struct {
Coin
} `json:"BTCD"`
BTG struct {
Coin
} `json:"BTG"`
BTS struct {
Coin
} `json:"BTS"`
CLAM struct {
Coin
} `json:"CLAM"`
CVC struct {
Coin
} `json:"CVC"`
DASH struct {
Coin
} `json:"DASH"`
DCR struct {
Coin
} `json:"DCR"`
DGB struct {
Coin
} `json:"DGB"`
DGD struct {
DNT struct {
Coin
} `json:"DGD"`
} `json:"DNT"`
DOGE struct {
Coin
} `json:"DOGE"`
EDG struct {
Coin
} `json:"EDG"`
EMC struct {
Coin
} `json:"EMC"`
ETH struct {
EOS struct {
Coin
} `json:"ETH"`
} `json:"EOS"`
ETC struct {
Coin
} `json:"ETC"`
ETH struct {
Coin
} `json:"ETH"`
FCT struct {
Coin
} `json:"FCT"`
FUN struct {
Coin
} `json:"FUN"`
GAME struct {
Coin
} `json:"GAME"`
GNO struct {
Coin
} `json:"GNO"`
GNT struct {
Coin
} `json:"GNT"`
GUP struct {
Coin
} `json:"GUP"`
KMD struct {
Coin
} `json:"KMD"`
LBC struct {
Coin
} `json:"LBC"`
Expand All @@ -71,54 +116,87 @@ type CoinsResponse struct {
MAID struct {
Coin
} `json:"MAID"`
MLN struct {
Coin
} `json:"MLN"`
MONA struct {
Coin
} `json:"MONA"`
MSC struct {
Coin
} `json:"MSC"`
MTL struct {
Coin
} `json:"MTL"`
NBT struct {
Coin
} `json:"NBT"`
NEO struct {
Coin
} `json:"NEO"`
NMC struct {
Coin
} `json:"NMC"`
NMR struct {
Coin
} `json:"NMR"`
NVC struct {
Coin
} `json:"NVC"`
NXT struct {
Coin
} `json:"NXT"`
OMG struct {
Coin
} `json:"OMG"`
POT struct {
Coin
} `json:"POT"`
PPC struct {
Coin
} `json:"PPC"`
REP struct {
QTUM struct {
Coin
} `json:"REP"`
} `json:"QTUM"`
RCN struct {
Coin
} `json:"RCN"`
RDD struct {
Coin
} `json:"RDD"`
SDC struct {
REP struct {
Coin
} `json:"REP"`
RLC struct {
Coin
} `json:"RLC"`
SALT struct {
Coin
} `json:"SDC"`
} `json:"SALT"`
SC struct {
Coin
} `json:"SC"`
SJCX struct {
SNGLS struct {
Coin
} `json:"SNGLS"`
SNT struct {
Coin
} `json:"SJCX"`
} `json:"SNT"`
START struct {
Coin
} `json:"START"`
STEEM struct {
Coin
} `json:"STEEM"`
SNGLS struct {
STORJ struct {
Coin
} `json:"SNGLS"`
} `json:"STORJ"`
SWT struct {
Coin
} `json:"SWT"`
TRST struct {
Coin
} `json:"TRST"`
USDT struct {
Coin
} `json:"USDT"`
Expand All @@ -131,9 +209,18 @@ type CoinsResponse struct {
VTC struct {
Coin
} `json:"VTC"`
WAVES struct {
Coin
} `json:"WAVES"`
WINGS struct {
Coin
} `json:"WINGS"`
XCP struct {
Coin
} `json:"XCP"`
XEM struct {
Coin
} `json:"XEM"`
XMR struct {
Coin
} `json:"XMR"`
Expand All @@ -143,4 +230,7 @@ type CoinsResponse struct {
ZEC struct {
Coin
} `json:"ZEC"`
ZRX struct {
Coin
} `json:"ZRX"`
}
12 changes: 12 additions & 0 deletions shapeshift_test.go
Expand Up @@ -132,6 +132,18 @@ func TestDepositStatus(t *testing.T) {

}

func TestGetCoinsAsList(t *testing.T) {

coins, err := CoinsAsList()
if err != nil || len(coins) == 0 {
t.Fail()
}

t.Log("Coin: ", coins[0].Name)
t.Log("Status: ", coins[0].Status)

}

func TestGetSupportedCoins(t *testing.T) {

coins, err := Coins()
Expand Down

0 comments on commit 75f7320

Please sign in to comment.