github.com/MetalBlockchain/metalgo@v1.11.9/wallet/subnet/primary/examples/create-locked-stakeable/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/formatting/address"
    14  	"github.com/MetalBlockchain/metalgo/utils/units"
    15  	"github.com/MetalBlockchain/metalgo/vms/components/avax"
    16  	"github.com/MetalBlockchain/metalgo/vms/platformvm/stakeable"
    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  	amount := 500 * units.MilliAvax
    26  	locktime := uint64(time.Date(2030, 1, 1, 0, 0, 0, 0, time.UTC).Unix())
    27  	destAddrStr := "P-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u00z96u"
    28  
    29  	destAddr, err := address.ParseToID(destAddrStr)
    30  	if err != nil {
    31  		log.Fatalf("failed to parse address: %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.
    38  	walletSyncStartTime := time.Now()
    39  	wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{
    40  		URI:          uri,
    41  		AVAXKeychain: kc,
    42  		EthKeychain:  kc,
    43  	})
    44  	if err != nil {
    45  		log.Fatalf("failed to initialize wallet: %s\n", err)
    46  	}
    47  	log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))
    48  
    49  	// Get the P-chain wallet
    50  	pWallet := wallet.P()
    51  	pBuilder := pWallet.Builder()
    52  	pContext := pBuilder.Context()
    53  	avaxAssetID := pContext.AVAXAssetID
    54  
    55  	issueTxStartTime := time.Now()
    56  	tx, err := pWallet.IssueBaseTx([]*avax.TransferableOutput{
    57  		{
    58  			Asset: avax.Asset{
    59  				ID: avaxAssetID,
    60  			},
    61  			Out: &stakeable.LockOut{
    62  				Locktime: locktime,
    63  				TransferableOut: &secp256k1fx.TransferOutput{
    64  					Amt: amount,
    65  					OutputOwners: secp256k1fx.OutputOwners{
    66  						Threshold: 1,
    67  						Addrs: []ids.ShortID{
    68  							destAddr,
    69  						},
    70  					},
    71  				},
    72  			},
    73  		},
    74  	})
    75  	if err != nil {
    76  		log.Fatalf("failed to issue transaction: %s\n", err)
    77  	}
    78  	log.Printf("issued %s in %s\n", tx.ID(), time.Since(issueTxStartTime))
    79  }