Skip to content

Commit

Permalink
feat: support output to file for claims (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur authored Jul 15, 2024
1 parent f2ec537 commit d3ee649
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
25 changes: 25 additions & 0 deletions pkg/common/fileio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package common

import (
"fmt"
"os"
"path"
)

func WriteToJSON(data []byte, filePath string) error {
// Write JSON data to a file
file, err := os.Create(path.Clean(filePath))
if err != nil {
fmt.Println("Error creating file:", err)
return err
}
defer file.Close()

_, err = file.Write(data)
if err != nil {
fmt.Println("Error writing to file:", err)
return err
}

return nil
}
22 changes: 19 additions & 3 deletions pkg/rewards/claim.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rewards

import (
"encoding/json"
"errors"
"fmt"
"log/slog"
Expand Down Expand Up @@ -214,9 +215,24 @@ func Claim(cCtx *cli.Context, p utils.Prompter) error {
common.PrintTransactionInfo(receipt.TxHash.String(), config.ChainID)
} else {
solidityClaim := claimgen.FormatProofForSolidity(accounts.Root(), claim)
fmt.Println("------- Claim generated -------")
common.PrettyPrintStruct(*solidityClaim)
fmt.Println("-------------------------------")
if !common.IsEmptyString(config.Output) {
jsonData, err := json.MarshalIndent(solidityClaim, "", " ")
if err != nil {
fmt.Println("Error marshaling JSON:", err)
return err
}

err = common.WriteToJSON(jsonData, config.Output)
if err != nil {
return err
}
logger.Infof("Claim written to file: %s", config.Output)
} else {
fmt.Println("------- Claim generated -------")
common.PrettyPrintStruct(*solidityClaim)
fmt.Println("-------------------------------")
fmt.Println("To write to a file, use the --output flag")
}
fmt.Println("To broadcast the claim, use the --broadcast flag")
}

Expand Down

0 comments on commit d3ee649

Please sign in to comment.