github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_transfer.go (about)

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"github.com/hashgraph/hedera-protobufs-go/services"
    27  	protobuf "google.golang.org/protobuf/proto"
    28  )
    29  
    30  // TokenTransfer is a token transfer record.
    31  type TokenTransfer struct {
    32  	AccountID  AccountID
    33  	Amount     int64
    34  	IsApproved bool
    35  }
    36  
    37  type _TokenTransfers struct {
    38  	transfers []TokenTransfer
    39  }
    40  
    41  // NewTokenTransfer creates a TokenTransfer with the given accountID and amount
    42  func NewTokenTransfer(accountID AccountID, amount int64) TokenTransfer {
    43  	return TokenTransfer{
    44  		AccountID: accountID,
    45  		Amount:    amount,
    46  	}
    47  }
    48  
    49  func _TokenTransferFromProtobuf(pb *services.AccountAmount) TokenTransfer {
    50  	if pb == nil {
    51  		return TokenTransfer{}
    52  	}
    53  
    54  	accountID := AccountID{}
    55  	if pb.AccountID != nil {
    56  		accountID = *_AccountIDFromProtobuf(pb.AccountID)
    57  	}
    58  
    59  	return TokenTransfer{
    60  		AccountID:  accountID,
    61  		Amount:     pb.Amount,
    62  		IsApproved: pb.IsApproval,
    63  	}
    64  }
    65  
    66  func (transfer *TokenTransfer) _ToProtobuf() *services.AccountAmount {
    67  	return &services.AccountAmount{
    68  		AccountID:  transfer.AccountID._ToProtobuf(),
    69  		Amount:     transfer.Amount,
    70  		IsApproval: transfer.IsApproved,
    71  	}
    72  }
    73  
    74  // ToBytes returns a protobuf encoded version of the TokenTransfer
    75  func (transfer TokenTransfer) ToBytes() []byte {
    76  	data, err := protobuf.Marshal(transfer._ToProtobuf())
    77  	if err != nil {
    78  		return make([]byte, 0)
    79  	}
    80  
    81  	return data
    82  }
    83  
    84  // TokenTransferFromBytes returns a TokenTransfer struct from a protobuf encoded byte array
    85  func TokenTransferFromBytes(data []byte) (TokenTransfer, error) {
    86  	if data == nil {
    87  		return TokenTransfer{}, errByteArrayNull
    88  	}
    89  	pb := services.AccountAmount{}
    90  	err := protobuf.Unmarshal(data, &pb)
    91  	if err != nil {
    92  		return TokenTransfer{}, err
    93  	}
    94  
    95  	return _TokenTransferFromProtobuf(&pb), nil
    96  }
    97  
    98  func (transfer TokenTransfer) String() string {
    99  	return fmt.Sprintf("accountID: %s, amount: %d", transfer.AccountID.String(), transfer.Amount)
   100  }
   101  
   102  func (transfers _TokenTransfers) Len() int {
   103  	return len(transfers.transfers)
   104  }
   105  func (transfers _TokenTransfers) Swap(i, j int) {
   106  	transfers.transfers[i], transfers.transfers[j] = transfers.transfers[j], transfers.transfers[i]
   107  }
   108  
   109  func (transfers _TokenTransfers) Less(i, j int) bool {
   110  	if transfers.transfers[i].AccountID.Compare(transfers.transfers[j].AccountID) < 0 { //nolint
   111  		return true
   112  	}
   113  
   114  	return false
   115  }