github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_nft_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  	"google.golang.org/protobuf/types/known/wrapperspb"
    28  )
    29  
    30  // TokenNftAllowance is a struct to encapsulate the nft methods for token allowance's.
    31  type TokenNftAllowance struct {
    32  	TokenID           *TokenID
    33  	SpenderAccountID  *AccountID
    34  	OwnerAccountID    *AccountID
    35  	SerialNumbers     []int64
    36  	AllSerials        bool
    37  	DelegatingSpender *AccountID
    38  }
    39  
    40  // NewTokenNftAllowance creates a TokenNftAllowance delegate for the given tokenID, owner, spender, serialNumbers, approvedForAll, and delegatingSpender
    41  func NewTokenNftAllowance(tokenID TokenID, owner AccountID, spender AccountID, serialNumbers []int64, approvedForAll bool, delegatingSpender AccountID) TokenNftAllowance {
    42  	return TokenNftAllowance{
    43  		TokenID:           &tokenID,
    44  		SpenderAccountID:  &spender,
    45  		OwnerAccountID:    &owner,
    46  		SerialNumbers:     serialNumbers,
    47  		AllSerials:        approvedForAll,
    48  		DelegatingSpender: &delegatingSpender,
    49  	}
    50  }
    51  
    52  func _TokenNftAllowanceFromProtobuf(pb *services.NftAllowance) TokenNftAllowance {
    53  	body := TokenNftAllowance{
    54  		AllSerials:    pb.ApprovedForAll.GetValue(),
    55  		SerialNumbers: pb.SerialNumbers,
    56  	}
    57  
    58  	if pb.TokenId != nil {
    59  		body.TokenID = _TokenIDFromProtobuf(pb.TokenId)
    60  	}
    61  
    62  	if pb.Spender != nil {
    63  		body.SpenderAccountID = _AccountIDFromProtobuf(pb.Spender)
    64  	}
    65  
    66  	if pb.Owner != nil {
    67  		body.OwnerAccountID = _AccountIDFromProtobuf(pb.Owner)
    68  	}
    69  
    70  	if pb.DelegatingSpender != nil {
    71  		body.DelegatingSpender = _AccountIDFromProtobuf(pb.DelegatingSpender)
    72  	}
    73  
    74  	return body
    75  }
    76  
    77  func _TokenNftWipeAllowanceProtobuf(pb *services.NftRemoveAllowance) TokenNftAllowance {
    78  	body := TokenNftAllowance{
    79  		SerialNumbers: pb.SerialNumbers,
    80  	}
    81  
    82  	if pb.TokenId != nil {
    83  		body.TokenID = _TokenIDFromProtobuf(pb.TokenId)
    84  	}
    85  
    86  	if pb.Owner != nil {
    87  		body.SpenderAccountID = _AccountIDFromProtobuf(pb.Owner)
    88  	}
    89  
    90  	return body
    91  }
    92  
    93  func (approval *TokenNftAllowance) _ToProtobuf() *services.NftAllowance {
    94  	body := &services.NftAllowance{
    95  		ApprovedForAll: &wrapperspb.BoolValue{Value: approval.AllSerials},
    96  		SerialNumbers:  approval.SerialNumbers,
    97  	}
    98  
    99  	if approval.SpenderAccountID != nil {
   100  		body.Spender = approval.SpenderAccountID._ToProtobuf()
   101  	}
   102  
   103  	if approval.OwnerAccountID != nil {
   104  		body.Owner = approval.OwnerAccountID._ToProtobuf()
   105  	}
   106  
   107  	if approval.TokenID != nil {
   108  		body.TokenId = approval.TokenID._ToProtobuf()
   109  	}
   110  
   111  	if approval.DelegatingSpender != nil {
   112  		body.DelegatingSpender = approval.DelegatingSpender._ToProtobuf()
   113  	}
   114  
   115  	return body
   116  }
   117  
   118  func (approval *TokenNftAllowance) _ToWipeProtobuf() *services.NftRemoveAllowance {
   119  	body := &services.NftRemoveAllowance{
   120  		SerialNumbers: approval.SerialNumbers,
   121  	}
   122  
   123  	if approval.OwnerAccountID != nil {
   124  		body.Owner = approval.OwnerAccountID._ToProtobuf()
   125  	}
   126  
   127  	if approval.TokenID != nil {
   128  		body.TokenId = approval.TokenID._ToProtobuf()
   129  	}
   130  
   131  	return body
   132  }
   133  
   134  // String returns a string representation of the TokenNftAllowance
   135  func (approval *TokenNftAllowance) String() string {
   136  	var owner string
   137  	var spender string
   138  	var token string
   139  	var serials string
   140  
   141  	if approval.OwnerAccountID != nil {
   142  		owner = approval.OwnerAccountID.String()
   143  	}
   144  
   145  	if approval.SpenderAccountID != nil {
   146  		spender = approval.SpenderAccountID.String()
   147  	}
   148  
   149  	if approval.TokenID != nil {
   150  		token = approval.TokenID.String()
   151  	}
   152  
   153  	for _, serial := range approval.SerialNumbers {
   154  		serials += fmt.Sprintf("%d, ", serial)
   155  	}
   156  
   157  	return fmt.Sprintf("OwnerAccountID: %s, SpenderAccountID: %s, TokenID: %s, Serials: %s, ApprovedForAll: %t", owner, spender, token, serials, approval.AllSerials)
   158  }