github.com/MetalBlockchain/metalgo@v1.11.9/wallet/subnet/primary/example_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package primary
     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/components/avax"
    16  	"github.com/MetalBlockchain/metalgo/vms/components/verify"
    17  	"github.com/MetalBlockchain/metalgo/vms/platformvm/reward"
    18  	"github.com/MetalBlockchain/metalgo/vms/platformvm/signer"
    19  	"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
    20  	"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
    21  )
    22  
    23  func ExampleWallet() {
    24  	ctx := context.Background()
    25  	kc := secp256k1fx.NewKeychain(genesis.EWOQKey)
    26  
    27  	// MakeWallet fetches the available UTXOs owned by [kc] on the network that
    28  	// [LocalAPIURI] is hosting.
    29  	walletSyncStartTime := time.Now()
    30  	wallet, err := MakeWallet(ctx, &WalletConfig{
    31  		URI:          LocalAPIURI,
    32  		AVAXKeychain: kc,
    33  		EthKeychain:  kc,
    34  	})
    35  	if err != nil {
    36  		log.Fatalf("failed to initialize wallet with: %s\n", err)
    37  		return
    38  	}
    39  	log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
    40  
    41  	// Get the P-chain and the X-chain wallets
    42  	pWallet := wallet.P()
    43  	xWallet := wallet.X()
    44  	xBuilder := xWallet.Builder()
    45  	xContext := xBuilder.Context()
    46  
    47  	// Pull out useful constants to use when issuing transactions.
    48  	xChainID := xContext.BlockchainID
    49  	owner := &secp256k1fx.OutputOwners{
    50  		Threshold: 1,
    51  		Addrs: []ids.ShortID{
    52  			genesis.EWOQKey.PublicKey().Address(),
    53  		},
    54  	}
    55  
    56  	// Create a custom asset to send to the P-chain.
    57  	createAssetStartTime := time.Now()
    58  	createAssetTx, err := xWallet.IssueCreateAssetTx(
    59  		"RnM",
    60  		"RNM",
    61  		9,
    62  		map[uint32][]verify.State{
    63  			0: {
    64  				&secp256k1fx.TransferOutput{
    65  					Amt:          100 * units.MegaAvax,
    66  					OutputOwners: *owner,
    67  				},
    68  			},
    69  		},
    70  	)
    71  	if err != nil {
    72  		log.Fatalf("failed to create new X-chain asset with: %s\n", err)
    73  		return
    74  	}
    75  	createAssetTxID := createAssetTx.ID()
    76  	log.Printf("created X-chain asset %s in %s\n", createAssetTxID, time.Since(createAssetStartTime))
    77  
    78  	// Send 100 MegaAvax to the P-chain.
    79  	exportStartTime := time.Now()
    80  	exportTx, err := xWallet.IssueExportTx(
    81  		constants.PlatformChainID,
    82  		[]*avax.TransferableOutput{
    83  			{
    84  				Asset: avax.Asset{
    85  					ID: createAssetTxID,
    86  				},
    87  				Out: &secp256k1fx.TransferOutput{
    88  					Amt:          100 * units.MegaAvax,
    89  					OutputOwners: *owner,
    90  				},
    91  			},
    92  		},
    93  	)
    94  	if err != nil {
    95  		log.Fatalf("failed to issue X->P export transaction with: %s\n", err)
    96  		return
    97  	}
    98  	exportTxID := exportTx.ID()
    99  	log.Printf("issued X->P export %s in %s\n", exportTxID, time.Since(exportStartTime))
   100  
   101  	// Import the 100 MegaAvax from the X-chain into the P-chain.
   102  	importStartTime := time.Now()
   103  	importTx, err := pWallet.IssueImportTx(xChainID, owner)
   104  	if err != nil {
   105  		log.Fatalf("failed to issue X->P import transaction with: %s\n", err)
   106  		return
   107  	}
   108  	importTxID := importTx.ID()
   109  	log.Printf("issued X->P import %s in %s\n", importTxID, time.Since(importStartTime))
   110  
   111  	createSubnetStartTime := time.Now()
   112  	createSubnetTx, err := pWallet.IssueCreateSubnetTx(owner)
   113  	if err != nil {
   114  		log.Fatalf("failed to issue create subnet transaction with: %s\n", err)
   115  		return
   116  	}
   117  	createSubnetTxID := createSubnetTx.ID()
   118  	log.Printf("issued create subnet transaction %s in %s\n", createSubnetTxID, time.Since(createSubnetStartTime))
   119  
   120  	transformSubnetStartTime := time.Now()
   121  	transformSubnetTx, err := pWallet.IssueTransformSubnetTx(
   122  		createSubnetTxID,
   123  		createAssetTxID,
   124  		50*units.MegaAvax,
   125  		100*units.MegaAvax,
   126  		reward.PercentDenominator,
   127  		reward.PercentDenominator,
   128  		1,
   129  		100*units.MegaAvax,
   130  		time.Second,
   131  		365*24*time.Hour,
   132  		0,
   133  		1,
   134  		5,
   135  		.80*reward.PercentDenominator,
   136  	)
   137  	if err != nil {
   138  		log.Fatalf("failed to issue transform subnet transaction with: %s\n", err)
   139  		return
   140  	}
   141  	transformSubnetTxID := transformSubnetTx.ID()
   142  	log.Printf("issued transform subnet transaction %s in %s\n", transformSubnetTxID, time.Since(transformSubnetStartTime))
   143  
   144  	addPermissionlessValidatorStartTime := time.Now()
   145  	startTime := time.Now().Add(time.Minute)
   146  	addSubnetValidatorTx, err := pWallet.IssueAddPermissionlessValidatorTx(
   147  		&txs.SubnetValidator{
   148  			Validator: txs.Validator{
   149  				NodeID: genesis.LocalConfig.InitialStakers[0].NodeID,
   150  				Start:  uint64(startTime.Unix()),
   151  				End:    uint64(startTime.Add(5 * time.Second).Unix()),
   152  				Wght:   25 * units.MegaAvax,
   153  			},
   154  			Subnet: createSubnetTxID,
   155  		},
   156  		&signer.Empty{},
   157  		createAssetTx.ID(),
   158  		&secp256k1fx.OutputOwners{},
   159  		&secp256k1fx.OutputOwners{},
   160  		reward.PercentDenominator,
   161  	)
   162  	if err != nil {
   163  		log.Fatalf("failed to issue add subnet validator with: %s\n", err)
   164  		return
   165  	}
   166  	addSubnetValidatorTxID := addSubnetValidatorTx.ID()
   167  	log.Printf("issued add subnet validator transaction %s in %s\n", addSubnetValidatorTxID, time.Since(addPermissionlessValidatorStartTime))
   168  
   169  	addPermissionlessDelegatorStartTime := time.Now()
   170  	addSubnetDelegatorTx, err := pWallet.IssueAddPermissionlessDelegatorTx(
   171  		&txs.SubnetValidator{
   172  			Validator: txs.Validator{
   173  				NodeID: genesis.LocalConfig.InitialStakers[0].NodeID,
   174  				Start:  uint64(startTime.Unix()),
   175  				End:    uint64(startTime.Add(5 * time.Second).Unix()),
   176  				Wght:   25 * units.MegaAvax,
   177  			},
   178  			Subnet: createSubnetTxID,
   179  		},
   180  		createAssetTxID,
   181  		&secp256k1fx.OutputOwners{},
   182  	)
   183  	if err != nil {
   184  		log.Fatalf("failed to issue add subnet delegator with: %s\n", err)
   185  		return
   186  	}
   187  	addSubnetDelegatorTxID := addSubnetDelegatorTx.ID()
   188  	log.Printf("issued add subnet validator delegator %s in %s\n", addSubnetDelegatorTxID, time.Since(addPermissionlessDelegatorStartTime))
   189  }