Skip to content

Commit

Permalink
feat(cli): add read inputs command
Browse files Browse the repository at this point in the history
  • Loading branch information
GMKrieger committed Nov 27, 2023
1 parent 15c4d25 commit ef92c9f
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd/cartesi-rollups-cli/root/read/inputs/inputs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package inputs

import (
"encoding/json"

"github.com/Khan/genqlient/graphql"
"github.com/cartesi/rollups-node/internal/logger"
"github.com/cartesi/rollups-node/pkg/readerclient"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "inputs",
Short: "Reads first N inputs from node GraphQL",
Example: examples,
Run: run,
}

const examples = `# Read inputs from GraphQL:
cartesi-rollups-cli read inputs --first 5 --graphqlEndpoint http://0.0.0.0:4000/graphql`

var (
first uint32
graphqlEndpoint string
)

func init() {
Cmd.Flags().Uint32Var(&first, "first", 0,
"index used to sign the transaction")

cobra.CheckErr(Cmd.MarkFlagRequired("first"))

Cmd.Flags().StringVar(&graphqlEndpoint, "graphql_endpoint", "http://0.0.0.0:4000/graphql",
"address used to connect to graphql")
}

func run(cmd *cobra.Command, args []string) {
logger.Init("info", true)

ctx := cmd.Context()
client := graphql.NewClient(graphqlEndpoint, nil)

resp, err := readerclient.GetInputs(ctx, client, int(first))
cobra.CheckErr(err)

val, err := json.MarshalIndent(resp, "", " ")
cobra.CheckErr(err)

logger.Info.Printf(string(val))
}
14 changes: 14 additions & 0 deletions pkg/readerclient/generate/inputs.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
query getInputs($first: Int!) {
inputs(first: $first) {
edges {
node {
index
status
msgSender
timestamp
blockNumber
payload
}
}
}
}
43 changes: 43 additions & 0 deletions pkg/readerclient/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,46 @@ func GetInput(

return &input, err
}

func GetInputs(
ctx context.Context,
client graphql.Client,
first int,
) (*[]Input, error) {

var inputs []Input

resp, err := getInputs(ctx, client, int(first))
if err != nil {
return nil, err
}

for _, edge := range resp.Inputs.Edges {

timestamp, err := strconv.ParseUint(edge.Node.Timestamp, 0, 64)
if err != nil {
return nil, err
}
blocknumber, err := strconv.ParseUint(edge.Node.BlockNumber, 0, 64)
if err != nil {
return nil, err
}
payload, err := hexutil.Decode(edge.Node.Payload)
if err != nil {
return nil, err
}

input := Input{
edge.Node.Index,
edge.Node.Status,
common.HexToAddress(edge.Node.MsgSender),
time.Duration(timestamp),
blocknumber,
payload,
}

inputs = append(inputs, input)
}

return &inputs, err
}

0 comments on commit ef92c9f

Please sign in to comment.