github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/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  	"github.com/hashgraph/hedera-protobufs-go/services"
    25  	protobuf "google.golang.org/protobuf/proto"
    26  )
    27  
    28  // Base struct for all custom fees
    29  type CustomFee struct {
    30  	FeeCollectorAccountID  *AccountID
    31  	AllCollectorsAreExempt bool
    32  }
    33  
    34  func _CustomFeeFromProtobuf(customFee *services.CustomFee) Fee {
    35  	if customFee == nil {
    36  		return nil
    37  	}
    38  
    39  	var id *AccountID
    40  	if customFee.FeeCollectorAccountId != nil {
    41  		id = _AccountIDFromProtobuf(customFee.FeeCollectorAccountId)
    42  	}
    43  
    44  	fee := CustomFee{
    45  		FeeCollectorAccountID:  id,
    46  		AllCollectorsAreExempt: customFee.AllCollectorsAreExempt,
    47  	}
    48  
    49  	switch t := customFee.Fee.(type) {
    50  	case *services.CustomFee_FixedFee:
    51  		return _CustomFixedFeeFromProtobuf(t.FixedFee, fee)
    52  	case *services.CustomFee_FractionalFee:
    53  		return _CustomFractionalFeeFromProtobuf(t.FractionalFee, fee)
    54  	case *services.CustomFee_RoyaltyFee:
    55  		return _CustomRoyaltyFeeFromProtobuf(t.RoyaltyFee, fee)
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  // SetFeeCollectorAccountID sets the account ID that will receive the custom fee
    62  func (fee *CustomFee) SetFeeCollectorAccountID(id AccountID) *CustomFee {
    63  	fee.FeeCollectorAccountID = &id
    64  	return fee
    65  }
    66  
    67  // GetFeeCollectorAccountID returns the account ID that will receive the custom fee
    68  func (fee *CustomFee) GetFeeCollectorAccountID() AccountID {
    69  	return *fee.FeeCollectorAccountID
    70  }
    71  
    72  // SetAllCollectorsAreExempt sets whether or not all collectors are exempt from the custom fee
    73  func (fee *CustomFee) SetAllCollectorsAreExempt(exempt bool) *CustomFee {
    74  	fee.AllCollectorsAreExempt = exempt
    75  	return fee
    76  }
    77  
    78  // GetAllCollectorsAreExempt returns whether or not all collectors are exempt from the custom fee
    79  func (fee *CustomFee) GetAllCollectorsAreExempt() bool {
    80  	return fee.AllCollectorsAreExempt
    81  }
    82  
    83  // CustomFeeFromBytes returns a CustomFee from a raw protobuf byte array
    84  func CustomFeeFromBytes(data []byte) (Fee, error) {
    85  	if data == nil {
    86  		return nil, errByteArrayNull
    87  	}
    88  	pb := services.CustomFee{}
    89  	err := protobuf.Unmarshal(data, &pb)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	return _CustomFeeFromProtobuf(&pb), nil
    95  }