Compare commits

..

3 Commits

Author SHA1 Message Date
Gustavo Chain
fdb8d4002f
update broken ws endpoint
Some checks failed
release / goreleaser (push) Has been cancelled
2023-03-27 11:34:27 +02:00
Gustavo Chaín
1e149e7a50
Merge pull request #15 from mempool/dependabot/go_modules/github.com/fatih/color-1.14.1
Bump github.com/fatih/color from 1.13.0 to 1.14.1
2023-01-30 16:37:02 +00:00
dependabot[bot]
c85f24abe7
Bump github.com/fatih/color from 1.13.0 to 1.14.1
Bumps [github.com/fatih/color](https://github.com/fatih/color) from 1.13.0 to 1.14.1.
- [Release notes](https://github.com/fatih/color/releases)
- [Commits](https://github.com/fatih/color/compare/v1.13.0...v1.14.1)

---
updated-dependencies:
- dependency-name: github.com/fatih/color
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-01-24 15:07:00 +00:00
6 changed files with 38 additions and 58 deletions

View File

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

8
go.mod
View File

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

24
go.sum
View File

@ -1,15 +1,14 @@
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= 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/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 h1:DCZc97zY9dMnHXJSJLLmx9VqiEnAj0yh0eTNpuEtG/4=
github.com/jroimartin/gocui v0.5.0/go.mod h1:l7Hz8DoYoL6NoYnlnaX6XCNR62G7J5FfSW5jEogzaxE= github.com/jroimartin/gocui v0.5.0/go.mod h1:l7Hz8DoYoL6NoYnlnaX6XCNR62G7J5FfSW5jEogzaxE=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
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.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 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
@ -17,9 +16,6 @@ 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/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 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/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,17 +1,13 @@
package main package main
import ( import (
"flag"
"log" "log"
"github.com/mempool/mempool-cli/ui" "github.com/mempool/mempool-cli/ui"
) )
func main() { func main() {
endpoint := flag.String("endpoint", "mempool.space", "The API endpoint") gui, err := ui.New()
flag.Parse()
gui, err := ui.New(*endpoint)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

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

View File

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