github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/cmd/gwan/transactioncmd.go (about)

     1  // Copyright 2018 Wanchain Foundation Ltd
     2  // Copyright 2016 The go-ethereum Authors
     3  // This file is part of go-ethereum.
     4  //
     5  // go-ethereum is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // go-ethereum is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package main
    19  
    20  import (
    21  	"errors"
    22  	"github.com/wanchain/go-wanchain/cmd/utils"
    23  	"github.com/wanchain/go-wanchain/common"
    24  	"github.com/wanchain/go-wanchain/core"
    25  	"github.com/wanchain/go-wanchain/core/types"
    26  	"gopkg.in/urfave/cli.v1"
    27  	"os"
    28  )
    29  
    30  var (
    31  	transactionCommand = cli.Command{
    32  		Name:      "transaction",
    33  		Usage:     "Manage transactions in chain",
    34  		ArgsUsage: "",
    35  		Category:  "TRANSACTION COMMANDS",
    36  		Description: `
    37      geth --datadir ./data transaction export 0x1111111111111111111111111111111111111111111111111111111111111111 ./tx.json
    38  
    39  will export the transaction using setting as hash from datadir chain, and save as ./tx.json .`,
    40  		Subcommands: []cli.Command{
    41  			{
    42  				Name:     "export",
    43  				Usage:    "export transaction save as json file",
    44  				Action:   utils.MigrateFlags(exportTransaction),
    45  				Category: "TRANSACTION COMMANDS",
    46  				Flags:    []cli.Flag{},
    47  				Description: `
    48  	geth --datadir ./data transaction export 0x1111111111111111111111111111111111111111111111111111111111111111 ./tx.json
    49  
    50  will export the transaction using setting as hash from datadir chain, and save as ./tx.json .`,
    51  			},
    52  		},
    53  	}
    54  )
    55  
    56  func exportTransaction(ctx *cli.Context) error {
    57  	args := ctx.Args()
    58  	if len(args) < 2 {
    59  		return errors.New("args count not enough")
    60  	}
    61  
    62  	hashStr := args[0]
    63  	filePath := args[1]
    64  
    65  	hash := common.HexToHash(hashStr)
    66  	stack := makeFullNode(ctx)
    67  	_, chainDb := utils.MakeChain(ctx, stack)
    68  
    69  	// Try to return an already finalized transaction
    70  	var tx *types.Transaction
    71  	if tx, _, _, _ = core.GetTransaction(chainDb, hash); tx == nil {
    72  		return errors.New("no found tx")
    73  	}
    74  
    75  	out, err := tx.MarshalJSON()
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	fh, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	defer fh.Close()
    85  
    86  	_, err = fh.Write(out)
    87  	return err
    88  }