github.com/MetalBlockchain/metalgo@v1.11.9/wallet/subnet/primary/examples/c-chain-export/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/utils/constants"
    14  	"github.com/MetalBlockchain/metalgo/utils/units"
    15  	"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
    16  	"github.com/MetalBlockchain/metalgo/wallet/subnet/primary"
    17  )
    18  
    19  func main() {
    20  	key := genesis.EWOQKey
    21  	uri := primary.LocalAPIURI
    22  	kc := secp256k1fx.NewKeychain(key)
    23  	avaxAddr := key.Address()
    24  
    25  	ctx := context.Background()
    26  
    27  	// MakeWallet fetches the available UTXOs owned by [kc] on the network that
    28  	// [uri] is hosting.
    29  	walletSyncStartTime := time.Now()
    30  	wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{
    31  		URI:          uri,
    32  		AVAXKeychain: kc,
    33  		EthKeychain:  kc,
    34  	})
    35  	if err != nil {
    36  		log.Fatalf("failed to initialize wallet: %s\n", err)
    37  	}
    38  	log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
    39  
    40  	// Get the P-chain wallet
    41  	pWallet := wallet.P()
    42  	cWallet := wallet.C()
    43  
    44  	// Pull out useful constants to use when issuing transactions.
    45  	cChainID := cWallet.Builder().Context().BlockchainID
    46  	owner := secp256k1fx.OutputOwners{
    47  		Threshold: 1,
    48  		Addrs: []ids.ShortID{
    49  			avaxAddr,
    50  		},
    51  	}
    52  
    53  	exportStartTime := time.Now()
    54  	exportTx, err := cWallet.IssueExportTx(
    55  		constants.PlatformChainID,
    56  		[]*secp256k1fx.TransferOutput{{
    57  			Amt:          units.Avax,
    58  			OutputOwners: owner,
    59  		}},
    60  	)
    61  	if err != nil {
    62  		log.Fatalf("failed to issue export transaction: %s\n", err)
    63  	}
    64  	log.Printf("issued export %s in %s\n", exportTx.ID(), time.Since(exportStartTime))
    65  
    66  	importStartTime := time.Now()
    67  	importTx, err := pWallet.IssueImportTx(cChainID, &owner)
    68  	if err != nil {
    69  		log.Fatalf("failed to issue import transaction: %s\n", err)
    70  	}
    71  	log.Printf("issued import %s in %s\n", importTx.ID(), time.Since(importStartTime))
    72  }