github.com/MetalBlockchain/metalgo@v1.11.9/api/common_args_responses.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package api
     5  
     6  import (
     7  	"encoding/json"
     8  
     9  	"github.com/MetalBlockchain/metalgo/ids"
    10  	"github.com/MetalBlockchain/metalgo/utils/formatting"
    11  
    12  	avajson "github.com/MetalBlockchain/metalgo/utils/json"
    13  )
    14  
    15  // This file contains structs used in arguments and responses in services
    16  
    17  // EmptyReply indicates that an api doesn't have a response to return.
    18  type EmptyReply struct{}
    19  
    20  // JSONTxID contains the ID of a transaction
    21  type JSONTxID struct {
    22  	TxID ids.ID `json:"txID"`
    23  }
    24  
    25  // UserPass contains a username and a password
    26  type UserPass struct {
    27  	Username string `json:"username"`
    28  	Password string `json:"password"`
    29  }
    30  
    31  // JSONAddress contains an address
    32  type JSONAddress struct {
    33  	Address string `json:"address"`
    34  }
    35  
    36  // JSONAddresses contains a list of address
    37  type JSONAddresses struct {
    38  	Addresses []string `json:"addresses"`
    39  }
    40  
    41  // JSONChangeAddr is the address change is sent to, if any
    42  type JSONChangeAddr struct {
    43  	ChangeAddr string `json:"changeAddr"`
    44  }
    45  
    46  // JSONTxIDChangeAddr is a tx ID and change address
    47  type JSONTxIDChangeAddr struct {
    48  	JSONTxID
    49  	JSONChangeAddr
    50  }
    51  
    52  // JSONFromAddrs is a list of addresses to send funds from
    53  type JSONFromAddrs struct {
    54  	From []string `json:"from"`
    55  }
    56  
    57  // JSONSpendHeader is 3 arguments to a method that spends (including those with tx fees)
    58  // 1) The username/password
    59  // 2) The addresses used in the method
    60  // 3) The address to send change to
    61  type JSONSpendHeader struct {
    62  	UserPass
    63  	JSONFromAddrs
    64  	JSONChangeAddr
    65  }
    66  
    67  // GetBlockArgs is the parameters supplied to the GetBlock API
    68  type GetBlockArgs struct {
    69  	BlockID  ids.ID              `json:"blockID"`
    70  	Encoding formatting.Encoding `json:"encoding"`
    71  }
    72  
    73  // GetBlockByHeightArgs is the parameters supplied to the GetBlockByHeight API
    74  type GetBlockByHeightArgs struct {
    75  	Height   avajson.Uint64      `json:"height"`
    76  	Encoding formatting.Encoding `json:"encoding"`
    77  }
    78  
    79  // GetBlockResponse is the response object for the GetBlock API.
    80  type GetBlockResponse struct {
    81  	Block json.RawMessage `json:"block"`
    82  	// If GetBlockResponse.Encoding is formatting.Hex, GetBlockResponse.Block is
    83  	// the string representation of the block under hex encoding.
    84  	// If GetBlockResponse.Encoding is formatting.JSON, GetBlockResponse.Block
    85  	// is the actual block returned as a JSON.
    86  	Encoding formatting.Encoding `json:"encoding"`
    87  }
    88  
    89  type GetHeightResponse struct {
    90  	Height avajson.Uint64 `json:"height"`
    91  }
    92  
    93  // FormattedBlock defines a JSON formatted struct containing a block in Hex
    94  // format
    95  type FormattedBlock struct {
    96  	Block    string              `json:"block"`
    97  	Encoding formatting.Encoding `json:"encoding"`
    98  }
    99  
   100  type GetTxArgs struct {
   101  	TxID     ids.ID              `json:"txID"`
   102  	Encoding formatting.Encoding `json:"encoding"`
   103  }
   104  
   105  // GetTxReply defines an object containing a single [Tx] object along with Encoding
   106  type GetTxReply struct {
   107  	// If [GetTxArgs.Encoding] is [Hex], [Tx] is the string representation of
   108  	// the tx under hex encoding.
   109  	// If [GetTxArgs.Encoding] is [JSON], [Tx] is the actual tx, which will be
   110  	// returned as JSON to the caller.
   111  	Tx       json.RawMessage     `json:"tx"`
   112  	Encoding formatting.Encoding `json:"encoding"`
   113  }
   114  
   115  // FormattedTx defines a JSON formatted struct containing a Tx as a string
   116  type FormattedTx struct {
   117  	Tx       string              `json:"tx"`
   118  	Encoding formatting.Encoding `json:"encoding"`
   119  }
   120  
   121  // Index is an address and an associated UTXO.
   122  // Marks a starting or stopping point when fetching UTXOs. Used for pagination.
   123  type Index struct {
   124  	Address string `json:"address"` // The address as a string
   125  	UTXO    string `json:"utxo"`    // The UTXO ID as a string
   126  }
   127  
   128  // GetUTXOsArgs are arguments for passing into GetUTXOs.
   129  // Gets the UTXOs that reference at least one address in [Addresses].
   130  // Returns at most [limit] addresses.
   131  // If specified, [SourceChain] is the chain where the atomic UTXOs were exported from. If empty,
   132  // or the Chain ID of this VM is specified, then GetUTXOs fetches the native UTXOs.
   133  // If [limit] == 0 or > [maxUTXOsToFetch], fetches up to [maxUTXOsToFetch].
   134  // [StartIndex] defines where to start fetching UTXOs (for pagination.)
   135  // UTXOs fetched are from addresses equal to or greater than [StartIndex.Address]
   136  // For address [StartIndex.Address], only UTXOs with IDs greater than [StartIndex.UTXO] will be returned.
   137  // If [StartIndex] is omitted, gets all UTXOs.
   138  // If GetUTXOs is called multiple times, with our without [StartIndex], it is not guaranteed
   139  // that returned UTXOs are unique. That is, the same UTXO may appear in the response of multiple calls.
   140  type GetUTXOsArgs struct {
   141  	Addresses   []string            `json:"addresses"`
   142  	SourceChain string              `json:"sourceChain"`
   143  	Limit       avajson.Uint32      `json:"limit"`
   144  	StartIndex  Index               `json:"startIndex"`
   145  	Encoding    formatting.Encoding `json:"encoding"`
   146  }
   147  
   148  // GetUTXOsReply defines the GetUTXOs replies returned from the API
   149  type GetUTXOsReply struct {
   150  	// Number of UTXOs returned
   151  	NumFetched avajson.Uint64 `json:"numFetched"`
   152  	// The UTXOs
   153  	UTXOs []string `json:"utxos"`
   154  	// The last UTXO that was returned, and the address it corresponds to.
   155  	// Used for pagination. To get the rest of the UTXOs, call GetUTXOs
   156  	// again and set [StartIndex] to this value.
   157  	EndIndex Index `json:"endIndex"`
   158  	// Encoding specifies the encoding format the UTXOs are returned in
   159  	Encoding formatting.Encoding `json:"encoding"`
   160  }