-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ac07ed
commit ea828f4
Showing
14 changed files
with
350 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
Oops, something went wrong.