github.com/stellar/stellar-etl@v1.0.1-0.20240312145900-4874b6bf2b89/cmd/export_trustlines.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/sirupsen/logrus"
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/stellar/stellar-etl/internal/input"
    10  	"github.com/stellar/stellar-etl/internal/transform"
    11  	"github.com/stellar/stellar-etl/internal/utils"
    12  
    13  	"github.com/stellar/go/xdr"
    14  )
    15  
    16  // trustlinesCmd represents the trustlines command
    17  var trustlinesCmd = &cobra.Command{
    18  	Use:   "export_trustlines",
    19  	Short: "Exports the trustline data over a specified range.",
    20  	Long: `Exports historical trustline data from the genesis ledger to the provided end-ledger to an output file. 
    21  	The command reads from the bucket list, which includes the full history of the Stellar ledger. As a result, it 
    22  	should be used in an initial data dump. In order to get trustline information within a specified ledger range, see 
    23  	the export_ledger_entry_changes command.`,
    24  	Run: func(cmd *cobra.Command, args []string) {
    25  		cmdLogger.SetLevel(logrus.InfoLevel)
    26  		endNum, strictExport, isTest, isFuture, extra := utils.MustCommonFlags(cmd.Flags(), cmdLogger)
    27  		cmdLogger.StrictExport = strictExport
    28  		env := utils.GetEnvironmentDetails(isTest, isFuture)
    29  		path := utils.MustBucketFlags(cmd.Flags(), cmdLogger)
    30  		cloudStorageBucket, cloudCredentials, cloudProvider := utils.MustCloudStorageFlags(cmd.Flags(), cmdLogger)
    31  
    32  		trustlines, err := input.GetEntriesFromGenesis(endNum, xdr.LedgerEntryTypeTrustline, env.ArchiveURLs)
    33  		if err != nil {
    34  			cmdLogger.Fatal("could not read trustlines: ", err)
    35  		}
    36  
    37  		outFile := mustOutFile(path)
    38  		numFailures := 0
    39  		totalNumBytes := 0
    40  		var header xdr.LedgerHeaderHistoryEntry
    41  		for _, trust := range trustlines {
    42  			transformed, err := transform.TransformTrustline(trust, header)
    43  			if err != nil {
    44  				cmdLogger.LogError(fmt.Errorf("could not json transform trustline %+v: %v", trust, err))
    45  				numFailures += 1
    46  				continue
    47  			}
    48  
    49  			numBytes, err := exportEntry(transformed, outFile, extra)
    50  			if err != nil {
    51  				cmdLogger.LogError(fmt.Errorf("could not export trustline %+v: %v", trust, err))
    52  				numFailures += 1
    53  				continue
    54  			}
    55  			totalNumBytes += numBytes
    56  		}
    57  
    58  		outFile.Close()
    59  
    60  		cmdLogger.Info("Number of bytes written: ", totalNumBytes)
    61  
    62  		printTransformStats(len(trustlines), numFailures)
    63  
    64  		maybeUpload(cloudCredentials, cloudStorageBucket, cloudProvider, path)
    65  	},
    66  }
    67  
    68  func init() {
    69  	rootCmd.AddCommand(trustlinesCmd)
    70  	utils.AddCommonFlags(trustlinesCmd.Flags())
    71  	utils.AddBucketFlags("trustlines", trustlinesCmd.Flags())
    72  	utils.AddCloudStorageFlags(trustlinesCmd.Flags())
    73  	trustlinesCmd.MarkFlagRequired("end-ledger")
    74  
    75  	/*
    76  		Current flags:
    77  			end-ledger: the ledger sequence number for the end of the export range (required)
    78  			output-file: filename of the output file
    79  			stdout: if set, output is printed to stdout
    80  
    81  		TODO: implement extra flags if possible
    82  			serialize-method: the method for serialization of the output data (JSON, XDR, etc)
    83  			 end time as a replacement for end sequence numbers
    84  	*/
    85  }