github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/abis/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 abisPkg
     6  
     7  import (
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
    11  )
    12  
    13  func (opts *AbisOptions) validateAbis() error {
    14  	chain := opts.Globals.Chain
    15  	proxy := base.HexToAddress(opts.ProxyFor)
    16  
    17  	opts.testLog()
    18  
    19  	if opts.BadFlag != nil {
    20  		return opts.BadFlag
    21  	}
    22  
    23  	if !config.IsChainConfigured(chain) {
    24  		return validate.Usage("chain {0} is not properly configured.", chain)
    25  	}
    26  
    27  	if opts.Globals.Decache {
    28  		if len(opts.Encode) > 0 {
    29  			return validate.Usage("Please choose only one of {0}.", "--decache or --encode")
    30  		}
    31  		if len(opts.Find) > 0 {
    32  			return validate.Usage("Please choose only one of {0}.", "--decache or --find")
    33  		}
    34  		if opts.Known {
    35  			return validate.Usage("Please choose only one of {0}.", "--decache or --known")
    36  		}
    37  		if !proxy.IsZero() {
    38  			return validate.Usage("Please choose only one of {0}.", "--decache or --proxy_for")
    39  		}
    40  	}
    41  
    42  	if len(opts.Globals.File) == 0 &&
    43  		len(opts.Encode) == 0 &&
    44  		len(opts.Find) == 0 &&
    45  		!opts.Count &&
    46  		!opts.List &&
    47  		!opts.Known &&
    48  		!opts.Globals.Decache {
    49  		// If we're not find and not known we better have at least one address
    50  		err := validate.ValidateAtLeastOneAddr(opts.Addrs)
    51  		if err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	other := len(opts.Encode) != 0 || len(opts.Find) != 0 || opts.Globals.Decache
    57  	if other && (opts.Count || opts.List) {
    58  		return validate.Usage("The {0} options must be used alone.", "--count and --list")
    59  	}
    60  
    61  	if len(opts.Find) > 0 && len(opts.Encode) > 0 {
    62  		return validate.Usage("Please choose only one of {0}.", "--find or --encode")
    63  	}
    64  
    65  	if len(opts.Addrs) != 1 && !proxy.IsZero() {
    66  		return validate.Usage("The {0} option requires exactly one address.", "--proxy_for")
    67  	}
    68  
    69  	for _, term := range opts.Find {
    70  		ok1, err1 := validate.IsValidFourByteE(term)
    71  		if !ok1 && len(term) < 10 {
    72  			return err1
    73  		}
    74  		ok2, err2 := validate.IsValidTopicE(term)
    75  		if !ok2 && len(term) > 66 {
    76  			return err2
    77  		}
    78  		if !ok1 && !ok2 {
    79  			if len(term) > 43 {
    80  				// more than halfway reports topic
    81  				return err2
    82  			}
    83  			return err1
    84  		}
    85  	}
    86  
    87  	return opts.Globals.Validate()
    88  }