Skip to content

Commit

Permalink
Merge pull request #1 from DiFronzo/campsite
Browse files Browse the repository at this point in the history
LGTM
  • Loading branch information
DiFronzo authored Sep 11, 2021
2 parents 5ed13ba + 05f8b53 commit d46e0a0
Show file tree
Hide file tree
Showing 18 changed files with 647 additions and 306 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</h1>
<p align="center">Client for the blockchair.com API using GO.

<p align="center"><a href="https://github.com/DiFronzo/blockchair/releases" target="_blank"><img src="https://img.shields.io/badge/version-v0.0.1-blue?style=for-the-badge&logo=none" alt="BC-API version" /></a>&nbsp;<a href="https://golang.org/" target="_blank"><img src="https://img.shields.io/badge/GO-1.17+-00ADD8?style=for-the-badge&logo=GO" alt="go version" /></a>&nbsp;<img src="https://img.shields.io/badge/license-MIT-red?style=for-the-badge&logo=none" alt="license" />&nbsp;<img alt="code size" src="https://img.shields.io/github/languages/code-size/difronzo/blockchair?style=for-the-badge&logo=none">&nbsp;<a href="https://goreportcard.com/report/github.com/DiFronzo/blockchair" target="_blank"><img src="https://goreportcard.com/badge/github.com/DiFronzo/blockchair?style=for-the-badge&logo=none" alt="GO report" />&nbsp;<a href="https://pkg.go.dev/github.com/DiFronzo/blockchair" target="_blank"><img src="https://img.shields.io/badge/GoDoc-reference-blue?style=for-the-badge&logo=go" alt="GoDoc" /></a></p>
<p align="center"><a href="https://github.com/DiFronzo/blockchair/releases" target="_blank"><img src="https://img.shields.io/badge/version-v0.1.0-blue?style=for-the-badge&logo=none" alt="BC-API version" /></a>&nbsp;<a href="https://golang.org/" target="_blank"><img src="https://img.shields.io/badge/GO-1.17+-00ADD8?style=for-the-badge&logo=GO" alt="go version" /></a>&nbsp;<img src="https://img.shields.io/badge/license-MIT-red?style=for-the-badge&logo=none" alt="license" />&nbsp;<img alt="code size" src="https://img.shields.io/github/languages/code-size/difronzo/blockchair?style=for-the-badge&logo=none">&nbsp;<a href="https://goreportcard.com/report/github.com/DiFronzo/blockchair" target="_blank"><img src="https://goreportcard.com/badge/github.com/DiFronzo/blockchair?style=for-the-badge&logo=none" alt="GO report" />&nbsp;<a href="https://pkg.go.dev/github.com/DiFronzo/blockchair" target="_blank"><img src="https://img.shields.io/badge/GoDoc-reference-blue?style=for-the-badge&logo=go" alt="GoDoc" /></a></p>



Expand All @@ -21,7 +21,7 @@ go version
To quickly start using the module run the following command for installation.

```bash
go get -u github.com/DiFronzo/blockchair
go install github.com/DiFronzo/blockchair@latest
```

That's all you need to know to start! 🎉
Expand Down
149 changes: 87 additions & 62 deletions address.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
package blockchair

import (
"fmt"
"log"
"strings"
)

// DataAddress includes full server response to address request.
type DataAddress struct {
Data map[string]AddressInfo `json:"data"`
Context ContextAddress `json:"context"`
}

// DataAddressEth includes full server response to address request for Ethereum.
type DataAddressEth struct {
Data map[string]AddressInfoEth `json:"data"`
Context ContextAddress `json:"context"`
}

// DataAddresses includes full server response to addresses request.
type DataAddresses struct {
Data AddressesInfo `json:"data"`
Context ContextAddress `json:"context"`
}

// DataXpub includes full server response to xpub request.
type DataXpub struct {
Data map[string]XpubInfo `json:"data"`
Context ContextAddress `json:"context"`
}

// XpubInfo describes the outer structure of the x/z/v-pub.
type XpubInfo struct {
Xpub Xpub `json:"xpub"`
Addresses map[string]Address `json:"addresses"`
Transactions []string `json:"transactions"`
Utxo []Utxo `json:"utxo"`
}

