github.com/MetalBlockchain/metalgo@v1.11.9/vms/example/xsvm/cmd/chain/genesis/flags.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package genesis
     5  
     6  import (
     7  	"fmt"
     8  	"math"
     9  	"time"
    10  
    11  	"github.com/spf13/pflag"
    12  
    13  	"github.com/MetalBlockchain/metalgo/genesis"
    14  	"github.com/MetalBlockchain/metalgo/ids"
    15  
    16  	xsgenesis "github.com/MetalBlockchain/metalgo/vms/example/xsvm/genesis"
    17  )
    18  
    19  const (
    20  	TimeKey     = "time"
    21  	AddressKey  = "address"
    22  	BalanceKey  = "balance"
    23  	EncodingKey = "encoding"
    24  
    25  	binaryEncoding = "binary"
    26  	hexEncoding    = "hex"
    27  )
    28  
    29  func AddFlags(flags *pflag.FlagSet) {
    30  	flags.Int64(TimeKey, time.Now().Unix(), "Unix timestamp to include in the genesis")
    31  	flags.String(AddressKey, genesis.EWOQKey.Address().String(), "Address to fund in the genesis")
    32  	flags.Uint64(BalanceKey, math.MaxUint64, "Amount to provide the funded address in the genesis")
    33  	flags.String(EncodingKey, hexEncoding, fmt.Sprintf("Encoding to use for the genesis. Available values: %s or %s", hexEncoding, binaryEncoding))
    34  }
    35  
    36  type Config struct {
    37  	Genesis  *xsgenesis.Genesis
    38  	Encoding string
    39  }
    40  
    41  func ParseFlags(flags *pflag.FlagSet, args []string) (*Config, error) {
    42  	if err := flags.Parse(args); err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	timestamp, err := flags.GetInt64(TimeKey)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	addrStr, err := flags.GetString(AddressKey)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	addr, err := ids.ShortFromString(addrStr)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	balance, err := flags.GetUint64(BalanceKey)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	encoding, err := flags.GetString(EncodingKey)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	return &Config{
    72  		Genesis: &xsgenesis.Genesis{
    73  			Timestamp: timestamp,
    74  			Allocations: []xsgenesis.Allocation{
    75  				{
    76  					Address: addr,
    77  					Balance: balance,
    78  				},
    79  			},
    80  		},
    81  		Encoding: encoding,
    82  	}, nil
    83  }