github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_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 token transfers for a spender.
    30  type TokenAllowance struct {
    31  	TokenID          *TokenID
    32  	SpenderAccountID *AccountID
    33  	OwnerAccountID   *AccountID
    34  	Amount           int64
    35  }
    36  
    37  // NewTokenAllowance creates a TokenAllowance with the given tokenID, owner, spender, and amount
    38  func NewTokenAllowance(tokenID TokenID, owner AccountID, spender AccountID, amount int64) TokenAllowance { //nolint
    39  	return TokenAllowance{
    40  		TokenID:          &tokenID,
    41  		SpenderAccountID: &spender,
    42  		OwnerAccountID:   &owner,
    43  		Amount:           amount,
    44  	}
    45  }
    46  
    47  func _TokenAllowanceFromProtobuf(pb *services.TokenAllowance) TokenAllowance {
    48  	body := TokenAllowance{
    49  		Amount: pb.Amount,
    50  	}
    51  
    52  	if pb.TokenId != nil {
    53  		body.TokenID = _TokenIDFromProtobuf(pb.TokenId)
    54  	}
    55  
    56  	if pb.Spender != nil {
    57  		body.SpenderAccountID = _AccountIDFromProtobuf(pb.Spender)
    58  	}
    59  
    60  	if pb.Owner != nil {
    61  		body.OwnerAccountID = _AccountIDFromProtobuf(pb.Owner)
    62  	}
    63  
    64  	return body
    65  }
    66  
    67  func (approval *TokenAllowance) _ToProtobuf() *services.TokenAllowance {
    68  	body := &services.TokenAllowance{
    69  		Amount: approval.Amount,
    70  	}
    71  
    72  	if approval.SpenderAccountID != nil {
    73  		body.Spender = approval.SpenderAccountID._ToProtobuf()
    74  	}
    75  
    76  	if approval.TokenID != nil {
    77  		body.TokenId = approval.TokenID._ToProtobuf()
    78  	}
    79  
    80  	if approval.OwnerAccountID != nil {
    81  		body.Owner = approval.OwnerAccountID._ToProtobuf()
    82  	}
    83  
    84  	return body
    85  }
    86  
    87  // String returns a string representation of the TokenAllowance
    88  func (approval *TokenAllowance) String() string {
    89  	var owner string
    90  	var spender string
    91  	var token string
    92  
    93  	if approval.OwnerAccountID != nil {
    94  		owner = approval.OwnerAccountID.String()
    95  	}
    96  
    97  	if approval.SpenderAccountID != nil {
    98  		spender = approval.SpenderAccountID.String()
    99  	}
   100  
   101  	if approval.TokenID != nil {
   102  		token = approval.TokenID.String()
   103  	}
   104  
   105  	return fmt.Sprintf("OwnerAccountID: %s, SpenderAccountID: %s, TokenID: %s, Amount: %s", owner, spender, token, HbarFromTinybar(approval.Amount).String())
   106  }