Skip to content

Commit

Permalink
added get_memos_by_coin_name (#13)
Browse files Browse the repository at this point in the history
* added get_memos_by_coin_name

* added get_fee_estimate
  • Loading branch information
cameroncooper authored Dec 19, 2024
1 parent ae83dc8 commit feae6e1
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
58 changes: 58 additions & 0 deletions internal/cmd/coinset/get_fee_estimate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

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

"github.com/spf13/cobra"
)

func init() {
getFeeEstimateCmd.Flags().StringP("file", "f", "", "Path to file containing the spend bundle JSON")
rootCmd.AddCommand(getFeeEstimateCmd)
}

var getFeeEstimateCmd = &cobra.Command{
Use: "get_fee_estimate [spend_bundle_json]",
Short: "Get fee estimate for a spend bundle",
Long: `Get fee estimate for a spend bundle. The spend bundle can be provided directly as an argument or via a file using the -f flag.`,
}

func init() {
var spendBundleJson string
var parsedJson map[string]interface{}

getFeeEstimateCmd.Args = func(cmd *cobra.Command, args []string) error {
fileFlag, _ := cmd.Flags().GetString("file")

if (len(args) == 0 && fileFlag == "") || (len(args) > 0 && fileFlag != "") {
return fmt.Errorf("must provide either spend bundle JSON as argument or file path with -f flag, but not both")
}

if len(args) > 0 {
if len(args) > 1 {
return fmt.Errorf("too many arguments provided. Did you forget to quote your JSON? Expected: get_fee_estimate '<json>' or get_fee_estimate -f <filename>")
}
spendBundleJson = args[0]
}

if fileFlag != "" {
data, err := os.ReadFile(fileFlag)
if err != nil {
return fmt.Errorf("unable to read file %s: %v", fileFlag, err)
}
spendBundleJson = string(data)
}

if err := json.Unmarshal([]byte(spendBundleJson), &parsedJson); err != nil {
return fmt.Errorf("invalid JSON: %v", err)
}

return nil
}

getFeeEstimateCmd.Run = func(cmd *cobra.Command, args []string) {
makeRequest("get_fee_estimate", parsedJson)
}
}
31 changes: 31 additions & 0 deletions internal/cmd/coinset/get_memos_by_coin_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

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

var getMemosByCoinNameCmd = &cobra.Command{
Use: "get_memos_by_coin_name <name>",
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 memos for a coin",
Long: "Retrieves memos for a coin",
Run: func(cmd *cobra.Command, args []string) {
jsonData := map[string]interface{}{}
jsonData["name"] = formatHex(args[0])
makeRequest("get_memos_by_coin_name", jsonData)
},
}

0 comments on commit feae6e1

Please sign in to comment.