├── .gitignore ├── v3 ├── json │ ├── ping.json │ ├── simple_single_price.json │ ├── simple_price.json │ ├── simple_supported_vs_currencies.json │ └── coins_list.json ├── v3_test.go ├── types │ ├── types.go │ └── model.go └── v3.go ├── .vscode └── settings.json ├── CHANGELOG.md ├── gogecko.png ├── Makefile ├── go.mod ├── .travis.yml ├── format └── format.go ├── _example └── v3 │ ├── ping.go │ ├── coinsList.go │ ├── eventsTypes.go │ ├── coinsID.go │ ├── coinsIDHistory.go │ ├── simpleSupportedVSCurrencies.go │ ├── simpleSinglePrice.go │ ├── exchangeRates.go │ ├── eventsCountries.go │ ├── coinsIDTicker.go │ ├── simplePrice.go │ ├── global.go │ ├── coinsIDMarketChart.go │ └── coinsMarket.go ├── script └── test_example.sh ├── LICENSE.txt ├── request └── request.go ├── go.sum ├── README.md └── gogecko.svg /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /v3/json/ping.json: -------------------------------------------------------------------------------- 1 | { "gecko_says": "(V3) To the Moon!" } 2 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "go.formatTool": "goimports" 3 | } -------------------------------------------------------------------------------- /v3/json/simple_single_price.json: -------------------------------------------------------------------------------- 1 | { "bitcoin": { "usd": 5013.61 } } 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # v1.0.0 2 | 3 | - Initial release 4 | - Add in All-Time Low 5 | -------------------------------------------------------------------------------- /gogecko.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superoo7/go-gecko/HEAD/gogecko.png -------------------------------------------------------------------------------- /v3/json/simple_price.json: -------------------------------------------------------------------------------- 1 | { 2 | "bitcoin": { "usd": 5005.73, "myr": 20474 }, 3 | "ethereum": { "usd": 163.58, "myr": 669.07 } 4 | } 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | test: 2 | @go test -count=1 v3/v3_test.go v3/v3.go 3 | 4 | test_v3: 5 | @go test -count=1 v3/v3_test.go v3/v3.go 6 | 7 | test_example: 8 | @./script/test_example.sh 9 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/superoo7/go-gecko 2 | 3 | require ( 4 | github.com/google/pprof v0.0.0-20190404155422-f8f10df84213 // indirect 5 | golang.org/x/arch v0.0.0-20190312162104-788fe5ffcd8c // indirect 6 | gopkg.in/h2non/gock.v1 v1.0.14 7 | ) 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | 3 | go: 4 | - "1.x" 5 | - "1.8" 6 | - "1.10.x" 7 | - master 8 | 9 | script: 10 | - go get -u gopkg.in/h2non/gock.v1 11 | - go build v3/v3_test.go v3/v3.go 12 | - go test v3/v3_test.go v3/v3.go 13 | - ./script/test_example.sh 14 | -------------------------------------------------------------------------------- /format/format.go: -------------------------------------------------------------------------------- 1 | package format 2 | 3 | import "strconv" 4 | 5 | // Bool2String boolean to string 6 | func Bool2String(b bool) string { 7 | return strconv.FormatBool(b) 8 | } 9 | 10 | // Int2String Integer to string 11 | func Int2String(i int) string { 12 | return strconv.Itoa(i) 13 | } 14 | -------------------------------------------------------------------------------- /_example/v3/ping.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | ) 9 | 10 | func main() { 11 | cg := gecko.NewClient(nil) 12 | ping, err := cg.Ping() 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | fmt.Println(ping.GeckoSays) 17 | } 18 | -------------------------------------------------------------------------------- /_example/v3/coinsList.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | ) 9 | 10 | func main() { 11 | cg := gecko.NewClient(nil) 12 | list, err := cg.CoinsList() 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | fmt.Println("Available coins:", len(*list)) 17 | } 18 | -------------------------------------------------------------------------------- /_example/v3/eventsTypes.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | ) 9 | 10 | func main() { 11 | cg := gecko.NewClient(nil) 12 | t, err := cg.EventsTypes() 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | fmt.Println(t.Count) 17 | fmt.Println(t.Data) 18 | } 19 | -------------------------------------------------------------------------------- /_example/v3/coinsID.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | ) 9 | 10 | func main() { 11 | cg := gecko.NewClient(nil) 12 | coin, err := cg.CoinsID("dogecoin", true, true, true, true, true, true) 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | fmt.Println(coin) 17 | } 18 | -------------------------------------------------------------------------------- /_example/v3/coinsIDHistory.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | ) 9 | 10 | func main() { 11 | cg := gecko.NewClient(nil) 12 | btc, err := cg.CoinsIDHistory("bitcoin", "30-12-2018", true) 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | fmt.Println(*btc) 17 | } 18 | -------------------------------------------------------------------------------- /script/test_example.sh: -------------------------------------------------------------------------------- 1 | #/bin/sh 2 | 3 | set -e 4 | 5 | Green='\033[0;32m' # Green 6 | Color_Off='\033[0m' # Text Reset 7 | 8 | 9 | for filename in $(ls _example/v3) 10 | do 11 | echo "$Green========================" 12 | echo $filename 13 | echo "========================$Color_Off" 14 | go run _example/v3/$filename >/dev/null 15 | echo "SUCCESSFUL" 16 | done -------------------------------------------------------------------------------- /_example/v3/simpleSupportedVSCurrencies.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | ) 9 | 10 | func main() { 11 | cg := gecko.NewClient(nil) 12 | currencies, err := cg.SimpleSupportedVSCurrencies() 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | fmt.Println("Total currencies", len(*currencies)) 17 | fmt.Println(*currencies) 18 | } 19 | -------------------------------------------------------------------------------- /_example/v3/simpleSinglePrice.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | ) 9 | 10 | func main() { 11 | cg := gecko.NewClient(nil) 12 | 13 | price, err := cg.SimpleSinglePrice("bitcoin", "usd") 14 | if err != nil { 15 | log.Fatal(err) 16 | } 17 | fmt.Println(fmt.Sprintf("%s is worth %f %s", price.ID, price.MarketPrice, price.Currency)) 18 | } 19 | -------------------------------------------------------------------------------- /_example/v3/exchangeRates.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | ) 9 | 10 | func main() { 11 | cg := gecko.NewClient(nil) 12 | rate, err := cg.ExchangeRates() 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | r := (*rate)["btc"] 17 | fmt.Println(r.Name) 18 | fmt.Println(r.Unit) 19 | fmt.Println(r.Value) 20 | fmt.Println(r.Type) 21 | } 22 | -------------------------------------------------------------------------------- /_example/v3/eventsCountries.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | ) 9 | 10 | func main() { 11 | cg := gecko.NewClient(nil) 12 | e, err := cg.EventsCountries() 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | fmt.Println(len(e)) 17 | fmt.Println(e[0]) 18 | fmt.Println(e[1]) 19 | fmt.Println(e[2].Code) 20 | fmt.Println(e[2].Country) 21 | } 22 | -------------------------------------------------------------------------------- /_example/v3/coinsIDTicker.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | ) 9 | 10 | func main() { 11 | cg := gecko.NewClient(nil) 12 | tickers, err := cg.CoinsIDTickers("bitcoin", 0) 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | fmt.Println(len(tickers.Tickers)) 17 | tickers, err = cg.CoinsIDTickers("bitcoin", 1) 18 | fmt.Println(len(tickers.Tickers)) 19 | } 20 | -------------------------------------------------------------------------------- /_example/v3/simplePrice.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | ) 9 | 10 | func main() { 11 | cg := gecko.NewClient(nil) 12 | 13 | ids := []string{"bitcoin", "ethereum"} 14 | vc := []string{"usd", "myr"} 15 | sp, err := cg.SimplePrice(ids, vc) 16 | if err != nil { 17 | log.Fatal(err) 18 | } 19 | bitcoin := (*sp)["bitcoin"] 20 | eth := (*sp)["ethereum"] 21 | fmt.Println(fmt.Sprintf("Bitcoin is worth %f usd (myr %f)", bitcoin["usd"], bitcoin["myr"])) 22 | fmt.Println(fmt.Sprintf("Ethereum is worth %f usd (myr %f)", eth["usd"], eth["myr"])) 23 | } 24 | -------------------------------------------------------------------------------- /v3/json/simple_supported_vs_currencies.json: -------------------------------------------------------------------------------- 1 | [ 2 | "btc", 3 | "eth", 4 | "ltc", 5 | "bch", 6 | "bnb", 7 | "eos", 8 | "xrp", 9 | "xlm", 10 | "usd", 11 | "aed", 12 | "ars", 13 | "aud", 14 | "bdt", 15 | "bhd", 16 | "bmd", 17 | "brl", 18 | "cad", 19 | "chf", 20 | "clp", 21 | "cny", 22 | "czk", 23 | "dkk", 24 | "eur", 25 | "gbp", 26 | "hkd", 27 | "huf", 28 | "idr", 29 | "ils", 30 | "inr", 31 | "jpy", 32 | "krw", 33 | "kwd", 34 | "lkr", 35 | "mmk", 36 | "mxn", 37 | "myr", 38 | "nok", 39 | "nzd", 40 | "php", 41 | "pkr", 42 | "pln", 43 | "rub", 44 | "sar", 45 | "sek", 46 | "sgd", 47 | "thb", 48 | "try", 49 | "twd", 50 | "vef", 51 | "vnd", 52 | "zar", 53 | "xdr", 54 | "xag", 55 | "xau" 56 | ] 57 | -------------------------------------------------------------------------------- /_example/v3/global.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "time" 7 | 8 | gecko "github.com/superoo7/go-gecko/v3" 9 | ) 10 | 11 | func main() { 12 | cg := gecko.NewClient(nil) 13 | global, err := cg.Global() 14 | if err != nil { 15 | log.Fatal(err) 16 | } 17 | fmt.Println("active crypto:", global.ActiveCryptocurrencies) 18 | fmt.Println("upcoming ico:", global.UpcomingICOs) 19 | fmt.Println("ended ico:", global.EndedICOs) 20 | fmt.Println("market:", global.Markets) 21 | fmt.Println("BTC Volume:", global.TotalVolume["btc"]) 22 | fmt.Println("Global Total Market Cap in USD:", global.TotalMarketCap["usd"]) 23 | fmt.Println("Market Cap Percentage of ETH:", global.MarketCapPercentage["eth"]) 24 | fmt.Println("Market Cap Change Percentage 24h USD:", global.MarketCapChangePercentage24hUSD) 25 | fmt.Println("last updated:", time.Unix(global.UpdatedAt, 0)) 26 | } 27 | -------------------------------------------------------------------------------- /_example/v3/coinsIDMarketChart.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "time" 7 | 8 | gecko "github.com/superoo7/go-gecko/v3" 9 | ) 10 | 11 | func main() { 12 | cg := gecko.NewClient(nil) 13 | m, err := cg.CoinsIDMarketChart("bitcoin", "usd", "1") 14 | if err != nil { 15 | log.Fatal(err) 16 | } 17 | 18 | fmt.Printf("Prices\n") 19 | for _, v := range *m.Prices { 20 | fmt.Printf("%s:%.04f\n", time.Unix(int64(v[0])/1000, int64(v[0])%1000).UTC().Format(time.RFC3339), v[1]) 21 | } 22 | 23 | fmt.Printf("MarketCaps\n") 24 | for _, v := range *m.MarketCaps { 25 | fmt.Printf("%s:%.04f\n", time.Unix(int64(v[0])/1000, int64(v[0])%1000).UTC().Format(time.RFC3339), v[1]) 26 | } 27 | 28 | fmt.Printf("TotalVolumes\n") 29 | for _, v := range *m.TotalVolumes { 30 | fmt.Printf("%s:%.04f\n", time.Unix(int64(v[0])/1000, int64(v[0])%1000).UTC().Format(time.RFC3339), v[1]) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2019 Lai Weng Han 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /request/request.go: -------------------------------------------------------------------------------- 1 | package request 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "net/http" 7 | "strconv" 8 | ) 9 | 10 | // helper 11 | // doReq HTTP client 12 | func doReq(req *http.Request) ([]byte, error) { 13 | client := &http.Client{} 14 | resp, err := client.Do(req) 15 | if err != nil { 16 | return nil, err 17 | } 18 | defer resp.Body.Close() 19 | body, err := ioutil.ReadAll(resp.Body) 20 | if err != nil { 21 | return nil, err 22 | } 23 | if 200 != resp.StatusCode { 24 | return nil, fmt.Errorf("%s", body) 25 | } 26 | return body, nil 27 | } 28 | 29 | // MakeReq HTTP request helper 30 | func MakeReq(url string) ([]byte, error) { 31 | req, err := http.NewRequest("GET", url, nil) 32 | if err != nil { 33 | return nil, err 34 | } 35 | resp, err := doReq(req) 36 | if err != nil { 37 | return nil, err 38 | } 39 | return resp, err 40 | } 41 | 42 | // Bool2String boolean to string 43 | func Bool2String(b bool) string { 44 | return strconv.FormatBool(b) 45 | } 46 | 47 | // Int2String Integer to string 48 | func Int2String(i int) string { 49 | return strconv.Itoa(i) 50 | } 51 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/google/pprof v0.0.0-20190404155422-f8f10df84213 h1:y77GPNuZbqlt3/OjKA9yysnnxxqW+XwLaGY3ol9Mgmc= 2 | github.com/google/pprof v0.0.0-20190404155422-f8f10df84213/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= 3 | github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= 4 | github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= 5 | github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4= 6 | github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= 7 | golang.org/x/arch v0.0.0-20190312162104-788fe5ffcd8c h1:Rx/HTKi09myZ25t1SOlDHmHOy/mKxNAcu0hP1oPX9qM= 8 | golang.org/x/arch v0.0.0-20190312162104-788fe5ffcd8c/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4= 9 | gopkg.in/h2non/gock.v1 v1.0.14 h1:fTeu9fcUvSnLNacYvYI54h+1/XEteDyHvrVCZEEEYNM= 10 | gopkg.in/h2non/gock.v1 v1.0.14/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdODlynE= 11 | rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= 12 | -------------------------------------------------------------------------------- /_example/v3/coinsMarket.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | gecko "github.com/superoo7/go-gecko/v3" 8 | geckoTypes "github.com/superoo7/go-gecko/v3/types" 9 | ) 10 | 11 | func main() { 12 | cg := gecko.NewClient(nil) 13 | // find specific coins 14 | vsCurrency := "usd" 15 | ids := []string{"bitcoin", "ethereum", "steem"} 16 | perPage := 1 17 | page := 1 18 | sparkline := true 19 | pcp := geckoTypes.PriceChangePercentageObject 20 | priceChangePercentage := []string{pcp.PCP1h, pcp.PCP24h, pcp.PCP7d, pcp.PCP14d, pcp.PCP30d, pcp.PCP200d, pcp.PCP1y} 21 | order := geckoTypes.OrderTypeObject.MarketCapDesc 22 | market, err := cg.CoinsMarket(vsCurrency, ids, order, perPage, page, sparkline, priceChangePercentage) 23 | if err != nil { 24 | log.Fatal(err) 25 | } 26 | fmt.Println("Total coins: ", len(*market)) 27 | fmt.Println(*market) 28 | 29 | // with pagination instead 30 | ids = []string{} 31 | perPage = 10 32 | page = 1 33 | market, err = cg.CoinsMarket(vsCurrency, ids, order, perPage, page, sparkline, priceChangePercentage) 34 | fmt.Println("Total coins: ", len(*market)) 35 | fmt.Println(*market) 36 | } 37 | -------------------------------------------------------------------------------- /v3/v3_test.go: -------------------------------------------------------------------------------- 1 | package coingecko 2 | 3 | import ( 4 | "io/ioutil" 5 | "os" 6 | "testing" 7 | 8 | gock "gopkg.in/h2non/gock.v1" 9 | ) 10 | 11 | func init() { 12 | defer gock.Off() 13 | } 14 | 15 | var c = NewClient(nil) 16 | var mockURL = "https://api.coingecko.com/api/v3" 17 | 18 | func TestPing(t *testing.T) { 19 | err := setupGock("json/ping.json", "/ping") 20 | ping, err := c.Ping() 21 | if err != nil { 22 | t.FailNow() 23 | } 24 | if ping.GeckoSays != "(V3) To the Moon!" { 25 | t.FailNow() 26 | } 27 | } 28 | 29 | func TestSimpleSinglePrice(t *testing.T) { 30 | err := setupGock("json/simple_single_price.json", "/simple/price") 31 | if err != nil { 32 | t.FailNow() 33 | } 34 | simplePrice, err := c.SimpleSinglePrice("bitcoin", "usd") 35 | if err != nil { 36 | t.FailNow() 37 | } 38 | if simplePrice.ID != "bitcoin" || simplePrice.Currency != "usd" || simplePrice.MarketPrice != float32(5013.61) { 39 | t.FailNow() 40 | } 41 | } 42 | 43 | func TestSimplePrice(t *testing.T) { 44 | err := setupGock("json/simple_price.json", "/simple/price") 45 | if err != nil { 46 | t.FailNow() 47 | } 48 | ids := []string{"bitcoin", "ethereum"} 49 | vc := []string{"usd", "myr"} 50 | sp, err := c.SimplePrice(ids, vc) 51 | if err != nil { 52 | t.FailNow() 53 | } 54 | bitcoin := (*sp)["bitcoin"] 55 | eth := (*sp)["ethereum"] 56 | 57 | if bitcoin["usd"] != 5005.73 || bitcoin["myr"] != 20474 { 58 | t.FailNow() 59 | } 60 | if eth["usd"] != 163.58 || eth["myr"] != 669.07 { 61 | t.FailNow() 62 | } 63 | } 64 | 65 | func TestSimpleSupportedVSCurrencies(t *testing.T) { 66 | err := setupGock("json/simple_supported_vs_currencies.json", "/simple/supported_vs_currencies") 67 | s, err := c.SimpleSupportedVSCurrencies() 68 | if err != nil { 69 | t.FailNow() 70 | } 71 | if len(*s) != 54 { 72 | t.FailNow() 73 | } 74 | } 75 | 76 | func TestCoinsList(t *testing.T) { 77 | err := setupGock("json/coins_list.json", "/coins/list") 78 | list, err := c.CoinsList() 79 | if err != nil { 80 | t.FailNow() 81 | } 82 | item := (*list)[0] 83 | if item.ID != "01coin" { 84 | t.FailNow() 85 | } 86 | } 87 | 88 | // Util: Setup Gock 89 | func setupGock(filename string, url string) error { 90 | testJSON, err := os.Open(filename) 91 | defer testJSON.Close() 92 | if err != nil { 93 | return err 94 | } 95 | testByte, err := ioutil.ReadAll(testJSON) 96 | if err != nil { 97 | return err 98 | } 99 | gock.New(mockURL). 100 | Get(url). 101 | Reply(200). 102 | JSON(testByte) 103 | 104 | return nil 105 | } 106 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CoinGecko API Client for Go 2 | 3 | [![Build Status](https://travis-ci.com/superoo7/go-gecko.svg?branch=master)](https://travis-ci.com/superoo7/go-gecko) [![GoDoc](https://godoc.org/github.com/superoo7/go-gecko?status.svg)](https://godoc.org/github.com/superoo7/go-gecko) 4 | 5 | Simple API Client for CoinGecko written in Go 6 | 7 |

