github.com/MetalBlockchain/metalgo@v1.11.9/vms/example/xsvm/cmd/issue/transfer/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 transfer 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: "transfer", 21 Short: "Issues a transfer transaction", 22 RunE: transferFunc, 23 } 24 flags := c.Flags() 25 AddFlags(flags) 26 return c 27 } 28 29 func transferFunc(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 := Transfer(c.Context(), config) 37 if err != nil { 38 return err 39 } 40 log.Print(txStatus) 41 42 return nil 43 } 44 45 func Transfer(ctx context.Context, config *Config) (*status.TxIssuance, error) { 46 client := api.NewClient(config.URI, config.ChainID.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.Transfer{ 55 ChainID: config.ChainID, 56 Nonce: nonce, 57 MaxFee: config.MaxFee, 58 AssetID: config.AssetID, 59 Amount: config.Amount, 60 To: config.To, 61 } 62 stx, err := tx.Sign(utx, config.PrivateKey) 63 if err != nil { 64 return nil, err 65 } 66 67 issueTxStartTime := time.Now() 68 txID, err := client.IssueTx(ctx, stx) 69 if err != nil { 70 return nil, err 71 } 72 73 if err := api.AwaitTxAccepted(ctx, client, address, nonce, api.DefaultPollingInterval); err != nil { 74 return nil, err 75 } 76 77 return &status.TxIssuance{ 78 Tx: stx, 79 TxID: txID, 80 Nonce: nonce, 81 StartTime: issueTxStartTime, 82 }, nil 83 }