Skip to content

Commit

Permalink
Broke each section into their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jan 17, 2020
1 parent 74ec245 commit cd9a784
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 638 deletions.
75 changes: 75 additions & 0 deletions addresses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package whatsonchain

import (
"encoding/json"
"fmt"
"net/http"
)

// AddressInfo this endpoint retrieves various address info.
//
// For more information: https://developers.whatsonchain.com/#address
func (c *Client) AddressInfo(address string) (addressInfo *AddressInfo, err error) {

var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/address/<address>/info
url := fmt.Sprintf("%s%s/address/%s/info", apiEndpoint, c.Parameters.Network, address)
if resp, err = c.Request(url, http.MethodGet, nil); err != nil {
return
}

addressInfo = new(AddressInfo)
err = json.Unmarshal([]byte(resp), addressInfo)
return
}

// AddressBalance this endpoint retrieves confirmed and unconfirmed address balance.
//
// For more information: https://developers.whatsonchain.com/#get-balance
func (c *Client) AddressBalance(address string) (balance *AddressBalance, err error) {

var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/address/<address>/balance
url := fmt.Sprintf("%s%s/address/%s/balance", apiEndpoint, c.Parameters.Network, address)
if resp, err = c.Request(url, http.MethodGet, nil); err != nil {
return
}

balance = new(AddressBalance)
err = json.Unmarshal([]byte(resp), balance)
return
}

// AddressHistory this endpoint retrieves confirmed and unconfirmed address transactions.
//
// For more information: https://developers.whatsonchain.com/#get-history
func (c *Client) AddressHistory(address string) (history AddressHistory, err error) {

var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/address/<address>/history
url := fmt.Sprintf("%s%s/address/%s/history", apiEndpoint, c.Parameters.Network, address)
if resp, err = c.Request(url, http.MethodGet, nil); err != nil {
return
}

history = *new(AddressHistory)
err = json.Unmarshal([]byte(resp), &history)
return
}

// AddressUnspentTransactions this endpoint retrieves ordered list of UTXOs.
//
// For more information: https://developers.whatsonchain.com/#get-unspent-transactions
func (c *Client) AddressUnspentTransactions(address string) (history AddressHistory, err error) {

var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/address/<address>/unspent
url := fmt.Sprintf("%s%s/address/%s/unspent", apiEndpoint, c.Parameters.Network, address)
if resp, err = c.Request(url, http.MethodGet, nil); err != nil {
return
}

history = *new(AddressHistory)
err = json.Unmarshal([]byte(resp), &history)
return
}
114 changes: 114 additions & 0 deletions addresses_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package whatsonchain

import "testing"

// TestClient_AddressInfo tests the AddressInfo()
func TestClient_AddressInfo(t *testing.T) {
// Skip this test in short mode (not needed)
if testing.Short() {
t.Skip("skipping testing in short mode")
}

// Create a new client object to handle your queries (supply an API Key)
client, err := NewClient(NetworkMain, nil)
if err != nil {
t.Fatal(err)
}

var resp *AddressInfo
address := "16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA"
if resp, err = client.AddressInfo(address); err != nil {
t.Fatal("error occurred: " + err.Error())
}

if resp.Address != address {
t.Fatal("failed to get the address", address, resp.Address)
}

}

// TestClient_AddressBalance tests the AddressBalance()
func TestClient_AddressBalance(t *testing.T) {
// Skip this test in short mode (not needed)
if testing.Short() {
t.Skip("skipping testing in short mode")
}

// Create a new client object to handle your queries (supply an API Key)
client, err := NewClient(NetworkMain, nil)
if err != nil {
t.Fatal(err)
}

//var resp *AddressBalance
address := "16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA"
if _, err = client.AddressBalance(address); err != nil {
t.Fatal("error occurred: " + err.Error())
}

// todo: not so easy to test for unless you leave a wallet with unspent transactions just for this test ;-)
/*if resp.Unconfirmed != 0 {
t.Fatal("failed to get the unconfirmed value", resp.Unconfirmed)
}
if resp.Confirmed != 0 {
t.Fatal("failed to get the confirmed value", resp.Unconfirmed)
}*/

}

