Skip to content

Commit

Permalink
added events command (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
cameroncooper authored Sep 25, 2024
1 parent 1ac07ed commit ea828f4
Show file tree
Hide file tree
Showing 14 changed files with 350 additions and 244 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (

require (
github.com/fatih/color v1.13.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/itchyny/timefmt-go v0.1.4 // indirect
Expand All @@ -19,5 +20,6 @@ require (
github.com/samber/mo v1.13.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
Expand Down Expand Up @@ -39,6 +41,8 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
96 changes: 96 additions & 0 deletions internal/cmd/coinset/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"os/signal"
"time"

"github.com/gorilla/websocket"
"github.com/spf13/cobra"
)

var eventsCmd = &cobra.Command{
Use: "events [type]",
Short: "Connect to WebSocket and display events",
Long: `Connect to the Coinset WebSocket and display events.
Optionally filter by event type. Valid types are:
- peak
- mempool
- offer
If no type is specified, all events will be displayed.`,
ValidArgs: []string{"peak", "mempool", "offer"},
Args: cobra.MaximumNArgs(1),
Run: runEvents,
}

func init() {
rootCmd.AddCommand(eventsCmd)
}

type Event struct {
Type string `json:"type"`
Data json.RawMessage `json:"data"`
}

func runEvents(cmd *cobra.Command, args []string) {
eventType := ""
if len(args) > 0 {
eventType = args[0]
}

c, _, err := websocket.DefaultDialer.Dial("wss://api.coinset.org/ws", nil)
if err != nil {
fmt.Println(err)
return
}
defer c.Close()

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

done := make(chan struct{})

go func() {
defer close(done)
for {
_, message, err := c.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
return
}
fmt.Println(err)
return
}

var event Event
if err := json.Unmarshal(message, &event); err != nil {
fmt.Println(err)
continue
}

if eventType == "" || event.Type == eventType {
printJson([]byte(message))
}
}
}()

for {
select {
case <-done:
return
case <-interrupt:
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
fmt.Println(err)
return
}
select {
case <-done:
case <-time.After(time.Second):
}
return
}
}
}
42 changes: 21 additions & 21 deletions internal/cmd/coinset/get_additions_and_removals.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package cmd

import (
"fmt"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(getAdditionsAndRemovalsCmd)
rootCmd.AddCommand(getAdditionsAndRemovalsCmd)
}

var getAdditionsAndRemovalsCmd = &cobra.Command{
Use: "get_additions_and_removals <header_hash>",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
if isHex(args[0]) == true {
return nil
}
return fmt.Errorf("invalid hex value specified: %s", args[0])
},
Short: "Retrieves the additions and removals for a certain block",
Long: "Retrieves the additions and removals for a certain block. Returns coin records for each addition and removal. Blocks that are not transaction blocks will have empty removal and addition lists. To get the actual puzzles and solutions for spent coins, use the get_puzzle_and_solution api.",
Run: func(cmd *cobra.Command, args []string) {
jsonData := map[string]interface{}{}
jsonData["header_hash"] = formatHex(args[0])
makeRequest("get_additions_and_removals", jsonData)
},
}
Use: "get_additions_and_removals <header_hash>",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
if isHex(args[0]) {
return nil
}
return fmt.Errorf("invalid hex value specified: %s", args[0])
},
Short: "Retrieves the additions and removals for a certain block",
Long: "Retrieves the additions and removals for a certain block. Returns coin records for each addition and removal. Blocks that are not transaction blocks will have empty removal and addition lists. To get the actual puzzles and solutions for spent coins, use the get_puzzle_and_solution api.",
Run: func(cmd *cobra.Command, args []string) {
jsonData := map[string]interface{}{}
jsonData["header_hash"] = formatHex(args[0])
makeRequest("get_additions_and_removals", jsonData)
},
}
42 changes: 21 additions & 21 deletions internal/cmd/coinset/get_block.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package cmd

import (
"fmt"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(getBlockCmd)
rootCmd.AddCommand(getBlockCmd)
}

