-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.go
171 lines (155 loc) · 4.44 KB
/
address.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package insight
import (
"encoding/json"
"fmt"
)
type address struct {
insight *insight
address string
*balancesQuery
}
// New Address pointer for Bitcoin Address, includes methods for multiple function
func (a *insight) NewAddress(addr string) *address {
return &address{insight: a, address: addr}
}
func (a *address) String() string {
return a.address
}
func (a *address) Balance() float64 {
if a.balancesQuery == nil {
a.balancesQuery = a.balanceQuery()
}
return a.balancesQuery.Balance
}
func (a *address) PendingBalance() float64 {
if a.balancesQuery == nil {
a.balancesQuery = a.balanceQuery()
}
return float64(a.balancesQuery.UnconfirmedBalance) * 0.000000008
}
func (a *address) balanceQuery() *balancesQuery {
url := fmt.Sprintf("%v/addr/%v", a.insight.Endpoint, a.address)
body, err := httpMethod(url, nil)
if err != nil {
return nil
}
var balance *balancesQuery
err = json.Unmarshal(body, &balance)
if err != nil {
panic(err)
}
fmt.Println(balance.Balance)
a.balancesQuery = balance
return balance
}
func (a *address) UTXO() []*utxos {
url := fmt.Sprintf("%v/addr/%v/utxo", a.insight.Endpoint, a.address)
body, err := httpMethod(url, nil)
if err != nil {
return nil
}
var utxos []*utxos
err = json.Unmarshal(body, &utxos)
if err != nil {
panic(err)
}
return utxos
}
func (a *address) Transactions() []*addressTxs {
pages := a.txPages()
var transactions []*addressTxs
for p := 0; p <= pages; p++ {
txs := a.transactions(p)
for _, tx := range txs {
transactions = append(transactions, tx)
}
}
return transactions
}
func (a *address) txPages() int {
url := fmt.Sprintf("%v/txs?address=%v", a.insight.Endpoint, a.address)
body, err := httpMethod(url, nil)
if err != nil {
return 0
}
var txs *addressTransactions
err = json.Unmarshal(body, &txs)
if err != nil {
panic(err)
}
return txs.PagesTotal
}
func (a *address) transactions(page int) []*addressTxs {
url := fmt.Sprintf("%v/txs?address=%v&pageNum=%v", a.insight.Endpoint, a.address, page)
body, err := httpMethod(url, nil)
if err != nil {
return nil
}
var txs *addressTransactions
err = json.Unmarshal(body, &txs)
if err != nil {
return nil
}
return txs.Txs
}
type balancesQuery struct {
AddrStr string `json:"addrStr"`
Balance float64 `json:"balance"`
BalanceSat int `json:"balanceSat"`
TotalReceived float64 `json:"totalReceived"`
TotalReceivedSat int64 `json:"totalReceivedSat"`
TotalSent float64 `json:"totalSent"`
TotalSentSat int64 `json:"totalSentSat"`
UnconfirmedBalance int `json:"unconfirmedBalance"`
UnconfirmedBalanceSat int `json:"unconfirmedBalanceSat"`
UnconfirmedTxApperances int `json:"unconfirmedTxApperances"`
TxApperances int `json:"txApperances"`
Transactions []string `json:"transactions"`
}
type utxos struct {
Address string `json:"address"`
Txid string `json:"txid"`
Vout int `json:"vout"`
ScriptPubKey string `json:"scriptPubKey"`
Amount float64 `json:"amount"`
Satoshis int `json:"satoshis"`
Height int `json:"height"`
Confirmations int `json:"confirmations"`
}
type addressTransactions struct {
PagesTotal int `json:"pagesTotal"`
Txs []*addressTxs `json:"txs"`
}
type addressTxs struct {
Txid string `json:"txid"`
Version int `json:"version"`
Locktime int `json:"locktime"`
Vin []struct {
Coinbase string `json:"coinbase"`
Sequence int64 `json:"sequence"`
N int `json:"n"`
} `json:"vin"`
Vout []struct {
Value string `json:"value"`
N int `json:"n"`
ScriptPubKey struct {
Hex string `json:"hex"`
Asm string `json:"asm"`
Addresses []string `json:"addresses"`
Type string `json:"type"`
} `json:"scriptPubKey"`
SpentTxID interface{} `json:"spentTxId"`
SpentIndex interface{} `json:"spentIndex"`
SpentHeight interface{} `json:"spentHeight"`
} `json:"vout"`
Blockhash string `json:"blockhash"`
Blockheight int `json:"blockheight"`
Confirmations int `json:"confirmations"`
Time int `json:"time"`
Blocktime int `json:"blocktime"`
IsCoinBase bool `json:"isCoinBase,omitempty"`
ValueOut float64 `json:"valueOut"`
Size int `json:"size"`
ValueIn float64 `json:"valueIn,omitempty"`
Fees float64 `json:"fees,omitempty"`
}