Skip to content

Instantly share code, notes, and snippets.

@dgellow
Created March 28, 2018 17:37
Show Gist options
  • Save dgellow/0b53bb3e96cfdbe68c094097d64055b7 to your computer and use it in GitHub Desktop.
Save dgellow/0b53bb3e96cfdbe68c094097d64055b7 to your computer and use it in GitHub Desktop.
package cryptocoins
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"github.com/dgellow/cryptofolio/pkg/domain"
)
type RippleService struct{}
var _ BalancesFetcher = &RippleService{}
func NewRippleService() *RippleService {
return &RippleService{}
}
func (s *RippleService) FetchBalances(addr string) ([]domain.Balance, error) {
// See https://ripple.com/build/data-api-v2/#get-account-balances
resp, err := http.Get(
"https://data.ripple.com/v2/accounts/" + addr + "/balances",
)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var b struct {
Balances []struct {
Currency string `json:"currency"`
Value string `json:"value"`
} `json:"balances"`
Result string `json:"result"`
}
err = json.Unmarshal(body, &b)
if err != nil {
return nil, err
}
balances := []domain.Balance{}
for _, bal := range b.Balances {
value, err := strconv.ParseFloat(bal.Value, 64)
if err != nil {
return nil, err
}
// FIXME: Amount should be correctly represented
// without decimal, and use the correct Unit.
balances = append(balances, domain.Balance{
Amount: uint64(value),
Currency: "XRP (rounded value)",
Unit: 1,
})
}
return balances, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment