github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/fee_schedule.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  	"time"
    26  
    27  	"github.com/hashgraph/hedera-protobufs-go/services"
    28  	protobuf "google.golang.org/protobuf/proto"
    29  )
    30  
    31  type FeeSchedule struct {
    32  	TransactionFeeSchedules []TransactionFeeSchedule
    33  	ExpirationTime          *time.Time
    34  }
    35  
    36  func _FeeScheduleFromProtobuf(feeSchedule *services.FeeSchedule) (FeeSchedule, error) {
    37  	if feeSchedule == nil {
    38  		return FeeSchedule{}, errParameterNull
    39  	}
    40  
    41  	txFeeSchedules := make([]TransactionFeeSchedule, 0)
    42  	for _, txFeeSchedule := range feeSchedule.GetTransactionFeeSchedule() {
    43  		txFeeScheduleFromProto, err := _TransactionFeeScheduleFromProtobuf(txFeeSchedule)
    44  		if err != nil {
    45  			return FeeSchedule{}, err
    46  		}
    47  		txFeeSchedules = append(txFeeSchedules, txFeeScheduleFromProto)
    48  	}
    49  
    50  	var expiry time.Time
    51  	if feeSchedule.ExpiryTime != nil {
    52  		expiry = time.Unix(feeSchedule.ExpiryTime.Seconds, 0)
    53  	}
    54  
    55  	return FeeSchedule{
    56  		TransactionFeeSchedules: txFeeSchedules,
    57  		ExpirationTime:          &expiry,
    58  	}, nil
    59  }
    60  
    61  func (feeSchedule FeeSchedule) _ToProtobuf() *services.FeeSchedule {
    62  	txFeeSchedules := make([]*services.TransactionFeeSchedule, 0)
    63  	for _, txFeeSchedule := range feeSchedule.TransactionFeeSchedules {
    64  		txFeeSchedules = append(txFeeSchedules, txFeeSchedule._ToProtobuf())
    65  	}
    66  
    67  	var expiry services.TimestampSeconds
    68  	if feeSchedule.ExpirationTime != nil {
    69  		expiry = services.TimestampSeconds{Seconds: feeSchedule.ExpirationTime.Unix()}
    70  	}
    71  
    72  	return &services.FeeSchedule{
    73  		TransactionFeeSchedule: txFeeSchedules,
    74  		ExpiryTime:             &expiry,
    75  	}
    76  }
    77  
    78  // ToBytes returns the byte representation of the FeeSchedule
    79  func (feeSchedule FeeSchedule) ToBytes() []byte {
    80  	data, err := protobuf.Marshal(feeSchedule._ToProtobuf())
    81  	if err != nil {
    82  		return make([]byte, 0)
    83  	}
    84  
    85  	return data
    86  }
    87  
    88  // FeeScheduleFromBytes returns a FeeSchedule from a raw protobuf byte array
    89  func FeeScheduleFromBytes(data []byte) (FeeSchedule, error) {
    90  	if data == nil {
    91  		return FeeSchedule{}, errByteArrayNull
    92  	}
    93  	pb := services.FeeSchedule{}
    94  	err := protobuf.Unmarshal(data, &pb)
    95  	if err != nil {
    96  		return FeeSchedule{}, err
    97  	}
    98  
    99  	info, err := _FeeScheduleFromProtobuf(&pb)
   100  	if err != nil {
   101  		return FeeSchedule{}, err
   102  	}
   103  
   104  	return info, nil
   105  }
   106  
   107  // String returns a string representation of the FeeSchedule
   108  func (feeSchedule FeeSchedule) String() string {
   109  	array := "\n"
   110  	for _, i := range feeSchedule.TransactionFeeSchedules {
   111  		array = array + i.String() + "\n"
   112  	}
   113  	return fmt.Sprintf("TransactionFeeSchedules: %s, Expiration: %s", array, feeSchedule.ExpirationTime)
   114  }