8 | gogecko 9 |

10 | 11 | gopher resources from [free-gophers-pack](https://github.com/MariaLetta/free-gophers-pack) 12 | 13 | ## Available endpoint 14 | 15 | [Refer to CoinGecko official API](https://www.coingecko.com/api) 16 | 17 | | Endpoint | Status | Testing | Function | 18 | | :-----------------------------: | :----: | :-----: | :----------------------------: | 19 | | /ping | [/] | [/] | Ping | 20 | | /simple/price | [/] | [/] | SimpleSinglePrice, SimplePrice | 21 | | /simple/supported_vs_currencies | [/] | [/] | SimpleSupportedVSCurrencies | 22 | | /coins/list | [/] | [/] | CoinsList | 23 | | /coins/market | [/] | [/] | CoinsMarket | 24 | | /coins/{id} | [/] | | CoinsID | 25 | | /coins/{id}/history | [/] | | CoinsIDHistory | 26 | | /coins/{id}/market_chart | [/] | | CoinsIDMarketChart | 27 | | /events/countries | [/] | | EventsCountries | 28 | | /events/types | [/] | | EventsType | 29 | | /exchange_rates | [/] | | ExchangeRate | 30 | | /global | [/] | | Global | 31 | 32 | ## Usage 33 | 34 | Installation with go get. 35 | 36 | ``` 37 | go get -u github.com/superoo7/go-gecko 38 | ``` 39 | 40 | For usage, checkout [Example folder for v3](/_example/v3) 41 | 42 | For production, you might need to set time out for httpClient, here's a sample code: 43 | 44 | ```go 45 | package main 46 | 47 | import ( 48 | "net/http" 49 | "time" 50 | 51 | coingecko "github.com/superoo7/go-gecko/v3" 52 | ) 53 | 54 | func main() { 55 | httpClient := &http.Client{ 56 | Timeout: time.Second * 10, 57 | } 58 | CG := coingecko.NewClient(httpClient) 59 | } 60 | ``` 61 | 62 | ## Convention 63 | 64 | refer to https://medium.com/@marcus.olsson/writing-a-go-client-for-your-restful-api-c193a2f4998c 65 | 66 | ## License 67 | 68 | MIT 69 | -------------------------------------------------------------------------------- /v3/json/coins_list.json: -------------------------------------------------------------------------------- 1 | [ 2 | { "id": "01coin", "symbol": "zoc", "name": "01coin" }, 3 | { "id": "02-token", "symbol": "o2t", "name": "O2 Token" }, 4 | { "id": "0chain", "symbol": "zcn", "name": "0chain" }, 5 | { "id": "0x", "symbol": "zrx", "name": "0x" }, 6 | { "id": "0xcert", "symbol": "zxc", "name": "0xcert" }, 7 | { "id": "10m-token", "symbol": "10mt", "name": "10M Token" }, 8 | { "id": "1337", "symbol": "1337", "name": "1337" }, 9 | { "id": "1irstcoin", "symbol": "fst", "name": "1irstcoin" }, 10 | { "id": "1sg", "symbol": "1sg", "name": "1SG" }, 11 | { "id": "1world", "symbol": "1wo", "name": "1World" }, 12 | { "id": "1x2-coin", "symbol": "1x2", "name": "1X2 Coin" }, 13 | { "id": "2345-star-coin", "symbol": "stc", "name": "2345 Star Coin" }, 14 | { "id": "2acoin", "symbol": "arms", "name": "2ACoin" }, 15 | { "id": "2give", "symbol": "2give", "name": "2GIVE" }, 16 | { "id": "300cubits", "symbol": "teu", "name": "300cubits" }, 17 | { "id": "300token", "symbol": "300", "name": "300 Token" }, 18 | { "id": "3d-chain", "symbol": "3dc", "name": "3D-Chain" }, 19 | { "id": "404", "symbol": "404", "name": "404" }, 20 | { "id": "42-coin", "symbol": "42", "name": "42-coin" }, 21 | { "id": "4a-coin", "symbol": "4ac", "name": "4A Coin" }, 22 | { "id": "4new", "symbol": "kwatt", "name": "4New" }, 23 | { "id": "4xbit", "symbol": "4xb", "name": "4xBit" }, 24 | { "id": "6x-token", "symbol": "xt", "name": "6X Token" }, 25 | { "id": "777-bingo", "symbol": "777", "name": "777.Bingo" }, 26 | { "id": "808coin", "symbol": "808", "name": "808Coin" }, 27 | { "id": "888tron", "symbol": "888", "name": "888tron" }, 28 | { "id": "8bit", "symbol": "8bit", "name": "8Bit" }, 29 | { "id": "9coin-token", "symbol": "t9", "name": "9Coin Token" }, 30 | { "id": "abc-chain", "symbol": "abc", "name": "ABC Chain" }, 31 | { "id": "abcc-token", "symbol": "at", "name": "ABCC Token" }, 32 | { "id": "abitshadow-token", "symbol": "abst", "name": "abitshadow token" }, 33 | { "id": "able", "symbol": "ablx", "name": "ABLE X Token" }, 34 | { 35 | "id": "able-dollar-x-token", 36 | "symbol": "abld", 37 | "name": "ABLE Dollar X Token" 38 | }, 39 | { "id": "abo", "symbol": "abo", "name": "ABO" }, 40 | { "id": "absolute", "symbol": "abs", "name": "Absolute" }, 41 | { "id": "abulaba", "symbol": "aaa", "name": "Abulaba" }, 42 | { "id": "ac3", "symbol": "ac3", "name": "AC3" }, 43 | { "id": "academy token", "symbol": "acad", "name": "Academy Token" }, 44 | { "id": "accel", "symbol": "acce", "name": "Accel" }, 45 | { 46 | "id": "accelerator-network", 47 | "symbol": "acc", 48 | "name": "Accelerator Network" 49 | }, 50 | { "id": "acchain", "symbol": "acc", "name": "ACChain" } 51 | ] 52 | -------------------------------------------------------------------------------- /v3/types/types.go: -------------------------------------------------------------------------------- 1 | package types 2 | 3 | // Ping https://api.coingecko.com/api/v3/ping 4 | type Ping struct { 5 | GeckoSays string `json:"gecko_says"` 6 | } 7 | 8 | // SimpleSinglePrice https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd 9 | type SimpleSinglePrice struct { 10 | ID string 11 | Currency string 12 | MarketPrice float32 13 | } 14 | 15 | // SimpleSupportedVSCurrencies https://api.coingecko.com/api/v3/simple/supported_vs_currencies 16 | type SimpleSupportedVSCurrencies []string 17 | 18 | // CoinList https://api.coingecko.com/api/v3/coins/list 19 | type CoinList []CoinsListItem 20 | 21 | // CoinsMarket https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=false 22 | type CoinsMarket []CoinsMarketItem 23 | 24 | // CoinsID https://api.coingecko.com/api/v3/coins/bitcoin 25 | type CoinsID struct { 26 | coinBaseStruct 27 | BlockTimeInMin int32 `json:"block_time_in_minutes"` 28 | Categories []string `json:"categories"` 29 | Localization LocalizationItem `json:"localization"` 30 | Description DescriptionItem `json:"description"` 31 | Links *LinksItem `json:"links"` 32 | Image ImageItem `json:"image"` 33 | CountryOrigin string `json:"country_origin"` 34 | GenesisDate string `json:"genesis_date"` 35 | MarketCapRank uint16 `json:"market_cap_rank"` 36 | CoinGeckoRank uint16 `json:"coingecko_rank"` 37 | CoinGeckoScore float32 `json:"coingecko_score"` 38 | DeveloperScore float32 `json:"developer_score"` 39 | CommunityScore float32 `json:"community_score"` 40 | LiquidityScore float32 `json:"liquidity_score"` 41 | PublicInterestScore float32 `json:"public_interest_score"` 42 | MarketData *MarketDataItem `json:"market_data"` 43 | CommunityData *CommunityDataItem `json:"community_data"` 44 | DeveloperData *DeveloperDataItem `json:"developer_data"` 45 | PublicInterestStats *PublicInterestItem `json:"public_interest_stats"` 46 | StatusUpdates *[]StatusUpdateItem `json:"status_updates"` 47 | LastUpdated string `json:"last_updated"` 48 | Tickers *[]TickerItem `json:"tickers"` 49 | } 50 | 51 | // CoinsIDTickers https://api.coingecko.com/api/v3/coins/steem/tickers?page=1 52 | type CoinsIDTickers struct { 53 | Name string `json:"name"` 54 | Tickers []TickerItem `json:"tickers"` 55 | } 56 | 57 | // CoinsIDHistory https://api.coingecko.com/api/v3/coins/steem/history?date=30-12-2018 58 | type CoinsIDHistory struct { 59 | coinBaseStruct 60 | Localization LocalizationItem `json:"localization"` 61 | Image ImageItem `json:"image"` 62 | MarketData *MarketDataItem `json:"market_data"` 63 | CommunityData *CommunityDataItem `json:"community_data"` 64 | DeveloperData *DeveloperDataItem `json:"developer_data"` 65 | PublicInterest *PublicInterestItem `json:"public_interest_stats"` 66 | } 67 | 68 | // CoinsIDMarketChart https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=1 69 | type CoinsIDMarketChart struct { 70 | coinBaseStruct 71 | Prices *[]ChartItem `json:"prices"` 72 | MarketCaps *[]ChartItem `json:"market_caps"` 73 | TotalVolumes *[]ChartItem `json:"total_volumes"` 74 | } 75 | 76 | // CoinsIDStatusUpdates 77 | 78 | // CoinsIDContractAddress https://api.coingecko.com/api/v3/coins/{id}/contract/{contract_address} 79 | // type CoinsIDContractAddress struct { 80 | // ID string `json:"id"` 81 | // Symbol string `json:"symbol"` 82 | // Name string `json:"name"` 83 | // BlockTimeInMin uint16 `json:"block_time_in_minutes"` 84 | // Categories []string `json:"categories"` 85 | // Localization LocalizationItem `json:"localization"` 86 | // Description DescriptionItem `json:"description"` 87 | // Links LinksItem `json:"links"` 88 | // Image ImageItem `json:"image"` 89 | // CountryOrigin string `json:"country_origin"` 90 | // GenesisDate string `json:"genesis_date"` 91 | // ContractAddress string `json:"contract_address"` 92 | // MarketCapRank uint16 `json:"market_cap_rank"` 93 | // CoinGeckoRank uint16 `json:"coingecko_rank"` 94 | // CoinGeckoScore float32 `json:"coingecko_score"` 95 | // DeveloperScore float32 `json:"developer_score"` 96 | // CommunityScore float32 `json:"community_score"` 97 | // LiquidityScore float32 `json:"liquidity_score"` 98 | // PublicInterestScore float32 `json:"public_interest_score"` 99 | // MarketData `json:"market_data"` 100 | // } 101 | 102 | // EventsCountries https://api.coingecko.com/api/v3/events/countries 103 | type EventsCountries struct { 104 | Data []EventCountryItem `json:"data"` 105 | } 106 | 107 | // EventsTypes https://api.coingecko.com/api/v3/events/types 108 | type EventsTypes struct { 109 | Data []string `json:"data"` 110 | Count uint16 `json:"count"` 111 | } 112 | 113 | // ExchangeRatesResponse https://api.coingecko.com/api/v3/exchange_rates 114 | type ExchangeRatesResponse struct { 115 | Rates ExchangeRatesItem `json:"rates"` 116 | } 117 | 118 | // GlobalResponse https://api.coingecko.com/api/v3/global 119 | type GlobalResponse struct { 120 | Data Global `json:"data"` 121 | } 122 | -------------------------------------------------------------------------------- /v3/v3.go: -------------------------------------------------------------------------------- 1 | package coingecko 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io/ioutil" 7 | "net/http" 8 | "net/url" 9 | "strings" 10 | 11 | "github.com/superoo7/go-gecko/format" 12 | "github.com/superoo7/go-gecko/v3/types" 13 | ) 14 | 15 | var baseURL = "https://api.coingecko.com/api/v3" 16 | 17 | // Client struct 18 | type Client struct { 19 | httpClient *http.Client 20 | } 21 | 22 | // NewClient create new client object 23 | func NewClient(httpClient *http.Client) *Client { 24 | if httpClient == nil { 25 | httpClient = http.DefaultClient 26 | } 27 | return &Client{httpClient: httpClient} 28 | } 29 | 30 | // helper 31 | // doReq HTTP client 32 | func doReq(req *http.Request, client *http.Client) ([]byte, error) { 33 | resp, err := client.Do(req) 34 | if err != nil { 35 | return nil, err 36 | } 37 | defer resp.Body.Close() 38 | body, err := ioutil.ReadAll(resp.Body) 39 | if err != nil { 40 | return nil, err 41 | } 42 | if 200 != resp.StatusCode { 43 | return nil, fmt.Errorf("%s", body) 44 | } 45 | return body, nil 46 | } 47 | 48 | // MakeReq HTTP request helper 49 | func (c *Client) MakeReq(url string) ([]byte, error) { 50 | req, err := http.NewRequest("GET", url, nil) 51 | 52 | if err != nil { 53 | return nil, err 54 | } 55 | resp, err := doReq(req, c.httpClient) 56 | if err != nil { 57 | return nil, err 58 | } 59 | return resp, err 60 | } 61 | 62 | // API 63 | 64 | // Ping /ping endpoint 65 | func (c *Client) Ping() (*types.Ping, error) { 66 | url := fmt.Sprintf("%s/ping", baseURL) 67 | resp, err := c.MakeReq(url) 68 | if err != nil { 69 | return nil, err 70 | } 71 | var data *types.Ping 72 | err = json.Unmarshal(resp, &data) 73 | if err != nil { 74 | return nil, err 75 | } 76 | return data, nil 77 | } 78 | 79 | // SimpleSinglePrice /simple/price Single ID and Currency (ids, vs_currency) 80 | func (c *Client) SimpleSinglePrice(id string, vsCurrency string) (*types.SimpleSinglePrice, error) { 81 | idParam := []string{strings.ToLower(id)} 82 | vcParam := []string{strings.ToLower(vsCurrency)} 83 | 84 | t, err := c.SimplePrice(idParam, vcParam) 85 | if err != nil { 86 | return nil, err 87 | } 88 | curr := (*t)[id] 89 | if len(curr) == 0 { 90 | return nil, fmt.Errorf("id or vsCurrency not existed") 91 | } 92 | data := &types.SimpleSinglePrice{ID: id, Currency: vsCurrency, MarketPrice: curr[vsCurrency]} 93 | return data, nil 94 | } 95 | 96 | // SimplePrice /simple/price Multiple ID and Currency (ids, vs_currencies) 97 | func (c *Client) SimplePrice(ids []string, vsCurrencies []string) (*map[string]map[string]float32, error) { 98 | params := url.Values{} 99 | idsParam := strings.Join(ids[:], ",") 100 | vsCurrenciesParam := strings.Join(vsCurrencies[:], ",") 101 | 102 | params.Add("ids", idsParam) 103 | params.Add("vs_currencies", vsCurrenciesParam) 104 | 105 | url := fmt.Sprintf("%s/simple/price?%s", baseURL, params.Encode()) 106 | resp, err := c.MakeReq(url) 107 | if err != nil { 108 | return nil, err 109 | } 110 | 111 | t := make(map[string]map[string]float32) 112 | err = json.Unmarshal(resp, &t) 113 | if err != nil { 114 | return nil, err 115 | } 116 | 117 | return &t, nil 118 | } 119 | 120 | // SimpleSupportedVSCurrencies /simple/supported_vs_currencies 121 | func (c *Client) SimpleSupportedVSCurrencies() (*types.SimpleSupportedVSCurrencies, error) { 122 | url := fmt.Sprintf("%s/simple/supported_vs_currencies", baseURL) 123 | resp, err := c.MakeReq(url) 124 | if err != nil { 125 | return nil, err 126 | } 127 | var data *types.SimpleSupportedVSCurrencies 128 | err = json.Unmarshal(resp, &data) 129 | if err != nil { 130 | return nil, err 131 | } 132 | return data, nil 133 | } 134 | 135 | // CoinsList /coins/list 136 | func (c *Client) CoinsList() (*types.CoinList, error) { 137 | url := fmt.Sprintf("%s/coins/list", baseURL) 138 | resp, err := c.MakeReq(url) 139 | if err != nil { 140 | return nil, err 141 | } 142 | 143 | var data *types.CoinList 144 | err = json.Unmarshal(resp, &data) 145 | if err != nil { 146 | return nil, err 147 | } 148 | return data, nil 149 | } 150 | 151 | // CoinsMarket /coins/market 152 | func (c *Client) CoinsMarket(vsCurrency string, ids []string, order string, perPage int, page int, sparkline bool, priceChangePercentage []string) (*types.CoinsMarket, error) { 153 | if len(vsCurrency) == 0 { 154 | return nil, fmt.Errorf("vs_currency is required") 155 | } 156 | params := url.Values{} 157 | // vs_currency 158 | params.Add("vs_currency", vsCurrency) 159 | // order 160 | if len(order) == 0 { 161 | order = types.OrderTypeObject.MarketCapDesc 162 | } 163 | params.Add("order", order) 164 | // ids 165 | if len(ids) != 0 { 166 | idsParam := strings.Join(ids[:], ",") 167 | params.Add("ids", idsParam) 168 | } 169 | // per_page 170 | if perPage <= 0 || perPage > 250 { 171 | perPage = 100 172 | } 173 | params.Add("per_page", format.Int2String(perPage)) 174 | params.Add("page", format.Int2String(page)) 175 | // sparkline 176 | params.Add("sparkline", format.Bool2String(sparkline)) 177 | // price_change_percentage 178 | if len(priceChangePercentage) != 0 { 179 | priceChangePercentageParam := strings.Join(priceChangePercentage[:], ",") 180 | params.Add("price_change_percentage", priceChangePercentageParam) 181 | } 182 | url := fmt.Sprintf("%s/coins/markets?%s", baseURL, params.Encode()) 183 | resp, err := c.MakeReq(url) 184 | if err != nil { 185 | return nil, err 186 | } 187 | var data *types.CoinsMarket 188 | err = json.Unmarshal(resp, &data) 189 | if err != nil { 190 | return nil, err 191 | } 192 | return data, nil 193 | } 194 | 195 | // CoinsID /coins/{id} 196 | func (c *Client) CoinsID(id string, localization bool, tickers bool, marketData bool, communityData bool, developerData bool, sparkline bool) (*types.CoinsID, error) { 197 | 198 | if len(id) == 0 { 199 | return nil, fmt.Errorf("id is required") 200 | } 201 | params := url.Values{} 202 | params.Add("localization", format.Bool2String(sparkline)) 203 | params.Add("tickers", format.Bool2String(tickers)) 204 | params.Add("market_data", format.Bool2String(marketData)) 205 | params.Add("community_data", format.Bool2String(communityData)) 206 | params.Add("developer_data", format.Bool2String(developerData)) 207 | params.Add("sparkline", format.Bool2String(sparkline)) 208 | url := fmt.Sprintf("%s/coins/%s?%s", baseURL, id, params.Encode()) 209 | resp, err := c.MakeReq(url) 210 | if err != nil { 211 | return nil, err 212 | } 213 | 214 | var data *types.CoinsID 215 | err = json.Unmarshal(resp, &data) 216 | if err != nil { 217 | return nil, err 218 | } 219 | return data, nil 220 | } 221 | 222 | // CoinsIDTickers /coins/{id}/tickers 223 | func (c *Client) CoinsIDTickers(id string, page int) (*types.CoinsIDTickers, error) { 224 | if len(id) == 0 { 225 | return nil, fmt.Errorf("id is required") 226 | } 227 | params := url.Values{} 228 | if page > 0 { 229 | params.Add("page", format.Int2String(page)) 230 | } 231 | url := fmt.Sprintf("%s/coins/%s/tickers?%s", baseURL, id, params.Encode()) 232 | resp, err := c.MakeReq(url) 233 | if err != nil { 234 | return nil, err 235 | } 236 | var data *types.CoinsIDTickers 237 | err = json.Unmarshal(resp, &data) 238 | if err != nil { 239 | return nil, err 240 | } 241 | return data, nil 242 | } 243 | 244 | // CoinsIDHistory /coins/{id}/history?date={date}&localization=false 245 | func (c *Client) CoinsIDHistory(id string, date string, localization bool) (*types.CoinsIDHistory, error) { 246 | if len(id) == 0 || len(date) == 0 { 247 | return nil, fmt.Errorf("id and date is required") 248 | } 249 | params := url.Values{} 250 | params.Add("date", date) 251 | params.Add("localization", format.Bool2String(localization)) 252 | 253 | url := fmt.Sprintf("%s/coins/%s/history?%s", baseURL, id, params.Encode()) 254 | resp, err := c.MakeReq(url) 255 | if err != nil { 256 | return nil, err 257 | } 258 | var data *types.CoinsIDHistory 259 | err = json.Unmarshal(resp, &data) 260 | if err != nil { 261 | return nil, err 262 | } 263 | return data, nil 264 | } 265 | 266 | // CoinsIDMarketChart /coins/{id}/market_chart?vs_currency={usd, eur, jpy, etc.}&days={1,14,30,max} 267 | func (c *Client) CoinsIDMarketChart(id string, vs_currency string, days string) (*types.CoinsIDMarketChart, error) { 268 | if len(id) == 0 || len(vs_currency) == 0 || len(days) == 0 { 269 | return nil, fmt.Errorf("id, vs_currency, and days is required") 270 | } 271 | 272 | params := url.Values{} 273 | params.Add("vs_currency", vs_currency) 274 | params.Add("days", days) 275 | 276 | url := fmt.Sprintf("%s/coins/%s/market_chart?%s", baseURL, id, params.Encode()) 277 | resp, err := c.MakeReq(url) 278 | if err != nil { 279 | return nil, err 280 | } 281 | 282 | m := types.CoinsIDMarketChart{} 283 | err = json.Unmarshal(resp, &m) 284 | if err != nil { 285 | return &m, err 286 | } 287 | 288 | return &m, nil 289 | } 290 | 291 | // CoinsIDStatusUpdates 292 | 293 | // CoinsIDContractAddress https://api.coingecko.com/api/v3/coins/{id}/contract/{contract_address} 294 | // func CoinsIDContractAddress(id string, address string) (nil, error) { 295 | // url := fmt.Sprintf("%s/coins/%s/contract/%s", baseURL, id, address) 296 | // resp, err := request.MakeReq(url) 297 | // if err != nil { 298 | // return nil, err 299 | // } 300 | // } 301 | 302 | // EventsCountries https://api.coingecko.com/api/v3/events/countries 303 | func (c *Client) EventsCountries() ([]types.EventCountryItem, error) { 304 | url := fmt.Sprintf("%s/events/countries", baseURL) 305 | resp, err := c.MakeReq(url) 306 | if err != nil { 307 | return nil, err 308 | } 309 | var data *types.EventsCountries 310 | err = json.Unmarshal(resp, &data) 311 | if err != nil { 312 | return nil, err 313 | } 314 | return data.Data, nil 315 | 316 | } 317 | 318 | // EventsTypes https://api.coingecko.com/api/v3/events/types 319 | func (c *Client) EventsTypes() (*types.EventsTypes, error) { 320 | url := fmt.Sprintf("%s/events/types", baseURL) 321 | resp, err := c.MakeReq(url) 322 | if err != nil { 323 | return nil, err 324 | } 325 | var data *types.EventsTypes 326 | err = json.Unmarshal(resp, &data) 327 | if err != nil { 328 | return nil, err 329 | } 330 | return data, nil 331 | 332 | } 333 | 334 | // ExchangeRates https://api.coingecko.com/api/v3/exchange_rates 335 | func (c *Client) ExchangeRates() (*types.ExchangeRatesItem, error) { 336 | url := fmt.Sprintf("%s/exchange_rates", baseURL) 337 | resp, err := c.MakeReq(url) 338 | if err != nil { 339 | return nil, err 340 | } 341 | var data *types.ExchangeRatesResponse 342 | err = json.Unmarshal(resp, &data) 343 | if err != nil { 344 | return nil, err 345 | } 346 | return &data.Rates, nil 347 | } 348 | 349 | // Global https://api.coingecko.com/api/v3/global 350 | func (c *Client) Global() (*types.Global, error) { 351 | url := fmt.Sprintf("%s/global", baseURL) 352 | resp, err := c.MakeReq(url) 353 | if err != nil { 354 | return nil, err 355 | } 356 | var data *types.GlobalResponse 357 | err = json.Unmarshal(resp, &data) 358 | if err != nil { 359 | return nil, err 360 | } 361 | return &data.Data, nil 362 | } 363 | -------------------------------------------------------------------------------- /v3/types/model.go: -------------------------------------------------------------------------------- 1 | package types 2 | 3 | // OrderType 4 | 5 | // OrderType in CoinGecko 6 | type OrderType struct { 7 | MarketCapDesc string 8 | MarketCapAsc string 9 | GeckoDesc string 10 | GeckoAsc string 11 | VolumeAsc string 12 | VolumeDesc string 13 | } 14 | 15 | // OrderTypeObject for certain order 16 | var OrderTypeObject = &OrderType{ 17 | MarketCapDesc: "market_cap_desc", 18 | MarketCapAsc: "market_cap_asc", 19 | GeckoDesc: "gecko_desc", 20 | GeckoAsc: "gecko_asc", 21 | VolumeAsc: "volume_asc", 22 | VolumeDesc: "volume_desc", 23 | } 24 | 25 | // PriceChangePercentage 26 | 27 | // PriceChangePercentage in different amount of time 28 | type PriceChangePercentage struct { 29 | PCP1h string 30 | PCP24h string 31 | PCP7d string 32 | PCP14d string 33 | PCP30d string 34 | PCP200d string 35 | PCP1y string 36 | } 37 | 38 | // PriceChangePercentageObject for different amount of time 39 | var PriceChangePercentageObject = &PriceChangePercentage{ 40 | PCP1h: "1h", 41 | PCP24h: "24h", 42 | PCP7d: "7d", 43 | PCP14d: "14d", 44 | PCP30d: "30d", 45 | PCP200d: "200d", 46 | PCP1y: "1y", 47 | } 48 | 49 | // SHARED 50 | // coinBaseStruct [private] 51 | type coinBaseStruct struct { 52 | ID string `json:"id"` 53 | Symbol string `json:"symbol"` 54 | Name string `json:"name"` 55 | } 56 | 57 | // AllCurrencies map all currencies (USD, BTC) to float64 58 | type AllCurrencies map[string]float64 59 | 60 | // LocalizationItem map all locale (en, zh) into respective string 61 | type LocalizationItem map[string]string 62 | 63 | // TYPES 64 | 65 | // DescriptionItem map all description (in locale) into respective string 66 | type DescriptionItem map[string]string 67 | 68 | // LinksItem map all links 69 | type LinksItem map[string]interface{} 70 | 71 | // ChartItem 72 | type ChartItem [2]float32 73 | 74 | // MarketDataItem map all market data item 75 | type MarketDataItem struct { 76 | CurrentPrice AllCurrencies `json:"current_price"` 77 | ROI *ROIItem `json:"roi"` 78 | ATH AllCurrencies `json:"ath"` 79 | ATHChangePercentage AllCurrencies `json:"ath_change_percentage"` 80 | ATHDate map[string]string `json:"ath_date"` 81 | ATL AllCurrencies `json:"atl"` 82 | ATLChangePercentage AllCurrencies `json:"atl_change_percentage"` 83 | ATLDate map[string]string `json:"atl_date"` 84 | MarketCap AllCurrencies `json:"market_cap"` 85 | MarketCapRank uint16 `json:"market_cap_rank"` 86 | TotalVolume AllCurrencies `json:"total_volume"` 87 | High24 AllCurrencies `json:"high_24h"` 88 | Low24 AllCurrencies `json:"low_24h"` 89 | PriceChange24h float64 `json:"price_change_24h"` 90 | PriceChangePercentage24h float64 `json:"price_change_percentage_24h"` 91 | PriceChangePercentage7d float64 `json:"price_change_percentage_7d"` 92 | PriceChangePercentage14d float64 `json:"price_change_percentage_14d"` 93 | PriceChangePercentage30d float64 `json:"price_change_percentage_30d"` 94 | PriceChangePercentage60d float64 `json:"price_change_percentage_60d"` 95 | PriceChangePercentage200d float64 `json:"price_change_percentage_200d"` 96 | PriceChangePercentage1y float64 `json:"price_change_percentage_1y"` 97 | MarketCapChange24h float64 `json:"market_cap_change_24h"` 98 | MarketCapChangePercentage24h float64 `json:"market_cap_change_percentage_24h"` 99 | PriceChange24hInCurrency AllCurrencies `json:"price_change_24h_in_currency"` 100 | PriceChangePercentage1hInCurrency AllCurrencies `json:"price_change_percentage_1h_in_currency"` 101 | PriceChangePercentage24hInCurrency AllCurrencies `json:"price_change_percentage_24h_in_currency"` 102 | PriceChangePercentage7dInCurrency AllCurrencies `json:"price_change_percentage_7d_in_currency"` 103 | PriceChangePercentage14dInCurrency AllCurrencies `json:"price_change_percentage_14d_in_currency"` 104 | PriceChangePercentage30dInCurrency AllCurrencies `json:"price_change_percentage_30d_in_currency"` 105 | PriceChangePercentage60dInCurrency AllCurrencies `json:"price_change_percentage_60d_in_currency"` 106 | PriceChangePercentage200dInCurrency AllCurrencies `json:"price_change_percentage_200d_in_currency"` 107 | PriceChangePercentage1yInCurrency AllCurrencies `json:"price_change_percentage_1y_in_currency"` 108 | MarketCapChange24hInCurrency AllCurrencies `json:"market_cap_change_24h_in_currency"` 109 | MarketCapChangePercentage24hInCurrency AllCurrencies `json:"market_cap_change_percentage_24h_in_currency"` 110 | TotalSupply *float64 `json:"total_supply"` 111 | CirculatingSupply float64 `json:"circulating_supply"` 112 | Sparkline *SparklineItem `json:"sparkline_7d"` 113 | LastUpdated string `json:"last_updated"` 114 | } 115 | 116 | // CommunityDataItem map all community data item 117 | type CommunityDataItem struct { 118 | FacebookLikes *uint `json:"facebook_likes"` 119 | TwitterFollowers *uint `json:"twitter_followers"` 120 | RedditAveragePosts48h *float64 `json:"reddit_average_posts_48h"` 121 | RedditAverageComments48h *float64 `json:"reddit_average_comments_48h"` 122 | RedditSubscribers *uint `json:"reddit_subscribers"` 123 | RedditAccountsActive48h *interface{} `json:"reddit_accounts_active_48h"` 124 | TelegramChannelUserCount *uint `json:"telegram_channel_user_count"` 125 | } 126 | 127 | // DeveloperDataItem map all developer data item 128 | type DeveloperDataItem struct { 129 | Forks *uint `json:"forks"` 130 | Stars *uint `json:"stars"` 131 | Subscribers *uint `json:"subscribers"` 132 | TotalIssues *uint `json:"total_issues"` 133 | ClosedIssues *uint `json:"closed_issues"` 134 | PRMerged *uint `json:"pull_requests_merged"` 135 | PRContributors *uint `json:"pull_request_contributors"` 136 | CommitsCount4Weeks *uint `json:"commit_count_4_weeks"` 137 | } 138 | 139 | // PublicInterestItem map all public interest item 140 | type PublicInterestItem struct { 141 | AlexaRank uint `json:"alexa_rank"` 142 | BingMatches uint `json:"bing_matches"` 143 | } 144 | 145 | // ImageItem struct for all sizes of image 146 | type ImageItem struct { 147 | Thumb string `json:"thumb"` 148 | Small string `json:"small"` 149 | Large string `json:"large"` 150 | } 151 | 152 | // ROIItem ROI Item 153 | type ROIItem struct { 154 | Times float64 `json:"times"` 155 | Currency string `json:"currency"` 156 | Percentage float64 `json:"percentage"` 157 | } 158 | 159 | // SparklineItem for sparkline 160 | type SparklineItem struct { 161 | Price []float64 `json:"price"` 162 | } 163 | 164 | // TickerItem for ticker 165 | type TickerItem struct { 166 | Base string `json:"base"` 167 | Target string `json:"target"` 168 | Market struct { 169 | Name string `json:"name"` 170 | Identifier string `json:"identifier"` 171 | TradingIncentive bool `json:"has_trading_incentive"` 172 | } `json:"market"` 173 | Last float64 `json:"last"` 174 | ConvertedLast map[string]float64 `json:"converted_last"` 175 | Volume float64 `json:"volume"` 176 | ConvertedVolume map[string]float64 `json:"converted_volume"` 177 | Timestamp string `json:"timestamp"` 178 | IsAnomaly bool `json:"is_anomaly"` 179 | IsStale bool `json:"is_stale"` 180 | CoinID string `json:"coin_id"` 181 | } 182 | 183 | // StatusUpdateItem for BEAM 184 | type StatusUpdateItem struct { 185 | Description string `json:"description"` 186 | Category string `json:"category"` 187 | CreatedAt string `json:"created_at"` 188 | User string `json:"user"` 189 | UserTitle string `json:"user_title"` 190 | Pin bool `json:"pin"` 191 | Project struct { 192 | coinBaseStruct 193 | Type string `json:"type"` 194 | Image ImageItem `json:"image"` 195 | } `json:"project"` 196 | } 197 | 198 | // CoinsListItem item in CoinList 199 | type CoinsListItem struct { 200 | coinBaseStruct 201 | } 202 | 203 | // CoinsMarketItem item in CoinMarket 204 | type CoinsMarketItem struct { 205 | coinBaseStruct 206 | Image string `json:"image"` 207 | CurrentPrice float64 `json:"current_price"` 208 | MarketCap float64 `json:"market_cap"` 209 | MarketCapRank int16 `json:"market_cap_rank"` 210 | TotalVolume float64 `json:"total_volume"` 211 | High24 float64 `json:"high_24h"` 212 | Low24 float64 `json:"low_24h"` 213 | PriceChange24h float64 `json:"price_change_24h"` 214 | PriceChangePercentage24h float64 `json:"price_change_percentage_24h"` 215 | MarketCapChange24h float64 `json:"market_cap_change_24h"` 216 | MarketCapChangePercentage24h float64 `json:"market_cap_change_percentage_24h"` 217 | CirculatingSupply float64 `json:"circulating_supply"` 218 | TotalSupply float64 `json:"total_supply"` 219 | ATH float64 `json:"ath"` 220 | ATHChangePercentage float64 `json:"ath_change_percentage"` 221 | ATHDate string `json:"ath_date"` 222 | ROI *ROIItem `json:"roi"` 223 | LastUpdated string `json:"last_updated"` 224 | SparklineIn7d *SparklineItem `json:"sparkline_in_7d"` 225 | PriceChangePercentage1hInCurrency *float64 `json:"price_change_percentage_1h_in_currency"` 226 | PriceChangePercentage24hInCurrency *float64 `json:"price_change_percentage_24h_in_currency"` 227 | PriceChangePercentage7dInCurrency *float64 `json:"price_change_percentage_7d_in_currency"` 228 | PriceChangePercentage14dInCurrency *float64 `json:"price_change_percentage_14d_in_currency"` 229 | PriceChangePercentage30dInCurrency *float64 `json:"price_change_percentage_30d_in_currency"` 230 | PriceChangePercentage200dInCurrency *float64 `json:"price_change_percentage_200d_in_currency"` 231 | PriceChangePercentage1yInCurrency *float64 `json:"price_change_percentage_1y_in_currency"` 232 | } 233 | 234 | // EventCountryItem item in EventsCountries 235 | type EventCountryItem struct { 236 | Country string `json:"country"` 237 | Code string `json:"code"` 238 | } 239 | 240 | // ExchangeRatesItem item in ExchangeRate 241 | type ExchangeRatesItem map[string]ExchangeRatesItemStruct 242 | 243 | // ExchangeRatesItemStruct struct in ExchangeRateItem 244 | type ExchangeRatesItemStruct struct { 245 | Name string `json:"name"` 246 | Unit string `json:"unit"` 247 | Value float64 `json:"value"` 248 | Type string `json:"type"` 249 | } 250 | 251 | // Global for data of /global 252 | type Global struct { 253 | ActiveCryptocurrencies uint16 `json:"active_cryptocurrencies"` 254 | UpcomingICOs uint16 `json:"upcoming_icos"` 255 | EndedICOs uint16 `json:"ended_icos"` 256 | Markets uint16 `json:"markets"` 257 | MarketCapChangePercentage24hUSD float32 `json:"market_cap_change_percentage_24h_usd"` 258 | TotalMarketCap AllCurrencies `json:"total_market_cap"` 259 | TotalVolume AllCurrencies `json:"total_volume"` 260 | MarketCapPercentage AllCurrencies `json:"market_cap_percentage"` 261 | UpdatedAt int64 `json:"updated_at"` 262 | } 263 | -------------------------------------------------------------------------------- /gogecko.svg: -------------------------------------------------------------------------------- 1 | --------------------------------------------------------------------------------