forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis_txs_export.go
92 lines (77 loc) · 2.21 KB
/
genesis_txs_export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"errors"
"fmt"
"os"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
)
var errNoOutputFile = errors.New("no output file path specified")
// newTxsExportCmd creates the genesis txs export subcommand
func newTxsExportCmd(txsCfg *txsCfg, io commands.IO) *commands.Command {
return commands.NewCommand(
commands.Metadata{
Name: "export",
ShortUsage: "txs export [flags] <output-path>",
ShortHelp: "exports the transactions from the genesis.json",
LongHelp: "Exports the transactions from the genesis.json to an output file",
},
commands.NewEmptyConfig(),
func(_ context.Context, args []string) error {
return execTxsExport(txsCfg, io, args)
},
)
}
func execTxsExport(cfg *txsCfg, io commands.IO, args []string) error {
// Load the genesis
genesis, loadErr := types.GenesisDocFromFile(cfg.genesisPath)
if loadErr != nil {
return fmt.Errorf("unable to load genesis, %w", loadErr)
}
// Load the genesis state
if genesis.AppState == nil {
return errAppStateNotSet
}
state := genesis.AppState.(gnoland.GnoGenesisState)
if len(state.Txs) == 0 {
io.Println("No genesis transactions to export")
return nil
}
// Make sure the output file path is specified
if len(args) == 0 {
return errNoOutputFile
}
// Open output file
outputFile, err := os.OpenFile(
args[0],
os.O_RDWR|os.O_CREATE|os.O_APPEND,
0o755,
)
if err != nil {
return fmt.Errorf("unable to create output file, %w", err)
}
// Save the transactions
for _, tx := range state.Txs {
// Marshal tx individual tx into JSON
jsonData, err := amino.MarshalJSON(tx)
if err != nil {
return fmt.Errorf("unable to marshal JSON data, %w", err)
}
// Write the JSON data as a line to the file
if _, err = outputFile.Write(jsonData); err != nil {
return fmt.Errorf("unable to write to output, %w", err)
}
// Write a newline character to separate JSON objects
if _, err = outputFile.WriteString("\n"); err != nil {
return fmt.Errorf("unable to write newline output, %w", err)
}
}
io.Printfln(
"Exported %d transactions",
len(state.Txs),
)
return nil
}