// TestClient_AddressHistory tests the AddressHistory()
func TestClient_AddressHistory(t *testing.T) {
// Skip this test in short mode (not needed)
if testing.Short() {
t.Skip("skipping testing in short mode")
}

// Create a new client object to handle your queries (supply an API Key)
client, err := NewClient(NetworkMain, nil)
if err != nil {
t.Fatal(err)
}

var resp AddressHistory
address := "16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA"
if resp, err = client.AddressHistory(address); err != nil {
t.Fatal("error occurred: " + err.Error())
}

if len(resp) == 0 {
t.Fatal("failed to get history values", resp)
}

// todo: this might change! not the best test :P
if resp[0].TxHash != "6b22c47e7956e5404e05c3dc87dc9f46e929acfd46c8dd7813a34e1218d2f9d1" {
t.Fatal("failed to get correct hash", resp, resp[0].TxHash)
}

}

// TestClient_AddressUnspentTransactions tests the AddressUnspentTransactions()
func TestClient_AddressUnspentTransactions(t *testing.T) {
// Skip this test in short mode (not needed)
if testing.Short() {
t.Skip("skipping testing in short mode")
}

// Create a new client object to handle your queries (supply an API Key)
client, err := NewClient(NetworkMain, nil)
if err != nil {
t.Fatal(err)
}

//var resp AddressHistory
address := "16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA"
if _, err = client.AddressUnspentTransactions(address); err != nil {
t.Fatal("error occurred: " + err.Error())
}

// todo: not so easy to test for unless you leave a wallet with unspent transactions just for this test ;-)
/*if len(resp) == 0 {
t.Fatal("failed to get history values", resp)
}*/

}
59 changes: 59 additions & 0 deletions blocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package whatsonchain

import (
"encoding/json"
"fmt"
"net/http"
)

// GetBlockByHash this endpoint retrieves block details with given hash.
//
// For more information: https://developers.whatsonchain.com/#get-by-hash
func (c *Client) GetBlockByHash(hash string) (blockInfo *BlockInfo, err error) {

var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/block/hash/<hash>
url := fmt.Sprintf("%s%s/block/hash/%s", apiEndpoint, c.Parameters.Network, hash)
if resp, err = c.Request(url, http.MethodGet, nil); err != nil {
return
}

blockInfo = new(BlockInfo)
err = json.Unmarshal([]byte(resp), blockInfo)
return
}

// GetBlockByHeight this endpoint retrieves block details with given block height.
//
// For more information: https://developers.whatsonchain.com/#get-by-height
func (c *Client) GetBlockByHeight(height int64) (blockInfo *BlockInfo, err error) {

var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/block/height/<height>
url := fmt.Sprintf("%s%s/block/height/%d", apiEndpoint, c.Parameters.Network, height)
if resp, err = c.Request(url, http.MethodGet, nil); err != nil {
return
}

blockInfo = new(BlockInfo)
err = json.Unmarshal([]byte(resp), blockInfo)
return
}

// GetBlockPages If the block has more that 1000 transactions the page URIs will
// be provided in the pages element when getting a block by hash or height.
//
// For more information: https://developers.whatsonchain.com/#get-block-pages
func (c *Client) GetBlockPages(hash string, page int) (txList *BlockPagesInfo, err error) {

var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/block/hash/<hash>/page/1
url := fmt.Sprintf("%s%s/block/hash/%s/page/%d", apiEndpoint, c.Parameters.Network, hash, page)
if resp, err = c.Request(url, http.MethodGet, nil); err != nil {
return
}

txList = new(BlockPagesInfo)
err = json.Unmarshal([]byte(resp), txList)
return
}
78 changes: 78 additions & 0 deletions blocks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package whatsonchain

import "testing"

