github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/exchange_rate.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  // ExchangeRate is the exchange rate between HBAR and USD
    31  type ExchangeRate struct {
    32  	Hbars          int32
    33  	cents          int32
    34  	expirationTime *services.TimestampSeconds
    35  }
    36  
    37  func _ExchangeRateFromProtobuf(protoExchange *services.ExchangeRate) ExchangeRate {
    38  	if protoExchange == nil {
    39  		return ExchangeRate{}
    40  	}
    41  	var expirationTime *services.TimestampSeconds
    42  	if protoExchange.ExpirationTime != nil {
    43  		expirationTime = protoExchange.ExpirationTime
    44  	}
    45  
    46  	return ExchangeRate{
    47  		protoExchange.HbarEquiv,
    48  		protoExchange.CentEquiv,
    49  		expirationTime,
    50  	}
    51  }
    52  
    53  func (exchange *ExchangeRate) _ToProtobuf() *services.ExchangeRate {
    54  	return &services.ExchangeRate{
    55  		HbarEquiv:      exchange.Hbars,
    56  		CentEquiv:      exchange.cents,
    57  		ExpirationTime: exchange.expirationTime,
    58  	}
    59  }
    60  
    61  // ToBytes returns the byte representation of the ExchangeRate
    62  func (exchange *ExchangeRate) ToBytes() []byte {
    63  	data, err := protobuf.Marshal(exchange._ToProtobuf())
    64  	if err != nil {
    65  		return make([]byte, 0)
    66  	}
    67  
    68  	return data
    69  }
    70  
    71  // ExchangeRateFromString returns an ExchangeRate from a string representation of the exchange rate
    72  func ExchangeRateFromBytes(data []byte) (ExchangeRate, error) {
    73  	if data == nil {
    74  		return ExchangeRate{}, errByteArrayNull
    75  	}
    76  	pb := services.ExchangeRate{}
    77  	err := protobuf.Unmarshal(data, &pb)
    78  	if err != nil {
    79  		return ExchangeRate{}, err
    80  	}
    81  
    82  	exchangeRate := _ExchangeRateFromProtobuf(&pb)
    83  	if err != nil {
    84  		return ExchangeRate{}, err
    85  	}
    86  
    87  	return exchangeRate, nil
    88  }
    89  
    90  // String returns a string representation of the ExchangeRate
    91  func (exchange *ExchangeRate) String() string {
    92  	return fmt.Sprintf("Hbars: %d to Cents: %d, expires: %s", exchange.Hbars, exchange.cents, exchange.expirationTime.String())
    93  }