github.com/MetalBlockchain/metalgo@v1.11.9/wallet/subnet/primary/examples/add-primary-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/MetalBlockchain/metalgo/api/info" 12 "github.com/MetalBlockchain/metalgo/genesis" 13 "github.com/MetalBlockchain/metalgo/ids" 14 "github.com/MetalBlockchain/metalgo/utils/units" 15 "github.com/MetalBlockchain/metalgo/vms/platformvm/reward" 16 "github.com/MetalBlockchain/metalgo/vms/platformvm/txs" 17 "github.com/MetalBlockchain/metalgo/vms/secp256k1fx" 18 "github.com/MetalBlockchain/metalgo/wallet/subnet/primary" 19 ) 20 21 func main() { 22 key := genesis.EWOQKey 23 uri := primary.LocalAPIURI 24 kc := secp256k1fx.NewKeychain(key) 25 startTime := time.Now().Add(time.Minute) 26 duration := 3 * 7 * 24 * time.Hour // 3 weeks 27 weight := 2_000 * units.Avax 28 validatorRewardAddr := key.Address() 29 delegatorRewardAddr := key.Address() 30 delegationFee := uint32(reward.PercentDenominator / 2) // 50% 31 32 ctx := context.Background() 33 infoClient := info.NewClient(uri) 34 35 nodeInfoStartTime := time.Now() 36 nodeID, nodePOP, err := infoClient.GetNodeID(ctx) 37 if err != nil { 38 log.Fatalf("failed to fetch node IDs: %s\n", err) 39 } 40 log.Printf("fetched node ID %s in %s\n", nodeID, time.Since(nodeInfoStartTime)) 41 42 // MakeWallet fetches the available UTXOs owned by [kc] on the network that 43 // [uri] is hosting. 44 walletSyncStartTime := time.Now() 45 wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{ 46 URI: uri, 47 AVAXKeychain: kc, 48 EthKeychain: kc, 49 }) 50 if err != nil { 51 log.Fatalf("failed to initialize wallet: %s\n", err) 52 } 53 log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime)) 54 55 // Get the P-chain wallet 56 pWallet := wallet.P() 57 pBuilder := pWallet.Builder() 58 pContext := pBuilder.Context() 59 avaxAssetID := pContext.AVAXAssetID 60 61 addValidatorStartTime := time.Now() 62 addValidatorTx, err := pWallet.IssueAddPermissionlessValidatorTx( 63 &txs.SubnetValidator{Validator: txs.Validator{ 64 NodeID: nodeID, 65 Start: uint64(startTime.Unix()), 66 End: uint64(startTime.Add(duration).Unix()), 67 Wght: weight, 68 }}, 69 nodePOP, 70 avaxAssetID, 71 &secp256k1fx.OutputOwners{ 72 Threshold: 1, 73 Addrs: []ids.ShortID{validatorRewardAddr}, 74 }, 75 &secp256k1fx.OutputOwners{ 76 Threshold: 1, 77 Addrs: []ids.ShortID{delegatorRewardAddr}, 78 }, 79 delegationFee, 80 ) 81 if err != nil { 82 log.Fatalf("failed to issue add permissionless validator transaction: %s\n", err) 83 } 84 log.Printf("added new primary network validator %s with %s in %s\n", nodeID, addValidatorTx.ID(), time.Since(addValidatorStartTime)) 85 }