var getBlockCmd = &cobra.Command{
Use: "get_block <header_hash>",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
if isHex(args[0]) == true {
return nil
}
return fmt.Errorf("invalid hex value specified: %s", args[0])
},
Short: "Retrieves an entire block as a block by header hash",
Long: `Retrieves an entire block as a block by header hash`,
Run: func(cmd *cobra.Command, args []string) {
jsonData := map[string]interface{}{}
jsonData["header_hash"] = formatHex(args[0])
makeRequest("get_block", jsonData)
},
}
Use: "get_block <header_hash>",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
if isHex(args[0]) {
return nil
}
return fmt.Errorf("invalid hex value specified: %s", args[0])
},
Short: "Retrieves an entire block as a block by header hash",
Long: `Retrieves an entire block as a block by header hash`,
Run: func(cmd *cobra.Command, args []string) {
jsonData := map[string]interface{}{}
jsonData["header_hash"] = formatHex(args[0])
makeRequest("get_block", jsonData)
},
}
42 changes: 21 additions & 21 deletions internal/cmd/coinset/get_block_record.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package cmd

import (
"fmt"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(getBlockRecordCmd)
rootCmd.AddCommand(getBlockRecordCmd)
}

var getBlockRecordCmd = &cobra.Command{
Use: "get_block_record <header_hash>",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
if isHex(args[0]) == true {
return nil
}
return fmt.Errorf("invalid hex value specified: %s", args[0])
},
Short: "Retrieves a block record by header hash",
Long: "Retrieves a block record by header hash",
Run: func(cmd *cobra.Command, args []string) {
jsonData := map[string]interface{}{}
jsonData["header_hash"] = formatHex(args[0])
makeRequest("get_block_record", jsonData)
},
}
Use: "get_block_record <header_hash>",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
if isHex(args[0]) {
return nil
}
return fmt.Errorf("invalid hex value specified: %s", args[0])
},
Short: "Retrieves a block record by header hash",
Long: "Retrieves a block record by header hash",
Run: func(cmd *cobra.Command, args []string) {
jsonData := map[string]interface{}{}
jsonData["header_hash"] = formatHex(args[0])
makeRequest("get_block_record", jsonData)
},
}
42 changes: 21 additions & 21 deletions internal/cmd/coinset/get_block_spends.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package cmd

import (
"fmt"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(getBlockSpendsCmd)
rootCmd.AddCommand(getBlockSpendsCmd)
}

var getBlockSpendsCmd = &cobra.Command{
Use: "get_block_spends <header_hash>",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
if isHex(args[0]) == true {
return nil
}
return fmt.Errorf("invalid hex value specified: %s", args[0])
},
Short: "Retrieves every coin that was spent in a block",
Long: `Retrieves every coin that was spent in a block`,
Run: func(cmd *cobra.Command, args []string) {
jsonData := map[string]interface{}{}
jsonData["header_hash"] = formatHex(args[0])
makeRequest("get_block_spends", jsonData)
},
}
Use: "get_block_spends <header_hash>",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
if isHex(args[0]) {
return nil
}
return fmt.Errorf("invalid hex value specified: %s", args[0])
},
Short: "Retrieves every coin that was spent in a block",
Long: `Retrieves every coin that was spent in a block`,
Run: func(cmd *cobra.Command, args []string) {
jsonData := map[string]interface{}{}
jsonData["header_hash"] = formatHex(args[0])
makeRequest("get_block_spends", jsonData)
},
}
42 changes: 21 additions & 21 deletions internal/cmd/coinset/get_block_spends_with_conditions.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package cmd

import (
"fmt"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(getBlockSpendsWithConditionsCmd)
rootCmd.AddCommand(getBlockSpendsWithConditionsCmd)
}

var getBlockSpendsWithConditionsCmd = &cobra.Command{
Use: "get_block_spends_with_conditions <header_hash>",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
if isHex(args[0]) == true {
return nil
}
return fmt.Errorf("invalid hex value specified: %s", args[0])
},
Short: "Retrieves every coin that was spent in a block with the returned conditions",
Long: `Retrieves every coin that was spent in a block with the returned conditions`,
Run: func(cmd *cobra.Command, args []string) {
jsonData := map[string]interface{}{}
jsonData["header_hash"] = formatHex(args[0])
makeRequest("get_block_spends_with_conditions", jsonData)
},
}
Use: "get_block_spends_with_conditions <header_hash>",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
if isHex(args[0]) {
return nil
}
return fmt.Errorf("invalid hex value specified: %s", args[0])
},
Short: "Retrieves every coin that was spent in a block with the returned conditions",
Long: `Retrieves every coin that was spent in a block with the returned conditions`,
Run: func(cmd *cobra.Command, args []string) {
jsonData := map[string]interface{}{}
jsonData["header_hash"] = formatHex(args[0])
makeRequest("get_block_spends_with_conditions", jsonData)
},
}
Loading

0 comments on commit ea828f4

Please sign in to comment.