Skip to content

Commit d46e0a0

Browse files
authored
Merge pull request #1 from DiFronzo/campsite
LGTM
2 parents 5ed13ba + 05f8b53 commit d46e0a0

18 files changed

Lines changed: 647 additions & 306 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</h1>
55
<p align="center">Client for the blockchair.com API using GO.
66

7-
<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>
7+
<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>
88

99

1010

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

2323
```bash
24-
go get -u github.com/DiFronzo/blockchair
24+
go install github.com/DiFronzo/blockchair@latest
2525
```
2626

2727
That's all you need to know to start! 🎉

address.go

Lines changed: 87 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
11
package blockchair
22

33
import (
4-
"fmt"
5-
"log"
64
"strings"
75
)
86

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

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

19+
// DataAddresses includes full server response to addresses request.
1920
type DataAddresses struct {
2021
Data AddressesInfo `json:"data"`
2122
Context ContextAddress `json:"context"`
2223
}
2324

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

31+
// XpubInfo describes the outer structure of the x/z/v-pub.
2932
type XpubInfo struct {
3033
Xpub Xpub `json:"xpub"`
3134
Addresses map[string]Address `json:"addresses"`
3235
Transactions []string `json:"transactions"`
3336
Utxo []Utxo `json:"utxo"`
3437
}
3538

