github.com/MetalBlockchain/metalgo@v1.11.9/wallet/subnet/primary/examples/create-asset/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/units"
    14  	"github.com/MetalBlockchain/metalgo/vms/components/verify"
    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  	subnetOwner := 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 X-chain wallet
    41  	xWallet := wallet.X()
    42  
    43  	// Pull out useful constants to use when issuing transactions.
    44  	owner := &secp256k1fx.OutputOwners{
    45  		Threshold: 1,
    46  		Addrs: []ids.ShortID{
    47  			subnetOwner,
    48  		},
    49  	}
    50  
    51  	createAssetStartTime := time.Now()
    52  	createAssetTx, err := xWallet.IssueCreateAssetTx(
    53  		"HI",
    54  		"HI",
    55  		1,
    56  		map[uint32][]verify.State{
    57  			0: {
    58  				&secp256k1fx.TransferOutput{
    59  					Amt:          units.Schmeckle,
    60  					OutputOwners: *owner,
    61  				},
    62  			},
    63  		},
    64  	)
    65  	if err != nil {
    66  		log.Fatalf("failed to issue create asset transaction: %s\n", err)
    67  	}
    68  	log.Printf("created new asset %s in %s\n", createAssetTx.ID(), time.Since(createAssetStartTime))
    69  }