-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (57 loc) · 1.47 KB
/
Copy pathmain.go
File metadata and controls
66 lines (57 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Cryptohopper Go SDK — hello world.
//
// Authenticates with a bearer token from $CRYPTOHOPPER_TOKEN, then makes
// two calls: User.Get and Exchange.Ticker.
//
// Run:
// export CRYPTOHOPPER_TOKEN=your-40-char-bearer
// go run .
package main
import (
"context"
"errors"
"fmt"
"os"
cryptohopper "github.com/cryptohopper/cryptohopper-go-sdk"
)
func main() {
token := os.Getenv("CRYPTOHOPPER_TOKEN")
if token == "" {
fmt.Fprintln(os.Stderr, "Set CRYPTOHOPPER_TOKEN to a 40-char OAuth bearer first.")
os.Exit(1)
}
ch, err := cryptohopper.NewClient(token)
if err != nil {
fmt.Fprintln(os.Stderr, "client init failed:", err)
os.Exit(1)
}
ctx := context.Background()
me, err := ch.User.Get(ctx)
if err != nil {
var ce *cryptohopper.Error
if errors.As(err, &ce) {
fmt.Fprintf(os.Stderr, "API error [%s]: %s\n", ce.Code, ce.Message)
if ce.IPAddress != "" {
fmt.Fprintf(os.Stderr, " Server saw your IP as: %s\n", ce.IPAddress)
}
os.Exit(2)
}
fmt.Fprintln(os.Stderr, "transport error:", err)
os.Exit(2)
}
fmt.Printf("Logged in as %v\n", firstNonEmpty(me, "email", "username", "id"))
ticker, err := ch.Exchange.Ticker(ctx, "binance", "BTC/USDT")
if err != nil {
fmt.Fprintln(os.Stderr, "ticker error:", err)
os.Exit(2)
}
fmt.Printf("BTC/USDT last: %v\n", ticker["last"])
}
func firstNonEmpty(m map[string]any, keys ...string) any {
for _, k := range keys {
if v, ok := m[k]; ok && v != nil && v != "" {
return v
}
}
return "?"
}