diff --git a/cmd/cartesi-rollups-cli/root/read/inputs/inputs.go b/cmd/cartesi-rollups-cli/root/read/inputs/inputs.go new file mode 100644 index 000000000..d2cbbf7d0 --- /dev/null +++ b/cmd/cartesi-rollups-cli/root/read/inputs/inputs.go @@ -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)) +} diff --git a/pkg/readerclient/generate/inputs.graphql b/pkg/readerclient/generate/inputs.graphql new file mode 100644 index 000000000..5ce2ca86d --- /dev/null +++ b/pkg/readerclient/generate/inputs.graphql @@ -0,0 +1,14 @@ +query getInputs($first: Int!) { + inputs(first: $first) { + edges { + node { + index + status + msgSender + timestamp + blockNumber + payload + } + } + } +} \ No newline at end of file diff --git a/pkg/readerclient/input.go b/pkg/readerclient/input.go index e0c7d51ff..0c98605ae 100644 --- a/pkg/readerclient/input.go +++ b/pkg/readerclient/input.go @@ -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 +}