github.com/MetalBlockchain/metalgo@v1.11.9/vms/example/xsvm/cmd/issue/export/cmd.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package export
     5  
     6  import (
     7  	"context"
     8  	"log"
     9  	"time"
    10  
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/MetalBlockchain/metalgo/vms/example/xsvm/api"
    14  	"github.com/MetalBlockchain/metalgo/vms/example/xsvm/cmd/issue/status"
    15  	"github.com/MetalBlockchain/metalgo/vms/example/xsvm/tx"
    16  )
    17  
    18  func Command() *cobra.Command {
    19  	c := &cobra.Command{
    20  		Use:   "export",
    21  		Short: "Issues an export transaction",
    22  		RunE:  exportFunc,
    23  	}
    24  	flags := c.Flags()
    25  	AddFlags(flags)
    26  	return c
    27  }
    28  
    29  func exportFunc(c *cobra.Command, args []string) error {
    30  	flags := c.Flags()
    31  	config, err := ParseFlags(flags, args)
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	txStatus, err := Export(c.Context(), config)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	log.Print(txStatus)
    41  
    42  	return nil
    43  }
    44  
    45  func Export(ctx context.Context, config *Config) (*status.TxIssuance, error) {
    46  	client := api.NewClient(config.URI, config.SourceChainID.String())
    47  
    48  	address := config.PrivateKey.Address()
    49  	nonce, err := client.Nonce(ctx, address)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	utx := &tx.Export{
    55  		ChainID:     config.SourceChainID,
    56  		Nonce:       nonce,
    57  		MaxFee:      config.MaxFee,
    58  		PeerChainID: config.DestinationChainID,
    59  		IsReturn:    config.IsReturn,
    60  		Amount:      config.Amount,
    61  		To:          config.To,
    62  	}
    63  	stx, err := tx.Sign(utx, config.PrivateKey)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	issueTxStartTime := time.Now()
    69  	txID, err := client.IssueTx(ctx, stx)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	if err := api.AwaitTxAccepted(ctx, client, address, nonce, api.DefaultPollingInterval); err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	return &status.TxIssuance{
    79  		Tx:        stx,
    80  		TxID:      txID,
    81  		Nonce:     nonce,
    82  		StartTime: issueTxStartTime,
    83  	}, nil
    84  }