// TestClient_GetBlockByHash tests the GetBlockByHash()
func TestClient_GetBlockByHash(t *testing.T) {
// Skip this test in short mode (not needed)
if testing.Short() {
t.Skip("skipping testing in short mode")
}

// Create a new client object to handle your queries (supply an API Key)
client, err := NewClient(NetworkMain, nil)
if err != nil {
t.Fatal(err)
}

var resp *BlockInfo
hash := "0000000000000000025b8506c83450afe84f0318775a52c7b91ee64aad0d5a23"
if resp, err = client.GetBlockByHash(hash); err != nil {
t.Fatal("error occurred: " + err.Error())
}

if resp.Hash != "0000000000000000025b8506c83450afe84f0318775a52c7b91ee64aad0d5a23" {
t.Fatal("failed to get the block hash", resp.Hash)
}

}

// TestClient_GetBlockByHeight tests the GetBlockByHeight()
func TestClient_GetBlockByHeight(t *testing.T) {
// Skip this test in short mode (not needed)
if testing.Short() {
t.Skip("skipping testing in short mode")
}

// Create a new client object to handle your queries (supply an API Key)
client, err := NewClient(NetworkMain, nil)
if err != nil {
t.Fatal(err)
}

var resp *BlockInfo
height := int64(604648)
if resp, err = client.GetBlockByHeight(height); err != nil {
t.Fatal("error occurred: " + err.Error())
}

if resp.Hash != "0000000000000000025b8506c83450afe84f0318775a52c7b91ee64aad0d5a23" {
t.Fatal("failed to get the block hash", resp.Hash)
}

}

// TestClient_GetBlockPages tests the GetBlockPages()
func TestClient_GetBlockPages(t *testing.T) {
// Skip this test in short mode (not needed)
if testing.Short() {
t.Skip("skipping testing in short mode")
}

// Create a new client object to handle your queries (supply an API Key)
client, err := NewClient(NetworkMain, nil)
if err != nil {
t.Fatal(err)
}

var resp *BlockPagesInfo
hash := "000000000000000000885a4d8e9912f085b42288adc58b3ee5830a7da9f4fef4"
if resp, err = client.GetBlockPages(hash, 1); err != nil {
t.Fatal("error occurred: " + err.Error())
}

if len(*resp) == 0 {
t.Fatal("no transactions found", resp)
}

}
24 changes: 24 additions & 0 deletions chain_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package whatsonchain

import (
"encoding/json"
"fmt"
"net/http"
)

// GetChainInfo this endpoint retrieves various state info of the chain for the selected network.
//
// For more information: https://developers.whatsonchain.com/#chain-info
func (c *Client) GetChainInfo() (chainInfo *ChainInfo, err error) {

var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/chain/info
url := fmt.Sprintf("%s%s/chain/info", apiEndpoint, c.Parameters.Network)
if resp, err = c.Request(url, http.MethodGet, nil); err != nil {
return
}

chainInfo = new(ChainInfo)
err = json.Unmarshal([]byte(resp), chainInfo)
return
}
40 changes: 40 additions & 0 deletions chain_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package whatsonchain

import (
"fmt"
"log"
"testing"
)

// TestClient_GetChainInfo tests the GetChainInfo()
func TestClient_GetChainInfo(t *testing.T) {
// Skip this test in short mode (not needed)
if testing.Short() {
t.Skip("skipping testing in short mode")
}

// Create a new client object to handle your queries (supply an API Key)
client, err := NewClient(NetworkMain, nil)
if err != nil {
t.Fatal(err)
}

var resp *ChainInfo
if resp, err = client.GetChainInfo(); err != nil {
t.Fatal("error occurred: " + err.Error())
}

if len(resp.BestBlockHash) == 0 {
t.Fatal("failed to get best block hash")
}

}

// ExampleClient_GetChainInfo example using GetChainInfo()
func ExampleClient_GetChainInfo() {
client, _ := NewClient(NetworkMain, nil)
resp, _ := client.GetChainInfo()
log.Println(resp.BestBlockHash)
fmt.Println("0000000000000000057d09c9d9928c53aaff1f6b019ead3ceed52aca8abbc1c9")
// Output:0000000000000000057d09c9d9928c53aaff1f6b019ead3ceed52aca8abbc1c9
}
Loading

0 comments on commit cd9a784

Please sign in to comment.