add support for bitcoin
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
37
bitcoin/address.go
Normal file
37
bitcoin/address.go
Normal file
@ -0,0 +1,37 @@
|
||||
package bitcoin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type Address struct {
|
||||
Address string `json:"address"`
|
||||
ChainStats struct {
|
||||
FundedTxoCount int `json:"funded_txo_count"`
|
||||
FundedTxoSum int `json:"funded_txo_sum"`
|
||||
SpentTxoCount int `json:"spent_txo_count"`
|
||||
SpentTxoSum int `json:"spent_txo_sum"`
|
||||
TxCount int `json:"tx_count"`
|
||||
} `json:"chain_stats"`
|
||||
MempoolStats struct {
|
||||
FundedTxoCount int `json:"funded_txo_count"`
|
||||
FundedTxoSum int `json:"funded_txo_sum"`
|
||||
SpentTxoCount int `json:"spent_txo_count"`
|
||||
SpentTxoSum int `json:"spent_txo_sum"`
|
||||
TxCount int `json:"tx_count"`
|
||||
} `json:"mempool_stats"`
|
||||
}
|
||||
|
||||
// GetAddress returns an Address struct populated with data from blockstream.info
|
||||
// for a given BTC address
|
||||
func (c *Client) GetAddress(address string) (*Address, error) {
|
||||
addressResponse := &Address{}
|
||||
|
||||
err := c.get(fmt.Sprintf("address/%s", address), addressResponse, url.Values{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return addressResponse, nil
|
||||
}
|
81
bitcoin/client.go
Normal file
81
bitcoin/client.go
Normal file
@ -0,0 +1,81 @@
|
||||
package bitcoin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
const apiBaseURL = "https://blockstream.info/api/"
|
||||
|
||||
// A client is the structure that will be used to consume the API
|
||||
// endpoints. It holds the login credentials, http client/transport,
|
||||
// rate limit information, and the login session timer.
|
||||
type Client struct {
|
||||
httpClient *http.Client
|
||||
transport *http.Transport
|
||||
}
|
||||
|
||||
// Send an HTTP GET request, and return the processed response
|
||||
func (c *Client) get(endpoint string, out interface{}, query url.Values) error {
|
||||
req, err := http.NewRequest("GET", apiBaseURL+endpoint+"?"+query.Encode(), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create new GET request: %v", err)
|
||||
}
|
||||
|
||||
res, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("http GET request failed: %v", err)
|
||||
}
|
||||
|
||||
err = c.processResponse(res, out)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to process response: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// processResponse takes the body of an HTTP response, and either returns
|
||||
// the error code, or unmarshalls the JSON response, extracts
|
||||
// rate limit info, and places it into the object
|
||||
// output parameter. This function closes the response body after reading it.
|
||||
func (c *Client) processResponse(res *http.Response, out interface{}) error {
|
||||
body, err := io.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read response body: %v", err)
|
||||
}
|
||||
|
||||
if res.StatusCode != 200 && res.StatusCode != 201 {
|
||||
return fmt.Errorf("unexpected http status code '%d': %v", res.StatusCode, err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, out)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to unmarshal response body: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewClient is the factory function for clients
|
||||
func NewClient() (*Client, error) {
|
||||
transport := &http.Transport{
|
||||
ResponseHeaderTimeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
|
||||
// Create a new client
|
||||
c := &Client{
|
||||
httpClient: client,
|
||||
transport: transport,
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
43
bitcoin/fiat.go
Normal file
43
bitcoin/fiat.go
Normal file
@ -0,0 +1,43 @@
|
||||
package bitcoin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// example: https://api.coinconvert.net/convert/btc/cad?amount=1
|
||||
const fiatConvertURL = "https://api.coinconvert.net/convert/btc/cad"
|
||||
|
||||
type FiatConversion struct {
|
||||
Status string `json:"status"`
|
||||
BTC int `json:"BTC"`
|
||||
CAD float64 `json:"CAD"`
|
||||
}
|
||||
|
||||
// BTC to CAD FIAT conversion - accepts an
|
||||
// amount in satoshi's and returns a CAD amount * 1000
|
||||
func (c *Client) ConvertBTCToCAD(amount int) (int, error) {
|
||||
|
||||
fiatConversion := &FiatConversion{}
|
||||
|
||||
req, err := http.NewRequest("GET", fiatConvertURL+"?amount=1", nil)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to create new GET request: %v", err)
|
||||
}
|
||||
|
||||
res, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("http GET request failed: %v", err)
|
||||
}
|
||||
|
||||
err = c.processResponse(res, fiatConversion)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to process response: %v", err)
|
||||
}
|
||||
|
||||
if fiatConversion.Status != "success" {
|
||||
return 0, fmt.Errorf("fiat conversion status was '%s' but expected 'success'", fiatConversion.Status)
|
||||
}
|
||||
|
||||
return (amount * int(fiatConversion.CAD*1000)) / 100000000, nil // one BTC = one hundred million satoshi's
|
||||
}
|
Reference in New Issue
Block a user