39+
// Xpub describes the inner structure of the x/z/v-pub.
3640
type Xpub struct {
3741
AddressCount int `json:"address_count"`
3842
Balance int `json:"balance"`
@@ -41,25 +45,28 @@ type Xpub struct {
4145
Spent int `json:"spent"`
4246
OutputCount int `json:"output_count"`
4347
UnspentOutputCount int `json:"unspent_output_count"`
44-
FirstSeenReceiving string `json:"first_seen_receiving"`
45-
LastSeenReceiving string `json:"last_seen_receiving"`
46-
FirstSeenSpending string `json:"first_seen_spending"`
47-
LastSeenSpending string `json:"last_seen_spending"`
48+
FirstSeenReceiving string `json:"first_seen_receiving,omitempty"`
49+
LastSeenReceiving string `json:"last_seen_receiving,omitempty"`
50+
FirstSeenSpending string `json:"first_seen_spending,omitempty"`
51+
LastSeenSpending string `json:"last_seen_spending,omitempty"`
4852
TransactionCount int `json:"transaction_count"`
4953
}
5054

55+
// AddressesInfo describes the outer structure of the addresses.
5156
type AddressesInfo struct {
5257
Set Set `json:"set"`
5358
Addresses map[string]Address `json:"addresses"`
5459
Transactions []string `json:"transactions"`
5560
Utxo []Utxo `json:"utxo"`
5661
}
5762

63+
// AddressInfoEth the structure of the set of address and calls for Ethereum.
5864
type AddressInfoEth struct {
5965
Address AddressEth `json:"address"`
6066
Calls []CallsAddress `json:"calls"`
6167
}
6268

69+
// Set the structure of the set for Bitcoin-like address.
6370
type Set struct {
6471
AddressCount int `json:"address_count"`
6572
Balance int64 `json:"balance"`
@@ -75,25 +82,27 @@ type Set struct {
7582
TransactionCount int `json:"transaction_count"`
7683
}
7784

85+
// AddressInfo structure of the set of address, transactions, and utxo.
7886
type AddressInfo struct {
7987
Address Address `json:"address"`
8088
Transactions []string `json:"transactions"`
8189
Utxo []Utxo `json:"utxo"`
8290
}
8391

92+
// AddressEth is the structure of one specific Ethereum address.
8493
type AddressEth struct {
8594
Type string `json:"type"`
86-
ContractCodeHex string `json:"contract_code_hex"`
87-
ContractCreated string `json:"contract_created"`
88-
ContractDestroyed string `json:"contract_destroyed"`
95+
ContractCodeHex string `json:"contract_code_hex,omitempty"`
96+
ContractCreated string `json:"contract_created,omitempty"`
97+
ContractDestroyed string `json:"contract_destroyed,omitempty"`
8998
Balance string `json:"balance"`
90-
BalanceUsd float64 `json:"balance_usd"`
99+
BalanceUsd float32 `json:"balance_usd"`
91100
ReceivedApproximate string `json:"received_approximate"`
92-
ReceivedUsd float64 `json:"received_usd"`
101+
ReceivedUsd float32 `json:"received_usd"`
93102
SpentApproximate string `json:"spent_approximate"`
94-
SpentUsd float64 `json:"spent_usd"`
103+
SpentUsd float32 `json:"spent_usd"`
95104
FeesApproximate string `json:"fees_approximate"`
96-
FeesUsd float64 `json:"fees_usd"`
105+
FeesUsd float32 `json:"fees_usd"`
97106
ReceivingCallCount int `json:"receiving_call_count"`
98107
SpendingCallCount int `json:"spending_call_count"`
99108
CallCount int `json:"call_count"`
@@ -102,118 +111,134 @@ type AddressEth struct {
102111
LastSeenReceiving string `json:"last_seen_receiving"`
103112
FirstSeenSpending string `json:"first_seen_spending"`
104113
LastSeenSpending string `json:"last_seen_spending"`
105-
Nonce int `json:"nonce"`
114+
Nonce int `json:"nonce,omitempty"`
106115
}
107116

117+
// Address the structure of one specific Bitcoin-like address.
108118
type Address struct {
119+
Path string `json:"path,omitempty"`
109120
Type string `json:"type"`
110121
ScriptHex string `json:"script_hex"`
111-
Balance int64 `json:"balance"`
122+
Balance float32 `json:"balance"`
112123
BalanceUsd float32 `json:"balance_usd"`
113-
Received int64 `json:"received"`
124+
Received float32 `json:"received"`
114125
ReceivedUsd float32 `json:"received_usd"`
115-
Spent int `json:"spent"`
126+
Spent float32 `json:"spent"`
116127
SpentUsd float32 `json:"spent_usd"`
117128
OutputCount int `json:"output_count"`
118129
UnspentOutputCount int `json:"unspent_output_count"`
119-
FirstSeenReceiving string `json:"first_seen_receiving"`
120-
LastSeenReceiving string `json:"last_seen_receiving"`
121-
FirstSeenSpending string `json:"first_seen_spending"`
122-
LastSeenSpending string `json:"last_seen_spending"`
123-
ScripthashType string `json:"scripthash_type"`
124-
TransactionCount int `json:"transaction_count"`
130+
FirstSeenReceiving string `json:"first_seen_receiving,omitempty"`
131+
LastSeenReceiving string `json:"last_seen_receiving,omitempty"`
132+
FirstSeenSpending string `json:"first_seen_spending,omitempty"`
133+
LastSeenSpending string `json:"last_seen_spending,omitempty"`
134+
ScripthashType string `json:"scripthash_type,omitempty"`
135+
TransactionCount int `json:"transaction_count,omitempty"`
125136
}
126137

138+
// Utxo the structure of utxo.
127139
type Utxo struct {
128140
BlockID int `json:"block_id"`
129141
TransactionHash string `json:"transaction_hash"`
130142
Index int `json:"index"`
131143
Value int `json:"value"`
132144
}
133145

146+
// CallsAddress is the structures of calls.
134147
type CallsAddress struct {
135148
BlockID int `json:"block_id"`
136-
TransactionHash string `json:"transaction_hash"`
149+
TransactionHash string `json:"transaction_hash,omitempty"`
137150
Index string `json:"index"`
138151
Time string `json:"time"`
139-
Sender string `json:"sender"`
152+
Sender string `json:"sender,omitempty"`
140153
Recipient string `json:"recipient"`
141154
Value float64 `json:"value"`
142155
ValueUsd float64 `json:"value_usd"`
143156
Transferred bool `json:"transferred"`
144157
}
145158

146159
// ContextAddress TODO! FIX "CHECKED" INTO A SLICE
160+
// the structure of context for address(es).
147161
type ContextAddress struct {
148162
Code int `json:"code"`
149163
Source string `json:"source"`
150164
Limit string `json:"limit"`
151165
Offset string `json:"offset"`
152166
Results int `json:"results"`
153-
Checked []string `json:"checked"`
167+
Checked []string `json:"checked,omitempty"`
154168
State int `json:"state"`
155169
MarketPriceUsd float32 `json:"market_price_usd"`
156170
Cache *Cache `json:"cache"`
157-
API *Api `json:"api"`
171+
API *API `json:"api"`
158172
Server string `json:"server"`
159173
Time float32 `json:"time"`
160174
RenderTime float32 `json:"render_time"`
161175
FullTime float32 `json:"full_time"`
162176
RequestCost float32 `json:"request_cost"`
163177
}
164178

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

173-
if e != nil {
174-
fmt.Print(e)
184+
// GetAddressAdv get the address by type of crypto, address hash, and options.
185+
func (c *Client) GetAddressAdv(crypto string, address string, options map[string]string) (resp *DataAddress, e error) {
186+
if e = c.ValidateCrypto(crypto); e != nil {
187+
return
175188
}
176-
return rsp, e
189+
190+
resp = &DataAddress{}
191+
var path = crypto + "/dashboards/address/" + address
192+
return resp, c.LoadResponse(path, resp, options)
177193
}
178194

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

187-
if e != nil {
188-
fmt.Print(e)
200+
// GetAddressesAdv get the addresses by type of crypto, addresses hash and options.
201+
func (c *Client) GetAddressesAdv(crypto string, addresses []string, options map[string]string) (resp *DataAddresses, e error) {
202+
if e = c.ValidateCrypto(crypto); e != nil {
203+
return
189204
}
190-
return rsp, e
205+
206+
resp = &DataAddresses{}
207+
var path = crypto + "/dashboards/addresses/" + strings.Join(addresses, ",")
208+
return resp, c.LoadResponse(path, resp, options)
191209
}
192210

211+
// GetXpub get the xpub/ypub/zpub by type of crypto and the extended key.
212+
// 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)
193213
func (c *Client) GetXpub(crypto string, extendedKey string) (*DataXpub, error) {
194-
// xpub (supported for all blockchains), ypub (supported for Bitcoin, Litecoin, Groestlcoin, and Bitcoin Testnet only)
195-
// zpub (supported for Bitcoin, Litecoin, Groestlcoin, and Bitcoin Testnet only)
196-
rsp := &DataXpub{}
197-
var path = crypto + "/dashboards/xpub/" + extendedKey
198-
e := c.loadResponse(path, rsp)
214+
return c.GetXpubAdv(crypto, extendedKey, nil)
215+
}
199216

200-
if e != nil {
201-
fmt.Print(e)
217+
// GetXpubAdv get the xpub/ypub/zpub by type of crypto, the extended key, and options.
218+
// 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)
219+
func (c *Client) GetXpubAdv(crypto string, extendedKey string, options map[string]string) (resp *DataXpub, e error) {
220+
if e = c.ValidateCryptoBoth(crypto); e != nil {
221+
return
202222
}
203-
return rsp, e
223+
224+
resp = &DataXpub{}
225+
var path = crypto + "/dashboards/xpub/" + extendedKey
226+
return resp, c.LoadResponse(path, resp, options)
204227
}
205228

229+
// GetAddressEth get the address by type of crypto and address hash for Ethereum.
206230
func (c *Client) GetAddressEth(crypto string, address string) (*DataAddressEth, error) {
207-
if !Contains(GetSupportedCryptoEth(), crypto) {
208-
log.Fatalf("error: %v is not supported", crypto)
231+
return c.GetAddressEthAdv(crypto, address, nil)
232+
}
233+
234+
// GetAddressEthAdv get the address by type of crypto, address hash, and options for Ethereum.
235+
// TODO! Validate address
236+
func (c *Client) GetAddressEthAdv(crypto string, address string, options map[string]string) (resp *DataAddressEth, e error) {
237+
if e = c.ValidateCryptoEth(crypto); e != nil {
238+
return
209239
}
210240

211-
rsp := &DataAddressEth{}
241+
resp = &DataAddressEth{}
212242
var path = crypto + "/dashboards/address/" + address
213-
e := c.loadResponse(path, rsp)
214-
215-
if e != nil {
216-
fmt.Print(e)
217-
}
218-
return rsp, e
243+
return resp, c.LoadResponse(path, resp, options)
219244
}

address_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ func TestGetAddress(t *testing.T) {
3131
}
3232
for _, test := range tests {
3333
t.Run(test.currency, func(t *testing.T) {
34-
cl, _ := New(clientID)
34+
cl := New()
35+
cl.APIKey = clientID
3536
_, e := cl.GetAddress(test.currency, test.address)
3637
if e != nil {
3738
t.Fatal(e)
@@ -50,7 +51,8 @@ func TestGetAddresses(t *testing.T) {
5051
}
5152
for _, test := range tests {
5253
t.Run(test.currency, func(t *testing.T) {
53-
cl, _ := New(clientID)
54+
cl := New()
55+
cl.APIKey = clientID
5456
_, e := cl.GetAddresses(test.currency, test.address)
5557
if e != nil {
5658
t.Fatal(e)
@@ -71,7 +73,8 @@ func TestGetAddressEth(t *testing.T) {
7173
}
7274
for _, test := range tests {
7375
t.Run(test.currency, func(t *testing.T) {
74-
cl, _ := New(clientID)
76+
cl := New()
77+
cl.APIKey = clientID
7578
_, e := cl.GetAddressEth(test.currency, test.address)
7679
if e != nil {
7780
t.Fatal(e)
@@ -81,7 +84,8 @@ func TestGetAddressEth(t *testing.T) {
8184
}
8285

8386
func BenchmarkGetAddressUnmarshal(b *testing.B) {
84-
cl, _ := New(clientID)
87+
cl := New()
88+
cl.APIKey = clientID
8589
response, e := cl.GetAddress("bitcoin", "bc1qgdjqv0av3q56jvd82tkdjpy7gdp9ut8tlqmgrpmv24sq90ecnvqqjwvw97")
8690
if e != nil {
8791
b.Fatal(e)

0 commit comments

Comments
 (0)