github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_balance_map.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  import (
    23  	"github.com/hashgraph/hedera-protobufs-go/services"
    24  )
    25  
    26  type TokenBalanceMap struct {
    27  	balances map[string]uint64
    28  }
    29  
    30  // Get returns the balance of the given tokenID
    31  func (tokenBalances *TokenBalanceMap) Get(tokenID TokenID) uint64 {
    32  	return tokenBalances.balances[tokenID.String()]
    33  }
    34  func _TokenBalanceMapFromProtobuf(pb []*services.TokenBalance) TokenBalanceMap {
    35  	balances := make(map[string]uint64)
    36  
    37  	for _, tokenBalance := range pb {
    38  		balances[_TokenIDFromProtobuf(tokenBalance.TokenId).String()] = tokenBalance.Balance
    39  	}
    40  
    41  	return TokenBalanceMap{balances}
    42  }
    43  
    44  func (tokenBalances *TokenBalanceMap) _ToProtobuf() []*services.TokenBalance { // nolint
    45  	decimals := make([]*services.TokenBalance, 0)
    46  
    47  	for s, t := range tokenBalances.balances {
    48  		token, err := TokenIDFromString(s)
    49  		if err != nil {
    50  			return []*services.TokenBalance{}
    51  		}
    52  		decimals = append(decimals, &services.TokenBalance{
    53  			TokenId: token._ToProtobuf(),
    54  			Balance: t,
    55  		})
    56  	}
    57  
    58  	return decimals
    59  }