github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/assessed_custom_fee.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  type AssessedCustomFee struct {
    31  	Amount                int64
    32  	TokenID               *TokenID
    33  	FeeCollectorAccountId *AccountID // nolint
    34  	PayerAccountIDs       []*AccountID
    35  }
    36  
    37  func _AssessedCustomFeeFromProtobuf(assessedFee *services.AssessedCustomFee) AssessedCustomFee {
    38  	accountID := _AccountIDFromProtobuf(assessedFee.FeeCollectorAccountId)
    39  	tokenID := _TokenIDFromProtobuf(assessedFee.TokenId)
    40  
    41  	payerAccountIds := make([]*AccountID, 0)
    42  
    43  	for _, id := range assessedFee.EffectivePayerAccountId {
    44  		payerAccountIds = append(payerAccountIds, _AccountIDFromProtobuf(id))
    45  	}
    46  
    47  	return AssessedCustomFee{
    48  		Amount:                assessedFee.Amount,
    49  		TokenID:               tokenID,
    50  		FeeCollectorAccountId: accountID,
    51  		PayerAccountIDs:       payerAccountIds,
    52  	}
    53  }
    54  
    55  func (fee *AssessedCustomFee) _ToProtobuf() *services.AssessedCustomFee {
    56  	var tokenID *services.TokenID
    57  	if fee.TokenID != nil {
    58  		tokenID = fee.TokenID._ToProtobuf()
    59  	}
    60  
    61  	var accountID *services.AccountID
    62  	if fee.FeeCollectorAccountId != nil {
    63  		accountID = fee.FeeCollectorAccountId._ToProtobuf()
    64  	}
    65  
    66  	payerAccountIds := make([]*services.AccountID, len(fee.PayerAccountIDs))
    67  
    68  	for _, id := range fee.PayerAccountIDs {
    69  		payerAccountIds = append(payerAccountIds, id._ToProtobuf())
    70  	}
    71  
    72  	return &services.AssessedCustomFee{
    73  		Amount:                  fee.Amount,
    74  		TokenId:                 tokenID,
    75  		FeeCollectorAccountId:   accountID,
    76  		EffectivePayerAccountId: payerAccountIds,
    77  	}
    78  }
    79  
    80  // ToBytes returns the serialized bytes of a AssessedCustomFee
    81  func (fee *AssessedCustomFee) ToBytes() []byte {
    82  	data, err := protobuf.Marshal(fee._ToProtobuf())
    83  	if err != nil {
    84  		return make([]byte, 0)
    85  	}
    86  
    87  	return data
    88  }
    89  
    90  // AssessedCustomFeeFromBytes returns a AssessedCustomFee from bytes
    91  func AssessedCustomFeeFromBytes(data []byte) (AssessedCustomFee, error) {
    92  	if data == nil {
    93  		return AssessedCustomFee{}, errByteArrayNull
    94  	}
    95  	pb := services.AssessedCustomFee{}
    96  	err := protobuf.Unmarshal(data, &pb)
    97  	if err != nil {
    98  		return AssessedCustomFee{}, err
    99  	}
   100  
   101  	return _AssessedCustomFeeFromProtobuf(&pb), nil
   102  }
   103  
   104  // String returns a string representation of a AssessedCustomFee
   105  func (fee AssessedCustomFee) String() string {
   106  	accountIDs := ""
   107  	for _, s := range fee.PayerAccountIDs {
   108  		accountIDs = accountIDs + " " + s.String()
   109  	}
   110  	if fee.TokenID != nil {
   111  		return fmt.Sprintf("feeCollectorAccountID: %s, amount: %d, tokenID: %s, payerAccountIds: %s", fee.FeeCollectorAccountId.String(), fee.Amount, fee.TokenID.String(), accountIDs)
   112  	}
   113  
   114  	return fmt.Sprintf("feeCollectorAccountID: %s, amount: %d, payerAccountIds: %s", fee.FeeCollectorAccountId.String(), fee.Amount, accountIDs)
   115  }