github.com/ava-labs/avalanchego@v1.11.11/wallet/subnet/primary/examples/remove-subnet-validator/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/ava-labs/avalanchego/genesis"
    12  	"github.com/ava-labs/avalanchego/ids"
    13  	"github.com/ava-labs/avalanchego/vms/secp256k1fx"
    14  	"github.com/ava-labs/avalanchego/wallet/subnet/primary"
    15  )
    16  
    17  func main() {
    18  	key := genesis.EWOQKey
    19  	uri := primary.LocalAPIURI
    20  	kc := secp256k1fx.NewKeychain(key)
    21  	subnetIDStr := "29uVeLPJB1eQJkzRemU8g8wZDw5uJRqpab5U2mX9euieVwiEbL"
    22  	nodeIDStr := "NodeID-7Xhw2mDxuDS44j42TCB6U5579esbSt3Lg"
    23  
    24  	subnetID, err := ids.FromString(subnetIDStr)
    25  	if err != nil {
    26  		log.Fatalf("failed to parse subnet ID: %s\n", err)
    27  	}
    28  
    29  	nodeID, err := ids.NodeIDFromString(nodeIDStr)
    30  	if err != nil {
    31  		log.Fatalf("failed to parse node ID: %s\n", err)
    32  	}
    33  
    34  	ctx := context.Background()
    35  
    36  	// MakeWallet fetches the available UTXOs owned by [kc] on the network that
    37  	// [uri] is hosting and registers [subnetID].
    38  	walletSyncStartTime := time.Now()
    39  	wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{
    40  		URI:          uri,
    41  		AVAXKeychain: kc,
    42  		EthKeychain:  kc,
    43  		SubnetIDs:    []ids.ID{subnetID},
    44  	})
    45  	if err != nil {
    46  		log.Fatalf("failed to initialize wallet: %s\n", err)
    47  	}
    48  	log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
    49  
    50  	// Get the P-chain wallet
    51  	pWallet := wallet.P()
    52  
    53  	removeValidatorStartTime := time.Now()
    54  	removeValidatorTx, err := pWallet.IssueRemoveSubnetValidatorTx(
    55  		nodeID,
    56  		subnetID,
    57  	)
    58  	if err != nil {
    59  		log.Fatalf("failed to issue remove subnet validator transaction: %s\n", err)
    60  	}
    61  	log.Printf("removed subnet validator %s from %s with %s in %s\n", nodeID, subnetID, removeValidatorTx.ID(), time.Since(removeValidatorStartTime))
    62  }