github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_info.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  	"time"
    25  
    26  	"github.com/hashgraph/hedera-protobufs-go/services"
    27  	protobuf "google.golang.org/protobuf/proto"
    28  )
    29  
    30  // TokenInfo is the information about a token
    31  type TokenInfo struct {
    32  	TokenID             TokenID
    33  	Name                string
    34  	Symbol              string
    35  	Decimals            uint32
    36  	TotalSupply         uint64
    37  	Treasury            AccountID
    38  	AdminKey            Key
    39  	KycKey              Key
    40  	FreezeKey           Key
    41  	WipeKey             Key
    42  	SupplyKey           Key
    43  	DefaultFreezeStatus *bool
    44  	DefaultKycStatus    *bool
    45  	Deleted             bool
    46  	AutoRenewPeriod     *time.Duration
    47  	AutoRenewAccountID  AccountID
    48  	ExpirationTime      *time.Time
    49  	TokenMemo           string
    50  	TokenType           TokenType
    51  	SupplyType          TokenSupplyType
    52  	MaxSupply           int64
    53  	FeeScheduleKey      Key
    54  	CustomFees          []Fee
    55  	PauseKey            Key
    56  	PauseStatus         *bool
    57  	MetadataKey         Key
    58  	Metadata            []byte
    59  	LedgerID            LedgerID
    60  }
    61  
    62  func _FreezeStatusFromProtobuf(pb services.TokenFreezeStatus) *bool {
    63  	var freezeStatus bool
    64  	switch pb.Number() {
    65  	case 1:
    66  		freezeStatus = true
    67  	case 2:
    68  		freezeStatus = false
    69  	default:
    70  		return nil
    71  	}
    72  
    73  	return &freezeStatus
    74  }
    75  
    76  func _KycStatusFromProtobuf(pb services.TokenKycStatus) *bool {
    77  	var kycStatus bool
    78  	switch pb.Number() {
    79  	case 1:
    80  		kycStatus = true
    81  	case 2:
    82  		kycStatus = false
    83  	default:
    84  		return nil
    85  	}
    86  	return &kycStatus
    87  }
    88  
    89  func _PauseStatusFromProtobuf(pb services.TokenPauseStatus) *bool {
    90  	var pauseStatus bool
    91  	switch pb.Number() {
    92  	case 1:
    93  		pauseStatus = true
    94  	case 2:
    95  		pauseStatus = false
    96  	default:
    97  		return nil
    98  	}
    99  	return &pauseStatus
   100  }
   101  
   102  // FreezeStatusToProtobuf converts the TokenInfo's DefaultFreezeStatus to a protobuf TokenFreezeStatus
   103  func (tokenInfo *TokenInfo) FreezeStatusToProtobuf() *services.TokenFreezeStatus {
   104  	var freezeStatus services.TokenFreezeStatus
   105  
   106  	if tokenInfo.DefaultFreezeStatus == nil {
   107  		freezeStatus = services.TokenFreezeStatus_FreezeNotApplicable
   108  		return &freezeStatus
   109  	}
   110  
   111  	switch *tokenInfo.DefaultFreezeStatus {
   112  	case true:
   113  		freezeStatus = services.TokenFreezeStatus_Frozen
   114  	case false:
   115  		freezeStatus = services.TokenFreezeStatus_Unfrozen
   116  	default:
   117  		freezeStatus = services.TokenFreezeStatus_FreezeNotApplicable
   118  	}
   119  
   120  	return &freezeStatus
   121  }
   122  
   123  // KycStatusToProtobuf converts the TokenInfo's DefaultKycStatus to a protobuf TokenKycStatus
   124  func (tokenInfo *TokenInfo) KycStatusToProtobuf() *services.TokenKycStatus {
   125  	var kycStatus services.TokenKycStatus
   126  
   127  	if tokenInfo.DefaultKycStatus == nil {
   128  		kycStatus = services.TokenKycStatus_KycNotApplicable
   129  		return &kycStatus
   130  	}
   131  
   132  	switch *tokenInfo.DefaultKycStatus {
   133  	case true:
   134  		kycStatus = services.TokenKycStatus_Granted
   135  	case false:
   136  		kycStatus = services.TokenKycStatus_Revoked
   137  	default:
   138  		kycStatus = services.TokenKycStatus_KycNotApplicable
   139  	}
   140  
   141  	return &kycStatus
   142  }
   143  
   144  // PauseStatusToProtobuf converts the TokenInfo's PauseStatus to a protobuf TokenPauseStatus
   145  func (tokenInfo *TokenInfo) PauseStatusToProtobuf() *services.TokenPauseStatus {
   146  	var pauseStatus services.TokenPauseStatus
   147  
   148  	if tokenInfo.PauseStatus == nil {
   149  		pauseStatus = services.TokenPauseStatus_PauseNotApplicable
   150  		return &pauseStatus
   151  	}
   152  
   153  	switch *tokenInfo.PauseStatus {
   154  	case true:
   155  		pauseStatus = services.TokenPauseStatus_Paused
   156  	case false:
   157  		pauseStatus = services.TokenPauseStatus_Unpaused
   158  	default:
   159  		pauseStatus = services.TokenPauseStatus_PauseNotApplicable
   160  	}
   161  
   162  	return &pauseStatus
   163  }
   164  
   165  func _TokenInfoFromProtobuf(pb *services.TokenInfo) TokenInfo {
   166  	if pb == nil {
   167  		return TokenInfo{}
   168  	}
   169  
   170  	var adminKey Key
   171  	if pb.AdminKey != nil {
   172  		adminKey, _ = _KeyFromProtobuf(pb.AdminKey)
   173  	}
   174  
   175  	var kycKey Key
   176  	if pb.KycKey != nil {
   177  		kycKey, _ = _KeyFromProtobuf(pb.KycKey)
   178  	}
   179  
   180  	var freezeKey Key
   181  	if pb.FreezeKey != nil {
   182  		freezeKey, _ = _KeyFromProtobuf(pb.FreezeKey)
   183  	}
   184  
   185  	var wipeKey Key
   186  	if pb.WipeKey != nil {
   187  		wipeKey, _ = _KeyFromProtobuf(pb.WipeKey)
   188  	}
   189  
   190  	var supplyKey Key
   191  	if pb.SupplyKey != nil {
   192  		supplyKey, _ = _KeyFromProtobuf(pb.SupplyKey)
   193  	}
   194  
   195  	var pauseKey Key
   196  	if pb.PauseKey != nil {
   197  		pauseKey, _ = _KeyFromProtobuf(pb.PauseKey)
   198  	}
   199  
   200  	var metadataKey Key
   201  	if pb.MetadataKey != nil {
   202  		metadataKey, _ = _KeyFromProtobuf(pb.MetadataKey)
   203  	}
   204  
   205  	var feeScheduleKey Key
   206  	if pb.FeeScheduleKey != nil {
   207  		feeScheduleKey, _ = _KeyFromProtobuf(pb.FeeScheduleKey)
   208  	}
   209  
   210  	var autoRenewPeriod time.Duration
   211  	if pb.AutoRenewPeriod != nil {
   212  		autoRenewPeriod = time.Duration(pb.GetAutoRenewPeriod().Seconds * time.Second.Nanoseconds())
   213  	}
   214  
   215  	var expirationTime time.Time
   216  	if pb.Expiry != nil {
   217  		expirationTime = time.Unix(pb.GetExpiry().Seconds, int64(pb.GetExpiry().Nanos))
   218  	}
   219  
   220  	var autoRenewAccountID AccountID
   221  	if pb.AutoRenewAccount != nil {
   222  		autoRenewAccountID = *_AccountIDFromProtobuf(pb.AutoRenewAccount)
   223  	}
   224  
   225  	var treasury AccountID
   226  	if pb.Treasury != nil {
   227  		treasury = *_AccountIDFromProtobuf(pb.Treasury)
   228  	}
   229  
   230  	customFees := make([]Fee, 0)
   231  	if pb.CustomFees != nil {
   232  		for _, custom := range pb.CustomFees {
   233  			customFees = append(customFees, _CustomFeeFromProtobuf(custom))
   234  		}
   235  	}
   236  
   237  	tokenID := TokenID{}
   238  	if pb.TokenId != nil {
   239  		tokenID = *_TokenIDFromProtobuf(pb.TokenId)
   240  	}
   241  
   242  	var metadata []byte
   243  	if pb.Metadata != nil {
   244  		metadata = pb.Metadata
   245  	}
   246  
   247  	return TokenInfo{
   248  		TokenID:             tokenID,
   249  		Name:                pb.Name,
   250  		Symbol:              pb.Symbol,
   251  		Decimals:            pb.Decimals,
   252  		TotalSupply:         pb.TotalSupply,
   253  		Treasury:            treasury,
   254  		AdminKey:            adminKey,
   255  		KycKey:              kycKey,
   256  		FreezeKey:           freezeKey,
   257  		WipeKey:             wipeKey,
   258  		SupplyKey:           supplyKey,
   259  		DefaultFreezeStatus: _FreezeStatusFromProtobuf(pb.DefaultFreezeStatus),
   260  		DefaultKycStatus:    _KycStatusFromProtobuf(pb.DefaultKycStatus),
   261  		Deleted:             pb.Deleted,
   262  		AutoRenewPeriod:     &autoRenewPeriod,
   263  		AutoRenewAccountID:  autoRenewAccountID,
   264  		ExpirationTime:      &expirationTime,
   265  		TokenMemo:           pb.Memo,
   266  		TokenType:           TokenType(pb.TokenType),
   267  		SupplyType:          TokenSupplyType(pb.SupplyType),
   268  		MaxSupply:           pb.MaxSupply,
   269  		FeeScheduleKey:      feeScheduleKey,
   270  		CustomFees:          customFees,
   271  		PauseKey:            pauseKey,
   272  		MetadataKey:         metadataKey,
   273  		Metadata:            metadata,
   274  		PauseStatus:         _PauseStatusFromProtobuf(pb.PauseStatus),
   275  		LedgerID:            LedgerID{pb.LedgerId},
   276  	}
   277  }
   278  
   279  func (tokenInfo *TokenInfo) _ToProtobuf() *services.TokenInfo {
   280  	var adminKey *services.Key
   281  	if tokenInfo.AdminKey != nil {
   282  		adminKey = tokenInfo.AdminKey._ToProtoKey()
   283  	}
   284  
   285  	var kycKey *services.Key
   286  	if tokenInfo.KycKey != nil {
   287  		kycKey = tokenInfo.KycKey._ToProtoKey()
   288  	}
   289  
   290  	var freezeKey *services.Key
   291  	if tokenInfo.FreezeKey != nil {
   292  		freezeKey = tokenInfo.FreezeKey._ToProtoKey()
   293  	}
   294  
   295  	var wipeKey *services.Key
   296  	if tokenInfo.WipeKey != nil {
   297  		wipeKey = tokenInfo.WipeKey._ToProtoKey()
   298  	}
   299  
   300  	var supplyKey *services.Key
   301  	if tokenInfo.SupplyKey != nil {
   302  		supplyKey = tokenInfo.SupplyKey._ToProtoKey()
   303  	}
   304  
   305  	var pauseKey *services.Key
   306  	if tokenInfo.PauseKey != nil {
   307  		pauseKey = tokenInfo.PauseKey._ToProtoKey()
   308  	}
   309  
   310  	var metadataKey *services.Key
   311  	if tokenInfo.MetadataKey != nil {
   312  		metadataKey = tokenInfo.MetadataKey._ToProtoKey()
   313  	}
   314  
   315  	var feeScheduleKey *services.Key
   316  	if tokenInfo.FeeScheduleKey != nil {
   317  		feeScheduleKey = tokenInfo.FeeScheduleKey._ToProtoKey()
   318  	}
   319  
   320  	var autoRenewPeriod *services.Duration
   321  	if tokenInfo.AutoRenewPeriod != nil {
   322  		autoRenewPeriod = _DurationToProtobuf(*tokenInfo.AutoRenewPeriod)
   323  	}
   324  
   325  	var expirationTime *services.Timestamp
   326  	if tokenInfo.ExpirationTime != nil {
   327  		expirationTime = _TimeToProtobuf(*tokenInfo.ExpirationTime)
   328  	}
   329  
   330  	customFees := make([]*services.CustomFee, 0)
   331  	if tokenInfo.CustomFees != nil {
   332  		for _, customFee := range tokenInfo.CustomFees {
   333  			customFees = append(customFees, customFee._ToProtobuf())
   334  		}
   335  	}
   336  
   337  	var metadata []byte
   338  	if tokenInfo.Metadata != nil {
   339  		metadata = tokenInfo.Metadata
   340  	}
   341  
   342  	return &services.TokenInfo{
   343  		TokenId:             tokenInfo.TokenID._ToProtobuf(),
   344  		Name:                tokenInfo.Name,
   345  		Symbol:              tokenInfo.Symbol,
   346  		Decimals:            tokenInfo.Decimals,
   347  		TotalSupply:         tokenInfo.TotalSupply,
   348  		Treasury:            tokenInfo.Treasury._ToProtobuf(),
   349  		AdminKey:            adminKey,
   350  		KycKey:              kycKey,
   351  		FreezeKey:           freezeKey,
   352  		WipeKey:             wipeKey,
   353  		SupplyKey:           supplyKey,
   354  		DefaultFreezeStatus: *tokenInfo.FreezeStatusToProtobuf(),
   355  		DefaultKycStatus:    *tokenInfo.KycStatusToProtobuf(),
   356  		Deleted:             tokenInfo.Deleted,
   357  		AutoRenewAccount:    tokenInfo.AutoRenewAccountID._ToProtobuf(),
   358  		AutoRenewPeriod:     autoRenewPeriod,
   359  		Expiry:              expirationTime,
   360  		Memo:                tokenInfo.TokenMemo,
   361  		TokenType:           services.TokenType(tokenInfo.TokenType),
   362  		SupplyType:          services.TokenSupplyType(tokenInfo.SupplyType),
   363  		MaxSupply:           tokenInfo.MaxSupply,
   364  		FeeScheduleKey:      feeScheduleKey,
   365  		CustomFees:          customFees,
   366  		PauseKey:            pauseKey,
   367  		MetadataKey:         metadataKey,
   368  		Metadata:            metadata,
   369  		PauseStatus:         *tokenInfo.PauseStatusToProtobuf(),
   370  		LedgerId:            tokenInfo.LedgerID.ToBytes(),
   371  	}
   372  }
   373  
   374  // ToBytes returns the byte representation of the TokenInfo
   375  func (tokenInfo TokenInfo) ToBytes() []byte {
   376  	data, err := protobuf.Marshal(tokenInfo._ToProtobuf())
   377  	if err != nil {
   378  		return make([]byte, 0)
   379  	}
   380  
   381  	return data
   382  }
   383  
   384  // TokenInfoFromBytes returns a TokenInfo struct from a raw protobuf byte array
   385  func TokenInfoFromBytes(data []byte) (TokenInfo, error) {
   386  	if data == nil {
   387  		return TokenInfo{}, errByteArrayNull
   388  	}
   389  	pb := services.TokenInfo{}
   390  	err := protobuf.Unmarshal(data, &pb)
   391  	if err != nil {
   392  		return TokenInfo{}, err
   393  	}
   394  
   395  	return _TokenInfoFromProtobuf(&pb), nil
   396  }