-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspendable_balance.go
76 lines (64 loc) · 1.88 KB
/
spendable_balance.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
package handcash
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// SpendableBalanceResponse is the balance response
type SpendableBalanceResponse struct {
SpendableSatoshiBalance uint64 `json:"spendableSatoshiBalance"`
SpendableFiatBalance float64 `json:"spendableFiatBalance"`
CurrencyCode CurrencyCode `json:"currencyCode"`
}
// GetSpendableBalance gets the user's spendable balance from the handcash connect API
func (c *Client) GetSpendableBalance(ctx context.Context, authToken string,
currencyCode CurrencyCode) (*SpendableBalanceResponse, error) {
// Make sure we have an auth token
if len(authToken) == 0 {
return nil, fmt.Errorf("missing auth token")
}
if len(currencyCode) == 0 {
return nil, fmt.Errorf("missing currency code")
}
// Get the signed request
signed, err := c.getSignedRequest(
http.MethodGet,
endpointGetSpendableBalanceRequest,
authToken,
&BalanceRequest{CurrencyCode: currencyCode},
currentISOTimestamp(),
)
if err != nil {
return nil, fmt.Errorf("error creating signed request: %w", err)
}
// Convert into bytes
var params []byte
if params, err = json.Marshal(
&BalanceRequest{CurrencyCode: currencyCode},
); err != nil {
return nil, err
}
// Make the HTTP request
response := httpRequest(
ctx,
c,
&httpPayload{
Data: params,
ExpectedStatus: http.StatusOK,
Method: signed.Method,
URL: signed.URI,
},
signed,
)
if response.Error != nil {
return nil, response.Error
}
spendableBalanceResponse := new(SpendableBalanceResponse)
if err = json.Unmarshal(response.BodyContents, &spendableBalanceResponse); err != nil {
return nil, fmt.Errorf("failed unmarshal %w", err)
} else if spendableBalanceResponse == nil || spendableBalanceResponse.CurrencyCode == "" {
return nil, fmt.Errorf("failed to get balance")
}
return spendableBalanceResponse, nil
}