Skip to content

Commit d3ee649

Browse files
feat: support output to file for claims (#166)
1 parent f2ec537 commit d3ee649

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

pkg/common/fileio.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package common
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
)
8+
9+
func WriteToJSON(data []byte, filePath string) error {
10+
// Write JSON data to a file
11+
file, err := os.Create(path.Clean(filePath))
12+
if err != nil {
13+
fmt.Println("Error creating file:", err)
14+
return err
15+
}
16+
defer file.Close()
17+
18+
_, err = file.Write(data)
19+
if err != nil {
20+
fmt.Println("Error writing to file:", err)
21+
return err
22+
}
23+
24+
return nil
25+
}

pkg/rewards/claim.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rewards
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"log/slog"
@@ -214,9 +215,24 @@ func Claim(cCtx *cli.Context, p utils.Prompter) error {
214215
common.PrintTransactionInfo(receipt.TxHash.String(), config.ChainID)
215216
} else {
216217
solidityClaim := claimgen.FormatProofForSolidity(accounts.Root(), claim)
217-
fmt.Println("------- Claim generated -------")
218-
common.PrettyPrintStruct(*solidityClaim)
219-
fmt.Println("-------------------------------")
218+
if !common.IsEmptyString(config.Output) {
219+
jsonData, err := json.MarshalIndent(solidityClaim, "", " ")
220+
if err != nil {
221+
fmt.Println("Error marshaling JSON:", err)
222+
return err
223+
}
224+
225+
err = common.WriteToJSON(jsonData, config.Output)
226+
if err != nil {
227+
return err
228+
}
229+
logger.Infof("Claim written to file: %s", config.Output)
230+
} else {
231+
fmt.Println("------- Claim generated -------")
232+
common.PrettyPrintStruct(*solidityClaim)
233+
fmt.Println("-------------------------------")
234+
fmt.Println("To write to a file, use the --output flag")
235+
}
220236
fmt.Println("To broadcast the claim, use the --broadcast flag")
221237
}
222238

0 commit comments

Comments
 (0)