-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathblockstream_test.go
More file actions
89 lines (78 loc) · 2.07 KB
/
blockstream_test.go
File metadata and controls
89 lines (78 loc) · 2.07 KB
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
package blockstream
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/davecgh/go-spew/spew"
"github.com/hemilabs/heminetwork/bitcoin/wallet/gozer"
)
func mockHttpServer() *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.HasPrefix(r.URL.Path, "/address/") && strings.HasSuffix(r.URL.Path, "/utxo"):
utxos := []map[string]interface{}{
{
"txid": chainhash.Hash{},
"vout": 1,
"value": 100000000,
"status": map[string]interface{}{
"confirmed": true,
"block_height": 1,
"block_hash": chainhash.Hash{},
"block_time": 1,
},
},
}
resp, err := json.Marshal(utxos)
if err != nil {
http.NotFound(w, r)
break
}
w.WriteHeader(http.StatusOK)
w.Write(resp)
case r.URL.Path == "/fee-estimates":
response := []byte(`{"1":1.0,"2":1.0,"3":1.0,"4":1.0,"5":1.0,"6":1.0}`)
w.WriteHeader(http.StatusOK)
w.Write(response)
default:
http.NotFound(w, r)
}
}))
return ts
}
func TestBlockstreamGozer(t *testing.T) {
testAddrString := "n2BosBT7DvxWk1tZprk1tR1kyQmXwcv8M8"
testAddr, err := btcutil.DecodeAddress(testAddrString, &chaincfg.TestNet3Params)
if err != nil {
t.Fatalf("Failed to decode address: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// use mock http server rather than blockstream api
ts := mockHttpServer()
defer ts.Close()
// can't use BlockstreamNew() for custom urls
b := &blockstream{}
b.url = ts.URL
feeEstimates, err := b.FeeEstimates(ctx)
if err != nil {
t.Fatal(err)
}
feeEstimate, err := gozer.FeeByConfirmations(6, feeEstimates)
if err != nil {
t.Fatal(err)
}
t.Log(spew.Sdump(feeEstimate))
utxos, err := b.UtxosByAddress(ctx, testAddr, 0, 0)
if err != nil {
t.Fatal(err)
}
t.Logf("balance %v: %v", testAddr, gozer.BalanceFromUtxos(utxos))
}