github.com/stellar/stellar-etl@v1.0.1-0.20240312145900-4874b6bf2b89/cmd/export_claimable_balances.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 var claimableBalancesCmd = &cobra.Command{ 17 Use: "export_claimable_balances", 18 Short: "Exports the data on claimable balances made from the genesis ledger to a specified endpoint.", 19 Long: `Exports historical offer data from the genesis ledger to the provided end-ledger to an output file. 20 The command reads from the bucket list, which includes the full history of the Stellar ledger. As a result, it 21 should be used in an initial data dump. In order to get offer information within a specified ledger range, see 22 the export_ledger_entry_changes command.`, 23 Run: func(cmd *cobra.Command, args []string) { 24 cmdLogger.SetLevel(logrus.InfoLevel) 25 endNum, strictExport, isTest, isFuture, extra := utils.MustCommonFlags(cmd.Flags(), cmdLogger) 26 cmdLogger.StrictExport = strictExport 27 env := utils.GetEnvironmentDetails(isTest, isFuture) 28 path := utils.MustBucketFlags(cmd.Flags(), cmdLogger) 29 cloudStorageBucket, cloudCredentials, cloudProvider := utils.MustCloudStorageFlags(cmd.Flags(), cmdLogger) 30 31 balances, err := input.GetEntriesFromGenesis(endNum, xdr.LedgerEntryTypeClaimableBalance, env.ArchiveURLs) 32 if err != nil { 33 cmdLogger.Fatal("could not read balances: ", err) 34 } 35 36 outFile := mustOutFile(path) 37 numFailures := 0 38 totalNumBytes := 0 39 var header xdr.LedgerHeaderHistoryEntry 40 for _, balance := range balances { 41 transformed, err := transform.TransformClaimableBalance(balance, header) 42 if err != nil { 43 cmdLogger.LogError(fmt.Errorf("could not transform balance %+v: %v", balance, err)) 44 numFailures += 1 45 continue 46 } 47 48 numBytes, err := exportEntry(transformed, outFile, extra) 49 if err != nil { 50 cmdLogger.LogError(fmt.Errorf("could not export balance %+v: %v", balance, err)) 51 numFailures += 1 52 continue 53 } 54 totalNumBytes += numBytes 55 } 56 57 outFile.Close() 58 cmdLogger.Info("Number of bytes written: ", totalNumBytes) 59 60 printTransformStats(len(balances), numFailures) 61 62 maybeUpload(cloudCredentials, cloudStorageBucket, cloudProvider, path) 63 }, 64 } 65 66 func init() { 67 rootCmd.AddCommand(claimableBalancesCmd) 68 utils.AddCommonFlags(claimableBalancesCmd.Flags()) 69 utils.AddBucketFlags("claimable_balances", claimableBalancesCmd.Flags()) 70 utils.AddCloudStorageFlags(claimableBalancesCmd.Flags()) 71 claimableBalancesCmd.MarkFlagRequired("end-ledger") 72 73 /* 74 Current flags: 75 end-ledger: the ledger sequence number for the end of the export range (required) 76 output-file: filename of the output file 77 78 TODO: implement extra flags if possible 79 serialize-method: the method for serialization of the output data (JSON, XDR, etc) 80 end time as a replacement for end sequence numbers 81 */ 82 }