github.com/MetalBlockchain/metalgo@v1.11.9/wallet/subnet/primary/examples/create-subnet/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  	"time"
    10  
    11  	"github.com/MetalBlockchain/metalgo/genesis"
    12  	"github.com/MetalBlockchain/metalgo/ids"
    13  	"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
    14  	"github.com/MetalBlockchain/metalgo/wallet/subnet/primary"
    15  )
    16  
    17  func main() {
    18  	key := genesis.EWOQKey
    19  	uri := primary.LocalAPIURI
    20  	kc := secp256k1fx.NewKeychain(key)
    21  	subnetOwner := key.Address()
    22  
    23  	ctx := context.Background()
    24  
    25  	// MakeWallet fetches the available UTXOs owned by [kc] on the network that
    26  	// [uri] is hosting.
    27  	walletSyncStartTime := time.Now()
    28  	wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{
    29  		URI:          uri,
    30  		AVAXKeychain: kc,
    31  		EthKeychain:  kc,
    32  	})
    33  	if err != nil {
    34  		log.Fatalf("failed to initialize wallet: %s\n", err)
    35  	}
    36  	log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
    37  
    38  	// Get the P-chain wallet
    39  	pWallet := wallet.P()
    40  
    41  	// Pull out useful constants to use when issuing transactions.
    42  	owner := &secp256k1fx.OutputOwners{
    43  		Threshold: 1,
    44  		Addrs: []ids.ShortID{
    45  			subnetOwner,
    46  		},
    47  	}
    48  
    49  	createSubnetStartTime := time.Now()
    50  	createSubnetTx, err := pWallet.IssueCreateSubnetTx(owner)
    51  	if err != nil {
    52  		log.Fatalf("failed to issue create subnet transaction: %s\n", err)
    53  	}
    54  	log.Printf("created new subnet %s in %s\n", createSubnetTx.ID(), time.Since(createSubnetStartTime))
    55  }