github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/slurp/validate.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package slurpPkg
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
    13  )
    14  
    15  func (opts *SlurpOptions) validateSlurp() error {
    16  	chain := opts.Globals.Chain
    17  
    18  	opts.testLog()
    19  
    20  	if opts.BadFlag != nil {
    21  		return opts.BadFlag
    22  	}
    23  
    24  	if !config.IsChainConfigured(chain) {
    25  		return validate.Usage("chain {0} is not properly configured.", chain)
    26  	}
    27  
    28  	err := validate.ValidateAtLeastOneAddr(opts.Addrs)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	err = validate.ValidateEnumSlice("--types", opts.Parts, "[ext|int|token|nfts|1155|miner|uncles|withdrawals|some|all]")
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	err = validate.ValidateEnum("--source", opts.Source, "[etherscan|key|covalent|alchemy]")
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	if opts.Source == "key" {
    44  		key := config.GetChain(chain).KeyEndpoint
    45  		if len(key) == 0 {
    46  			return validate.Usage("The {0} option is only available with {1}.", "--source=key", "a valid TrueBlocks Key endpoint")
    47  		}
    48  		opts.Parts = []string{"not-used"} // key returns everything
    49  		if !opts.Appearances && !opts.Count {
    50  			return validate.Usage("The {0} option is only available with {1} or {2}.", "--source=key", "--appearances", "--count")
    51  		}
    52  	}
    53  
    54  	if chain != "mainnet" {
    55  		return validate.Usage("The {0} command is currently available only on the {1} chain.", "slurp", "mainnet")
    56  	}
    57  
    58  	if opts.Sleep < .25 {
    59  		return validate.Usage("The {0} option ({1}) must {2}.", "--sleep", fmt.Sprintf("%f", opts.Sleep), "be at least .25")
    60  	}
    61  
    62  	if opts.PerPage < 10 {
    63  		return validate.Usage("The {0} option ({1}) must {2}.", "--per_page", fmt.Sprintf("%d", opts.PerPage), "be at least 10")
    64  	} else if opts.PerPage > 10000 {
    65  		return validate.Usage("The {0} option ({1}) must {2}.", "--per_page", fmt.Sprintf("%d", opts.PerPage), "be no more than 10,000")
    66  	}
    67  
    68  	err = validate.ValidateIdentifiers(
    69  		chain,
    70  		opts.Blocks,
    71  		validate.ValidBlockIdWithRange,
    72  		1,
    73  		&opts.BlockIds,
    74  	)
    75  	if err != nil {
    76  		if invalidLiteral, ok := err.(*validate.InvalidIdentifierLiteralError); ok {
    77  			return invalidLiteral
    78  		}
    79  		if errors.Is(err, validate.ErrTooManyRanges) {
    80  			return validate.Usage("Specify only a single block range at a time.")
    81  		}
    82  		return err
    83  	}
    84  
    85  	return opts.Globals.Validate()
    86  }