-
Notifications
You must be signed in to change notification settings - Fork 66
/
contract_info.go
157 lines (136 loc) · 5.02 KB
/
contract_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package hedera
/*-
*
* Hedera Go SDK
*
* Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import (
"time"
"github.com/hashgraph/hedera-sdk-go/v2/proto/services"
protobuf "google.golang.org/protobuf/proto"
)
// Current information on the smart contract instance, including its balance.
type ContractInfo struct {
AccountID AccountID
ContractID ContractID
ContractAccountID string
AdminKey Key
ExpirationTime time.Time
AutoRenewPeriod time.Duration
Storage uint64
ContractMemo string
Balance uint64
TokenRelationships []*TokenRelationship
LedgerID LedgerID
AutoRenewAccountID *AccountID
MaxAutomaticTokenAssociations int32
StakingInfo *StakingInfo
}
func _ContractInfoFromProtobuf(contractInfo *services.ContractGetInfoResponse_ContractInfo) (ContractInfo, error) {
if contractInfo == nil {
return ContractInfo{}, errParameterNull
}
var adminKey Key
var err error
if contractInfo.GetAdminKey() != nil {
adminKey, err = _KeyFromProtobuf(contractInfo.GetAdminKey())
if err != nil {
return ContractInfo{}, err
}
}
accountID := AccountID{}
if contractInfo.AccountID != nil {
accountID = *_AccountIDFromProtobuf(contractInfo.AccountID)
}
contractID := ContractID{}
if contractInfo.ContractID != nil {
contractID = *_ContractIDFromProtobuf(contractInfo.ContractID)
}
var autoRenewAccountID *AccountID
if contractInfo.AutoRenewAccountId != nil {
autoRenewAccountID = _AccountIDFromProtobuf(contractInfo.AutoRenewAccountId)
}
var stakingInfo StakingInfo
if contractInfo.StakingInfo != nil {
stakingInfo = _StakingInfoFromProtobuf(contractInfo.StakingInfo)
}
var tokenRelationships []*TokenRelationship
if contractInfo.TokenRelationships != nil { // nolint
tokenRelationships = _TokenRelationshipsFromProtobuf(contractInfo.TokenRelationships) // nolint
}
return ContractInfo{
AccountID: accountID,
ContractID: contractID,
ContractAccountID: contractInfo.ContractAccountID,
AdminKey: adminKey,
ExpirationTime: _TimeFromProtobuf(contractInfo.ExpirationTime),
AutoRenewPeriod: _DurationFromProtobuf(contractInfo.AutoRenewPeriod),
Storage: uint64(contractInfo.Storage),
ContractMemo: contractInfo.Memo,
Balance: contractInfo.Balance,
LedgerID: LedgerID{contractInfo.LedgerId},
AutoRenewAccountID: autoRenewAccountID,
MaxAutomaticTokenAssociations: contractInfo.MaxAutomaticTokenAssociations,
StakingInfo: &stakingInfo,
TokenRelationships: tokenRelationships,
}, nil
}
func (contractInfo *ContractInfo) _ToProtobuf() *services.ContractGetInfoResponse_ContractInfo {
body := &services.ContractGetInfoResponse_ContractInfo{
ContractID: contractInfo.ContractID._ToProtobuf(),
AccountID: contractInfo.AccountID._ToProtobuf(),
ContractAccountID: contractInfo.ContractAccountID,
AdminKey: contractInfo.AdminKey._ToProtoKey(),
ExpirationTime: _TimeToProtobuf(contractInfo.ExpirationTime),
AutoRenewPeriod: _DurationToProtobuf(contractInfo.AutoRenewPeriod),
Storage: int64(contractInfo.Storage),
Memo: contractInfo.ContractMemo,
Balance: contractInfo.Balance,
LedgerId: contractInfo.LedgerID.ToBytes(),
}
if contractInfo.AutoRenewAccountID != nil {
body.AutoRenewAccountId = contractInfo.AutoRenewAccountID._ToProtobuf()
}
if contractInfo.StakingInfo != nil {
body.StakingInfo = contractInfo.StakingInfo._ToProtobuf()
}
return body
}
// ToBytes returns a serialized version of the ContractInfo object
func (contractInfo ContractInfo) ToBytes() []byte {
data, err := protobuf.Marshal(contractInfo._ToProtobuf())
if err != nil {
return make([]byte, 0)
}
return data
}
// ContractInfoFromBytes returns a ContractInfo object deserialized from bytes
func ContractInfoFromBytes(data []byte) (ContractInfo, error) {
if data == nil {
return ContractInfo{}, errByteArrayNull
}
pb := services.ContractGetInfoResponse_ContractInfo{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return ContractInfo{}, err
}
info, err := _ContractInfoFromProtobuf(&pb)
if err != nil {
return ContractInfo{}, err
}
return info, nil
}