github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_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 // Current information on the smart contract instance, including its balance. 31 type ContractInfo struct { 32 AccountID AccountID 33 ContractID ContractID 34 ContractAccountID string 35 AdminKey Key 36 ExpirationTime time.Time 37 AutoRenewPeriod time.Duration 38 Storage uint64 39 ContractMemo string 40 Balance uint64 41 TokenRelationships []*TokenRelationship 42 LedgerID LedgerID 43 AutoRenewAccountID *AccountID 44 MaxAutomaticTokenAssociations int32 45 StakingInfo *StakingInfo 46 } 47 48 func _ContractInfoFromProtobuf(contractInfo *services.ContractGetInfoResponse_ContractInfo) (ContractInfo, error) { 49 if contractInfo == nil { 50 return ContractInfo{}, errParameterNull 51 } 52 53 var adminKey Key 54 var err error 55 if contractInfo.GetAdminKey() != nil { 56 adminKey, err = _KeyFromProtobuf(contractInfo.GetAdminKey()) 57 if err != nil { 58 return ContractInfo{}, err 59 } 60 } 61 62 accountID := AccountID{} 63 if contractInfo.AccountID != nil { 64 accountID = *_AccountIDFromProtobuf(contractInfo.AccountID) 65 } 66 67 contractID := ContractID{} 68 if contractInfo.ContractID != nil { 69 contractID = *_ContractIDFromProtobuf(contractInfo.ContractID) 70 } 71 72 var autoRenewAccountID *AccountID 73 if contractInfo.AutoRenewAccountId != nil { 74 autoRenewAccountID = _AccountIDFromProtobuf(contractInfo.AutoRenewAccountId) 75 } 76 77 var stakingInfo StakingInfo 78 if contractInfo.StakingInfo != nil { 79 stakingInfo = _StakingInfoFromProtobuf(contractInfo.StakingInfo) 80 } 81 82 var tokenRelationships []*TokenRelationship 83 if contractInfo.TokenRelationships != nil { // nolint 84 tokenRelationships = _TokenRelationshipsFromProtobuf(contractInfo.TokenRelationships) // nolint 85 } 86 87 return ContractInfo{ 88 AccountID: accountID, 89 ContractID: contractID, 90 ContractAccountID: contractInfo.ContractAccountID, 91 AdminKey: adminKey, 92 ExpirationTime: _TimeFromProtobuf(contractInfo.ExpirationTime), 93 AutoRenewPeriod: _DurationFromProtobuf(contractInfo.AutoRenewPeriod), 94 Storage: uint64(contractInfo.Storage), 95 ContractMemo: contractInfo.Memo, 96 Balance: contractInfo.Balance, 97 LedgerID: LedgerID{contractInfo.LedgerId}, 98 AutoRenewAccountID: autoRenewAccountID, 99 MaxAutomaticTokenAssociations: contractInfo.MaxAutomaticTokenAssociations, 100 StakingInfo: &stakingInfo, 101 TokenRelationships: tokenRelationships, 102 }, nil 103 } 104 105 func (contractInfo *ContractInfo) _ToProtobuf() *services.ContractGetInfoResponse_ContractInfo { 106 body := &services.ContractGetInfoResponse_ContractInfo{ 107 ContractID: contractInfo.ContractID._ToProtobuf(), 108 AccountID: contractInfo.AccountID._ToProtobuf(), 109 ContractAccountID: contractInfo.ContractAccountID, 110 AdminKey: contractInfo.AdminKey._ToProtoKey(), 111 ExpirationTime: _TimeToProtobuf(contractInfo.ExpirationTime), 112 AutoRenewPeriod: _DurationToProtobuf(contractInfo.AutoRenewPeriod), 113 Storage: int64(contractInfo.Storage), 114 Memo: contractInfo.ContractMemo, 115 Balance: contractInfo.Balance, 116 LedgerId: contractInfo.LedgerID.ToBytes(), 117 } 118 119 if contractInfo.AutoRenewAccountID != nil { 120 body.AutoRenewAccountId = contractInfo.AutoRenewAccountID._ToProtobuf() 121 } 122 123 if contractInfo.StakingInfo != nil { 124 body.StakingInfo = contractInfo.StakingInfo._ToProtobuf() 125 } 126 127 return body 128 } 129 130 // ToBytes returns a serialized version of the ContractInfo object 131 func (contractInfo ContractInfo) ToBytes() []byte { 132 data, err := protobuf.Marshal(contractInfo._ToProtobuf()) 133 if err != nil { 134 return make([]byte, 0) 135 } 136 137 return data 138 } 139 140 // ContractInfoFromBytes returns a ContractInfo object deserialized from bytes 141 func ContractInfoFromBytes(data []byte) (ContractInfo, error) { 142 if data == nil { 143 return ContractInfo{}, errByteArrayNull 144 } 145 pb := services.ContractGetInfoResponse_ContractInfo{} 146 err := protobuf.Unmarshal(data, &pb) 147 if err != nil { 148 return ContractInfo{}, err 149 } 150 151 info, err := _ContractInfoFromProtobuf(&pb) 152 if err != nil { 153 return ContractInfo{}, err 154 } 155 156 return info, nil 157 }