github.com/MetalBlockchain/metalgo@v1.11.9/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/MetalBlockchain/metalgo/genesis" 13 "github.com/MetalBlockchain/metalgo/ids" 14 "github.com/MetalBlockchain/metalgo/utils/constants" 15 "github.com/MetalBlockchain/metalgo/utils/set" 16 "github.com/MetalBlockchain/metalgo/vms/secp256k1fx" 17 "github.com/MetalBlockchain/metalgo/wallet/subnet/primary" 18 19 xsgenesis "github.com/MetalBlockchain/metalgo/vms/example/xsvm/genesis" 20 ) 21 22 func main() { 23 key := genesis.EWOQKey 24 uri := primary.LocalAPIURI 25 kc := secp256k1fx.NewKeychain(key) 26 subnetIDStr := "29uVeLPJB1eQJkzRemU8g8wZDw5uJRqpab5U2mX9euieVwiEbL" 27 genesis := &xsgenesis.Genesis{ 28 Timestamp: time.Now().Unix(), 29 Allocations: []xsgenesis.Allocation{ 30 { 31 Address: genesis.EWOQKey.Address(), 32 Balance: math.MaxUint64, 33 }, 34 }, 35 } 36 vmID := constants.XSVMID 37 name := "let there" 38 39 subnetID, err := ids.FromString(subnetIDStr) 40 if err != nil { 41 log.Fatalf("failed to parse subnet ID: %s\n", err) 42 } 43 44 genesisBytes, err := xsgenesis.Codec.Marshal(xsgenesis.CodecVersion, genesis) 45 if err != nil { 46 log.Fatalf("failed to create genesis bytes: %s\n", err) 47 } 48 49 ctx := context.Background() 50 51 // MakeWallet fetches the available UTXOs owned by [kc] on the network that 52 // [uri] is hosting and registers [subnetID]. 53 walletSyncStartTime := time.Now() 54 wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{ 55 URI: uri, 56 AVAXKeychain: kc, 57 EthKeychain: kc, 58 PChainTxsToFetch: set.Of(subnetID), 59 }) 60 if err != nil { 61 log.Fatalf("failed to initialize wallet: %s\n", err) 62 } 63 log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime)) 64 65 // Get the P-chain wallet 66 pWallet := wallet.P() 67 68 createChainStartTime := time.Now() 69 createChainTx, err := pWallet.IssueCreateChainTx( 70 subnetID, 71 genesisBytes, 72 vmID, 73 nil, 74 name, 75 ) 76 if err != nil { 77 log.Fatalf("failed to issue create chain transaction: %s\n", err) 78 } 79 log.Printf("created new chain %s in %s\n", createChainTx.ID(), time.Since(createChainStartTime)) 80 }