// Xpub describes the inner structure of the x/z/v-pub.
type Xpub struct {
AddressCount int `json:"address_count"`
Balance int `json:"balance"`
Expand All @@ -41,25 +45,28 @@ type Xpub struct {
Spent int `json:"spent"`
OutputCount int `json:"output_count"`
UnspentOutputCount int `json:"unspent_output_count"`
FirstSeenReceiving string `json:"first_seen_receiving"`
LastSeenReceiving string `json:"last_seen_receiving"`
FirstSeenSpending string `json:"first_seen_spending"`
LastSeenSpending string `json:"last_seen_spending"`
FirstSeenReceiving string `json:"first_seen_receiving,omitempty"`
LastSeenReceiving string `json:"last_seen_receiving,omitempty"`
FirstSeenSpending string `json:"first_seen_spending,omitempty"`
LastSeenSpending string `json:"last_seen_spending,omitempty"`
TransactionCount int `json:"transaction_count"`
}

// AddressesInfo describes the outer structure of the addresses.
type AddressesInfo struct {
Set Set `json:"set"`
Addresses map[string]Address `json:"addresses"`
Transactions []string `json:"transactions"`
Utxo []Utxo `json:"utxo"`
}

// AddressInfoEth the structure of the set of address and calls for Ethereum.
type AddressInfoEth struct {
Address AddressEth `json:"address"`
Calls []CallsAddress `json:"calls"`
}

// Set the structure of the set for Bitcoin-like address.
type Set struct {
AddressCount int `json:"address_count"`
Balance int64 `json:"balance"`
Expand All @@ -75,25 +82,27 @@ type Set struct {
TransactionCount int `json:"transaction_count"`
}

// AddressInfo structure of the set of address, transactions, and utxo.
type AddressInfo struct {
Address Address `json:"address"`
Transactions []string `json:"transactions"`
Utxo []Utxo `json:"utxo"`
}

// AddressEth is the structure of one specific Ethereum address.
type AddressEth struct {
Type string `json:"type"`
ContractCodeHex string `json:"contract_code_hex"`
ContractCreated string `json:"contract_created"`
ContractDestroyed string `json:"contract_destroyed"`
ContractCodeHex string `json:"contract_code_hex,omitempty"`
ContractCreated string `json:"contract_created,omitempty"`
ContractDestroyed string `json:"contract_destroyed,omitempty"`
Balance string `json:"balance"`
BalanceUsd float64 `json:"balance_usd"`
BalanceUsd float32 `json:"balance_usd"`
ReceivedApproximate string `json:"received_approximate"`
ReceivedUsd float64 `json:"received_usd"`
ReceivedUsd float32 `json:"received_usd"`
SpentApproximate string `json:"spent_approximate"`
SpentUsd float64 `json:"spent_usd"`
SpentUsd float32 `json:"spent_usd"`
FeesApproximate string `json:"fees_approximate"`
FeesUsd float64 `json:"fees_usd"`
FeesUsd float32 `json:"fees_usd"`
ReceivingCallCount int `json:"receiving_call_count"`
SpendingCallCount int `json:"spending_call_count"`
CallCount int `json:"call_count"`
Expand All @@ -102,118 +111,134 @@ type AddressEth struct {
LastSeenReceiving string `json:"last_seen_receiving"`
FirstSeenSpending string `json:"first_seen_spending"`
LastSeenSpending string `json:"last_seen_spending"`
Nonce int `json:"nonce"`
Nonce int `json:"nonce,omitempty"`
}

// Address the structure of one specific Bitcoin-like address.
type Address struct {
Path string `json:"path,omitempty"`
Type string `json:"type"`
ScriptHex string `json:"script_hex"`
Balance int64 `json:"balance"`
Balance float32 `json:"balance"`
BalanceUsd float32 `json:"balance_usd"`
Received int64 `json:"received"`
Received float32 `json:"received"`
ReceivedUsd float32 `json:"received_usd"`
Spent int `json:"spent"`
Spent float32 `json:"spent"`
SpentUsd float32 `json:"spent_usd"`
OutputCount int `json:"output_count"`
UnspentOutputCount int `json:"unspent_output_count"`
FirstSeenReceiving string `json:"first_seen_receiving"`
LastSeenReceiving string `json:"last_seen_receiving"`
FirstSeenSpending string `json:"first_seen_spending"`
LastSeenSpending string `json:"last_seen_spending"`
ScripthashType string `json:"scripthash_type"`
TransactionCount int `json:"transaction_count"`
FirstSeenReceiving string `json:"first_seen_receiving,omitempty"`
LastSeenReceiving string `json:"last_seen_receiving,omitempty"`
FirstSeenSpending string `json:"first_seen_spending,omitempty"`
LastSeenSpending string `json:"last_seen_spending,omitempty"`
ScripthashType string `json:"scripthash_type,omitempty"`
TransactionCount int `json:"transaction_count,omitempty"`
}

// Utxo the structure of utxo.
type Utxo struct {
BlockID int `json:"block_id"`
TransactionHash string `json:"transaction_hash"`
Index int `json:"index"`
Value int `json:"value"`
}

// CallsAddress is the structures of calls.
type CallsAddress struct {
BlockID int `json:"block_id"`
TransactionHash string `json:"transaction_hash"`
TransactionHash string `json:"transaction_hash,omitempty"`
Index string `json:"index"`
Time string `json:"time"`
Sender string `json:"sender"`
Sender string `json:"sender,omitempty"`
Recipient string `json:"recipient"`
Value float64 `json:"value"`
ValueUsd float64 `json:"value_usd"`
Transferred bool `json:"transferred"`
}

// ContextAddress TODO! FIX "CHECKED" INTO A SLICE
// the structure of context for address(es).
type ContextAddress struct {
Code int `json:"code"`
Source string `json:"source"`
Limit string `json:"limit"`
Offset string `json:"offset"`
Results int `json:"results"`
Checked []string `json:"checked"`
Checked []string `json:"checked,omitempty"`
State int `json:"state"`
MarketPriceUsd float32 `json:"market_price_usd"`
Cache *Cache `json:"cache"`
API *Api `json:"api"`
API *API `json:"api"`
Server string `json:"server"`
Time float32 `json:"time"`
RenderTime float32 `json:"render_time"`
FullTime float32 `json:"full_time"`
RequestCost float32 `json:"request_cost"`
}

// GetAddress get the address by type of crypto and address hash.
func (c *Client) GetAddress(crypto string, address string) (*DataAddress, error) {
if !Contains(GetSupportedCrypto(), crypto) {
log.Fatalf("error: %v is not supported", crypto)
}
rsp := &DataAddress{}
var path = crypto + "/dashboards/address/" + address
e := c.loadResponse(path, rsp)
return c.GetAddressAdv(crypto, address, nil)
}

if e != nil {
fmt.Print(e)
// GetAddressAdv get the address by type of crypto, address hash, and options.
func (c *Client) GetAddressAdv(crypto string, address string, options map[string]string) (resp *DataAddress, e error) {
if e = c.ValidateCrypto(crypto); e != nil {
return
}
return rsp, e

resp = &DataAddress{}
var path = crypto + "/dashboards/address/" + address
return resp, c.LoadResponse(path, resp, options)
}

// GetAddresses get the addresses by type of crypto and addresses hash.
func (c *Client) GetAddresses(crypto string, addresses []string) (*DataAddresses, error) {
if !Contains(GetSupportedCrypto(), crypto) {
log.Fatalf("error: %v is not supported", crypto)
}
rsp := &DataAddresses{}
var path = crypto + "/dashboards/addresses/" + strings.Join(addresses, ",")
e := c.loadResponse(path, rsp)
return c.GetAddressesAdv(crypto, addresses, nil)
}

if e != nil {
fmt.Print(e)
// GetAddressesAdv get the addresses by type of crypto, addresses hash and options.
func (c *Client) GetAddressesAdv(crypto string, addresses []string, options map[string]string) (resp *DataAddresses, e error) {
if e = c.ValidateCrypto(crypto); e != nil {
return
}
return rsp, e

resp = &DataAddresses{}
var path = crypto + "/dashboards/addresses/" + strings.Join(addresses, ",")
return resp, c.LoadResponse(path, resp, options)
}

// GetXpub get the xpub/ypub/zpub by type of crypto and the extended key.
// xpub (supported for all blockchains), ypub (supported for Bitcoin, Litecoin, Groestlcoin, and Bitcoin Testnet only) and zpub (supported for Bitcoin, Litecoin, Groestlcoin, and Bitcoin Testnet only)
func (c *Client) GetXpub(crypto string, extendedKey string) (*DataXpub, error) {
// xpub (supported for all blockchains), ypub (supported for Bitcoin, Litecoin, Groestlcoin, and Bitcoin Testnet only)
// zpub (supported for Bitcoin, Litecoin, Groestlcoin, and Bitcoin Testnet only)
rsp := &DataXpub{}
var path = crypto + "/dashboards/xpub/" + extendedKey
e := c.loadResponse(path, rsp)
return c.GetXpubAdv(crypto, extendedKey, nil)
}

if e != nil {
fmt.Print(e)
// GetXpubAdv get the xpub/ypub/zpub by type of crypto, the extended key, and options.
// xpub (supported for all blockchains), ypub (supported for Bitcoin, Litecoin, Groestlcoin, and Bitcoin Testnet only) and zpub (supported for Bitcoin, Litecoin, Groestlcoin, and Bitcoin Testnet only)
func (c *Client) GetXpubAdv(crypto string, extendedKey string, options map[string]string) (resp *DataXpub, e error) {
if e = c.ValidateCryptoBoth(crypto); e != nil {
return
}
return rsp, e

resp = &DataXpub{}
var path = crypto + "/dashboards/xpub/" + extendedKey
return resp, c.LoadResponse(path, resp, options)
}

// GetAddressEth get the address by type of crypto and address hash for Ethereum.
func (c *Client) GetAddressEth(crypto string, address string) (*DataAddressEth, error) {
if !Contains(GetSupportedCryptoEth(), crypto) {
log.Fatalf("error: %v is not supported", crypto)
return c.GetAddressEthAdv(crypto, address, nil)
}

// GetAddressEthAdv get the address by type of crypto, address hash, and options for Ethereum.
// TODO! Validate address
func (c *Client) GetAddressEthAdv(crypto string, address string, options map[string]string) (resp *DataAddressEth, e error) {
if e = c.ValidateCryptoEth(crypto); e != nil {
return
}

rsp := &DataAddressEth{}
resp = &DataAddressEth{}
var path = crypto + "/dashboards/address/" + address
e := c.loadResponse(path, rsp)

if e != nil {
fmt.Print(e)
}
return rsp, e
return resp, c.LoadResponse(path, resp, options)
}
12 changes: 8 additions & 4 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func TestGetAddress(t *testing.T) {
}
for _, test := range tests {
t.Run(test.currency, func(t *testing.T) {
cl, _ := New(clientID)
cl := New()
cl.APIKey = clientID
_, e := cl.GetAddress(test.currency, test.address)
if e != nil {
t.Fatal(e)
Expand All @@ -50,7 +51,8 @@ func TestGetAddresses(t *testing.T) {
}
for _, test := range tests {
t.Run(test.currency, func(t *testing.T) {
cl, _ := New(clientID)
cl := New()
cl.APIKey = clientID
_, e := cl.GetAddresses(test.currency, test.address)
if e != nil {
t.Fatal(e)
Expand All @@ -71,7 +73,8 @@ func TestGetAddressEth(t *testing.T) {
}
for _, test := range tests {
t.Run(test.currency, func(t *testing.T) {
cl, _ := New(clientID)
cl := New()
cl.APIKey = clientID
_, e := cl.GetAddressEth(test.currency, test.address)
if e != nil {
t.Fatal(e)
Expand All @@ -81,7 +84,8 @@ func TestGetAddressEth(t *testing.T) {
}

func BenchmarkGetAddressUnmarshal(b *testing.B) {
cl, _ := New(clientID)
cl := New()
cl.APIKey = clientID
response, e := cl.GetAddress("bitcoin", "bc1qgdjqv0av3q56jvd82tkdjpy7gdp9ut8tlqmgrpmv24sq90ecnvqqjwvw97")
if e != nil {
b.Fatal(e)
Expand Down
Loading

0 comments on commit d46e0a0

Please sign in to comment.