Compare commits

..

1 Commits

Author SHA1 Message Date
Gustavo Chain
b1715a7401
allow user to specify a custom endpoint 2022-05-05 20:34:00 +02:00
6 changed files with 58 additions and 38 deletions

View File

@ -5,14 +5,11 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/gorilla/websocket"
)
const (
API_URL = "https://mempool.space/api/v1/ws"
)
type MempoolInfo struct {
Size int `json:"size"`
Bytes int `json:"bytes"`
@ -69,12 +66,22 @@ type Response struct {
}
type Client struct {
conn *websocket.Conn
conn *websocket.Conn
endpoint string
}
func New() (*Client, error) {
return NewWithEndpoint("mempool.space")
}
func NewWithEndpoint(endpoint string) (*Client, error) {
if !strings.HasSuffix(endpoint, "/") {
endpoint = endpoint + "/"
}
endpoint = endpoint + "api/v1/ws"
dialer := websocket.Dialer{}
conn, _, err := dialer.Dial("wss://mempool.space/api/v1/ws", nil)
conn, _, err := dialer.Dial("wss://"+endpoint, nil)
if err != nil {
return nil, err
}
@ -83,7 +90,10 @@ func New() (*Client, error) {
`{"action": "init"}`,
))
return &Client{conn: conn}, nil
return &Client{
conn: conn,
endpoint: endpoint,
}, nil
}
func (c *Client) Read() (*Response, error) {
@ -114,8 +124,8 @@ func (f Fees) Len() int { return len(f) }
func (f Fees) Less(i, j int) bool { return f[i].FPV < f[j].FPV }
func (f Fees) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func Get(ctx context.Context, path string, v interface{}) error {
req, err := http.NewRequest("GET", API_URL+path, nil)
func (c *Client) Get(ctx context.Context, path string, v interface{}) error {
req, err := http.NewRequest("GET", "https://"+c.endpoint+path, nil)
if err != nil {
return err
}
@ -135,17 +145,17 @@ func Get(ctx context.Context, path string, v interface{}) error {
return json.NewDecoder(r.Body).Decode(v)
}
func GetMempoolFee(ctx context.Context, n int) (Fees, error) {
func (c *Client) GetMempoolFee(ctx context.Context, n int) (Fees, error) {
var fees Fees
if err := Get(ctx, fmt.Sprintf("transactions/mempool/%d", n), &fees); err != nil {
if err := c.Get(ctx, fmt.Sprintf("/transactions/mempool/%d", n), &fees); err != nil {
return nil, err
}
return fees, nil
}
func GetBlockFee(ctx context.Context, n int) (Fees, error) {
func (c *Client) GetBlockFee(ctx context.Context, n int) (Fees, error) {
var fees Fees
if err := Get(ctx, fmt.Sprintf("transactions/height/%d", n), &fees); err != nil {
if err := c.Get(ctx, fmt.Sprintf("/transactions/height/%d", n), &fees); err != nil {
return nil, err
}
return fees, nil

8
go.mod
View File

@ -3,16 +3,16 @@ module github.com/mempool/mempool-cli
go 1.17
require (
github.com/fatih/color v1.14.1
github.com/fatih/color v1.13.0
github.com/gorilla/websocket v1.5.0
github.com/jroimartin/gocui v0.5.0
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
)
require (
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
)

24
go.sum
View File

@ -1,14 +1,15 @@
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/jroimartin/gocui v0.5.0 h1:DCZc97zY9dMnHXJSJLLmx9VqiEnAj0yh0eTNpuEtG/4=
github.com/jroimartin/gocui v0.5.0/go.mod h1:l7Hz8DoYoL6NoYnlnaX6XCNR62G7J5FfSW5jEogzaxE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
@ -16,6 +17,9 @@ github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 h1:y/woIyUBFbpQGKS0u1aHF/40WUDnek3fPOyD08H5Vng=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@ -1,13 +1,17 @@
package main
import (
"flag"
"log"
"github.com/mempool/mempool-cli/ui"
)
func main() {
gui, err := ui.New()
endpoint := flag.String("endpoint", "mempool.space", "The API endpoint")
flag.Parse()
gui, err := ui.New(*endpoint)
if err != nil {
log.Fatal(err)
}

View File

@ -21,10 +21,11 @@ type FeeDistribution struct {
cancelFn context.CancelFunc
fees client.Fees
title string
client *client.Client
}
func NewFeeDistribution(g *gocui.Gui) *FeeDistribution {
return &FeeDistribution{gui: g}
func NewFeeDistribution(g *gocui.Gui, c *client.Client) *FeeDistribution {
return &FeeDistribution{gui: g, client: c}
}
func (fd *FeeDistribution) newCtx() context.Context {
@ -35,14 +36,14 @@ func (fd *FeeDistribution) newCtx() context.Context {
func (fd *FeeDistribution) FetchProjection(n int) error {
fn := func(ctx context.Context) (client.Fees, error) {
return client.GetMempoolFee(ctx, n)
return fd.client.GetMempoolFee(ctx, n)
}
return fd.fetch(fn)
}
func (fd *FeeDistribution) FetchBlock(n int) error {
fn := func(ctx context.Context) (client.Fees, error) {
return client.GetBlockFee(ctx, n)
return fd.client.GetBlockFee(ctx, n)
}
return fd.fetch(fn)
}

View File

@ -34,14 +34,19 @@ type UI struct {
state state
}
func New() (*UI, error) {
func New(endpoint string) (*UI, error) {
c, err := client.NewWithEndpoint(endpoint)
if err != nil {
log.Fatal(err)
}
gui, err := gocui.NewGui(gocui.Output256)
if err != nil {
return nil, err
}
ui := &UI{gui: gui}
ui.fd = NewFeeDistribution(gui)
ui.fd = NewFeeDistribution(gui, c)
ui.ts = NewTXSearch(gui)
gui.SetManager(ui, ui.fd, ui.ts)
@ -54,10 +59,6 @@ func New() (*UI, error) {
gui.SelFgColor = gocui.ColorWhite
go func() {
c, err := client.New()
if err != nil {
log.Fatal(err)
}
if err := c.Want(); err != nil {
log.Fatal(err)
}