github.com/MetalBlockchain/metalgo@v1.11.9/vms/secp256k1fx/transfer_output.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package secp256k1fx
     5  
     6  import (
     7  	"encoding/json"
     8  	"errors"
     9  
    10  	"github.com/MetalBlockchain/metalgo/vms/components/verify"
    11  )
    12  
    13  var (
    14  	_ verify.State = (*TransferOutput)(nil)
    15  
    16  	ErrNoValueOutput = errors.New("output has no value")
    17  )
    18  
    19  type TransferOutput struct {
    20  	verify.IsState `json:"-"`
    21  
    22  	Amt uint64 `serialize:"true" json:"amount"`
    23  
    24  	OutputOwners `serialize:"true"`
    25  }
    26  
    27  // MarshalJSON marshals Amt and the embedded OutputOwners struct
    28  // into a JSON readable format
    29  // If OutputOwners cannot be serialized then this will return error
    30  func (out *TransferOutput) MarshalJSON() ([]byte, error) {
    31  	result, err := out.OutputOwners.Fields()
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	result["amount"] = out.Amt
    37  	return json.Marshal(result)
    38  }
    39  
    40  // Amount returns the quantity of the asset this output consumes
    41  func (out *TransferOutput) Amount() uint64 {
    42  	return out.Amt
    43  }
    44  
    45  func (out *TransferOutput) Verify() error {
    46  	switch {
    47  	case out == nil:
    48  		return ErrNilOutput
    49  	case out.Amt == 0:
    50  		return ErrNoValueOutput
    51  	default:
    52  		return out.OutputOwners.Verify()
    53  	}
    54  }
    55  
    56  func (out *TransferOutput) Owners() interface{} {
    57  	return &out.OutputOwners
    58  }