github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/hbar_allowance.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  )
    28  
    29  // An approved allowance of hbar transfers for a spender.
    30  type HbarAllowance struct {
    31  	OwnerAccountID   *AccountID
    32  	SpenderAccountID *AccountID
    33  	Amount           int64
    34  }
    35  
    36  // NewHbarAllowance creates a new HbarAllowance with the given owner, spender, and amount.
    37  func NewHbarAllowance(ownerAccountID AccountID, spenderAccountID AccountID, amount int64) HbarAllowance { //nolint
    38  	return HbarAllowance{
    39  		OwnerAccountID:   &ownerAccountID,
    40  		SpenderAccountID: &spenderAccountID,
    41  		Amount:           amount,
    42  	}
    43  }
    44  
    45  func _HbarAllowanceFromProtobuf(pb *services.CryptoAllowance) HbarAllowance {
    46  	body := HbarAllowance{
    47  		Amount: pb.Amount,
    48  	}
    49  
    50  	if pb.Spender != nil {
    51  		body.SpenderAccountID = _AccountIDFromProtobuf(pb.Spender)
    52  	}
    53  
    54  	if pb.Owner != nil {
    55  		body.OwnerAccountID = _AccountIDFromProtobuf(pb.Owner)
    56  	}
    57  
    58  	return body
    59  }
    60  
    61  func (approval *HbarAllowance) _ToProtobuf() *services.CryptoAllowance {
    62  	body := &services.CryptoAllowance{
    63  		Amount: approval.Amount,
    64  	}
    65  
    66  	if approval.SpenderAccountID != nil {
    67  		body.Spender = approval.SpenderAccountID._ToProtobuf()
    68  	}
    69  
    70  	if approval.OwnerAccountID != nil {
    71  		body.Owner = approval.OwnerAccountID._ToProtobuf()
    72  	}
    73  
    74  	return body
    75  }
    76  
    77  // String returns a string representation of the HbarAllowance
    78  func (approval *HbarAllowance) String() string {
    79  	if approval.OwnerAccountID != nil && approval.SpenderAccountID != nil { //nolint
    80  		return fmt.Sprintf("OwnerAccountID: %s, SpenderAccountID: %s, Amount: %s", approval.OwnerAccountID.String(), approval.SpenderAccountID.String(), HbarFromTinybar(approval.Amount).String())
    81  	} else if approval.OwnerAccountID != nil {
    82  		return fmt.Sprintf("OwnerAccountID: %s, Amount: %s", approval.OwnerAccountID.String(), HbarFromTinybar(approval.Amount).String())
    83  	} else if approval.SpenderAccountID != nil {
    84  		return fmt.Sprintf("SpenderAccountID: %s, Amount: %s", approval.SpenderAccountID.String(), HbarFromTinybar(approval.Amount).String())
    85  	}
    86  
    87  	return ""
    88  }