-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoinbase.go
92 lines (73 loc) · 1.84 KB
/
coinbase.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"errors"
"fmt"
"log"
"os"
"strconv"
eth "github.com/ethereum/go-ethereum/common"
coinbase "github.com/preichenberger/go-gdax"
)
type ICoinbase interface {
BuyEther() (err error, filledSize float64)
SendEther(amount string, to eth.Address) error
GetEtherPrice() (float64, error)
}
type Coinbase struct {
client *coinbase.Client
}
func (c *Coinbase) Init() {
c.client = coinbase.NewClient(
os.Getenv("CoinbaseSecret"),
os.Getenv("CoinbaseKey"),
os.Getenv("CoinbasePassphrase"))
}
func (c *Coinbase) BuyEther() (err error, filledSize float64) {
log.Printf("Buy £%d worth of ETH on coinbase", EtherValueGBP)
// return nil, 0.1
order := coinbase.Order{
Type: "market",
Side: "buy",
ProductId: "ETH-GBP",
Funds: fmt.Sprintf("%d.00", EtherValueGBP),
}
result, err := c.client.CreateOrder(&order)
if err != nil {
return errors.New("Failed to buy Ether on Coinbase: " + err.Error()), 0
}
executedOrder, err := c.client.GetOrder(result.Id)
f, err := strconv.ParseFloat(executedOrder.FilledSize, 64)
if err != nil {
return err, 0
}
return nil, f
}
func (c *Coinbase) SendEther(amount string, to eth.Address) error {
log.Printf("Send %s ETH from Coinbase to %s", amount, to.Hex())
var params = CoinbaseWithdrawCryptoParams{
Amount: amount,
Currency: "ETH",
CryptoAddress: to.Hex(),
}
var result = CoinbaseWithdrawCryptoResult{}
_, err := c.client.Request(
"POST",
"/withdrawals/crypto",
params,
&result)
if err != nil {
return errors.New("Failed to transfer ETH from Coinbase to user: " + err.Error())
}
return nil
}
func (c *Coinbase) GetEtherPrice() (float64, error) {
b, err := coinbaseClient.client.GetBook("ETH-EUR", 1)
if err != nil {
return 0, err
}
f, err := strconv.ParseFloat(b.Asks[0].Price, 64)
if err != nil {
return 0, err
}
return f, nil
}