github.com/ava-labs/avalanchego@v1.11.11/wallet/subnet/primary/examples/create-chain/main.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package main
     5  
     6  import (
     7  	"context"
     8  	"log"
     9  	"math"
    10  	"time"
    11  
    12  	"github.com/ava-labs/avalanchego/genesis"
    13  	"github.com/ava-labs/avalanchego/ids"
    14  	"github.com/ava-labs/avalanchego/utils/constants"
    15  	"github.com/ava-labs/avalanchego/vms/secp256k1fx"
    16  	"github.com/ava-labs/avalanchego/wallet/subnet/primary"
    17  
    18  	xsgenesis "github.com/ava-labs/avalanchego/vms/example/xsvm/genesis"
    19  )
    20  
    21  func main() {
    22  	key := genesis.EWOQKey
    23  	uri := primary.LocalAPIURI
    24  	kc := secp256k1fx.NewKeychain(key)
    25  	subnetIDStr := "29uVeLPJB1eQJkzRemU8g8wZDw5uJRqpab5U2mX9euieVwiEbL"
    26  	genesis := &xsgenesis.Genesis{
    27  		Timestamp: time.Now().Unix(),
    28  		Allocations: []xsgenesis.Allocation{
    29  			{
    30  				Address: genesis.EWOQKey.Address(),
    31  				Balance: math.MaxUint64,
    32  			},
    33  		},
    34  	}
    35  	vmID := constants.XSVMID
    36  	name := "let there"
    37  
    38  	subnetID, err := ids.FromString(subnetIDStr)
    39  	if err != nil {
    40  		log.Fatalf("failed to parse subnet ID: %s\n", err)
    41  	}
    42  
    43  	genesisBytes, err := xsgenesis.Codec.Marshal(xsgenesis.CodecVersion, genesis)
    44  	if err != nil {
    45  		log.Fatalf("failed to create genesis bytes: %s\n", err)
    46  	}
    47  
    48  	ctx := context.Background()
    49  
    50  	// MakeWallet fetches the available UTXOs owned by [kc] on the network that
    51  	// [uri] is hosting and registers [subnetID].
    52  	walletSyncStartTime := time.Now()
    53  	wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{
    54  		URI:          uri,
    55  		AVAXKeychain: kc,
    56  		EthKeychain:  kc,
    57  		SubnetIDs:    []ids.ID{subnetID},
    58  	})
    59  	if err != nil {
    60  		log.Fatalf("failed to initialize wallet: %s\n", err)
    61  	}
    62  	log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
    63  
    64  	// Get the P-chain wallet
    65  	pWallet := wallet.P()
    66  
    67  	createChainStartTime := time.Now()
    68  	createChainTx, err := pWallet.IssueCreateChainTx(
    69  		subnetID,
    70  		genesisBytes,
    71  		vmID,
    72  		nil,
    73  		name,
    74  	)
    75  	if err != nil {
    76  		log.Fatalf("failed to issue create chain transaction: %s\n", err)
    77  	}
    78  	log.Printf("created new chain %s in %s\n", createChainTx.ID(), time.Since(createChainStartTime))
    79  }