forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract_update_transaction.go
343 lines (284 loc) · 12.3 KB
/
contract_update_transaction.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package hedera
import (
"time"
"github.com/hashgraph/hedera-sdk-go/v2/proto"
)
// ContractUpdateTransaction is used to modify a smart contract instance to have the given parameter values. Any nil
// field is ignored (left unchanged). If only the contractInstanceExpirationTime is being modified, then no signature is
// needed on this transaction other than for the account paying for the transaction itself. But if any of the other
// fields are being modified, then it must be signed by the adminKey. The use of adminKey is not currently supported in
// this API, but in the future will be implemented to allow these fields to be modified, and also to make modifications
// to the state of the instance. If the contract is created with no admin key, then none of the fields can be changed
// that need an admin signature, and therefore no admin key can ever be added. So if there is no admin key, then things
// like the bytecode are immutable. But if there is an admin key, then they can be changed.
//
// For example, the admin key might be a threshold key, which requires 3 of 5 binding arbitration judges to agree before
// the bytecode can be changed. This can be used to add flexibility to the management of smart contract behavior. But
// this is optional. If the smart contract is created without an admin key, then such a key can never be added, and its
// bytecode will be immutable.
type ContractUpdateTransaction struct {
Transaction
pb *proto.ContractUpdateTransactionBody
}
// NewContractUpdateTransaction creates a ContractUpdateTransaction transaction which can be
// used to construct and execute a Contract Update Transaction.
func NewContractUpdateTransaction() *ContractUpdateTransaction {
pb := &proto.ContractUpdateTransactionBody{}
transaction := ContractUpdateTransaction{
pb: pb,
Transaction: newTransaction(),
}
transaction.SetMaxTransactionFee(NewHbar(2))
return &transaction
}
func contractUpdateTransactionFromProtobuf(transaction Transaction, pb *proto.TransactionBody) ContractUpdateTransaction {
return ContractUpdateTransaction{
Transaction: transaction,
pb: pb.GetContractUpdateInstance(),
}
}
// SetContractID sets The Contract ID instance to update (this can't be changed on the contract)
func (transaction *ContractUpdateTransaction) SetContractID(contractID ContractID) *ContractUpdateTransaction {
transaction.pb.ContractID = contractID.toProtobuf()
return transaction
}
func (transaction *ContractUpdateTransaction) GetContractID() ContractID {
return contractIDFromProtobuf(transaction.pb.GetContractID())
}
// SetBytecodeFileID sets the file ID of file containing the smart contract byte code. A copy will be made and held by
// the contract instance, and have the same expiration time as the instance.
func (transaction *ContractUpdateTransaction) SetBytecodeFileID(fileID FileID) *ContractUpdateTransaction {
transaction.requireNotFrozen()
transaction.pb.FileID = fileID.toProtobuf()
return transaction
}
func (transaction *ContractUpdateTransaction) GetBytecodeFileID() FileID {
return fileIDFromProtobuf(transaction.pb.GetFileID())
}
// SetAdminKey sets the key which can be used to arbitrarily modify the state of the instance by signing a
// ContractUpdateTransaction to modify it. If the admin key was never set then such modifications are not possible,
// and there is no administrator that can overrIDe the normal operation of the smart contract instance.
func (transaction *ContractUpdateTransaction) SetAdminKey(publicKey PublicKey) *ContractUpdateTransaction {
transaction.requireNotFrozen()
transaction.pb.AdminKey = publicKey.toProtoKey()
return transaction
}
func (transaction *ContractUpdateTransaction) GetAdminKey() (Key, error) {
return keyFromProtobuf(transaction.pb.GetAdminKey())
}
// SetProxyAccountID sets the ID of the account to which this contract is proxy staked. If proxyAccountID is left unset,
// is an invalID account, or is an account that isn't a node, then this contract is automatically proxy staked to a node
// chosen by the network, but without earning payments. If the proxyAccountID account refuses to accept proxy staking,
// or if it is not currently running a node, then it will behave as if proxyAccountID was never set.
func (transaction *ContractUpdateTransaction) SetProxyAccountID(accountID AccountID) *ContractUpdateTransaction {
transaction.requireNotFrozen()
transaction.pb.ProxyAccountID = accountID.toProtobuf()
return transaction
}
func (transaction *ContractUpdateTransaction) GetProxyAccountID() AccountID {
return accountIDFromProtobuf(transaction.pb.GetProxyAccountID())
}
// SetAutoRenewPeriod sets the duration for which the contract instance will automatically charge its account to
// renew for.
func (transaction *ContractUpdateTransaction) SetAutoRenewPeriod(autoRenewPeriod time.Duration) *ContractUpdateTransaction {
transaction.requireNotFrozen()
transaction.pb.AutoRenewPeriod = durationToProtobuf(autoRenewPeriod)
return transaction
}
func (transaction *ContractUpdateTransaction) GetAutoRenewPeriod() time.Duration {
return durationFromProtobuf(transaction.pb.GetAutoRenewPeriod())
}
// SetExpirationTime extends the expiration of the instance and its account to the provIDed time. If the time provIDed
// is the current or past time, then there will be no effect.
func (transaction *ContractUpdateTransaction) SetExpirationTime(expiration time.Time) *ContractUpdateTransaction {
transaction.requireNotFrozen()
transaction.pb.ExpirationTime = timeToProtobuf(expiration)
return transaction
}
func (transaction *ContractUpdateTransaction) GetExpirationTime() time.Time {
return timeFromProtobuf(transaction.pb.GetExpirationTime())
}
// SetContractMemo sets the memo associated with the contract (max 100 bytes)
func (transaction *ContractUpdateTransaction) SetContractMemo(memo string) *ContractUpdateTransaction {
transaction.requireNotFrozen()
transaction.pb.Memo = memo
return transaction
}
func (transaction *ContractUpdateTransaction) GetContractMemo() string {
return transaction.pb.GetMemo()
}
//
// The following methods must be copy-pasted/overriden at the bottom of **every** _transaction.go file
// We override the embedded fluent setter methods to return the outer type
//
func contractUpdateTransaction_getMethod(request request, channel *channel) method {
return method{
transaction: channel.getContract().UpdateContract,
}
}
func (transaction *ContractUpdateTransaction) IsFrozen() bool {
return transaction.isFrozen()
}
// Sign uses the provided privateKey to sign the transaction.
func (transaction *ContractUpdateTransaction) Sign(
privateKey PrivateKey,
) *ContractUpdateTransaction {
return transaction.SignWith(privateKey.PublicKey(), privateKey.Sign)
}
func (transaction *ContractUpdateTransaction) SignWithOperator(
client *Client,
) (*ContractUpdateTransaction, error) {
// If the transaction is not signed by the operator, we need
// to sign the transaction with the operator
if client == nil {
return nil, errNoClientProvided
} else if client.operator == nil {
return nil, errClientOperatorSigning
}
if !transaction.IsFrozen() {
_, err := transaction.FreezeWith(client)
if err != nil {
return transaction, err
}
}
return transaction.SignWith(client.operator.publicKey, client.operator.signer), nil
}
// SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
// with the publicKey as the map key.
func (transaction *ContractUpdateTransaction) SignWith(
publicKey PublicKey,
signer TransactionSigner,
) *ContractUpdateTransaction {
if !transaction.IsFrozen() {
_, _ = transaction.Freeze()
} else {
transaction.transactions = make([]*proto.Transaction, 0)
}
if transaction.keyAlreadySigned(publicKey) {
return transaction
}
for index := 0; index < len(transaction.signedTransactions); index++ {
signature := signer(transaction.signedTransactions[index].GetBodyBytes())
transaction.signedTransactions[index].SigMap.SigPair = append(
transaction.signedTransactions[index].SigMap.SigPair,
publicKey.toSignaturePairProtobuf(signature),
)
}
return transaction
}
// Execute executes the Transaction with the provided client
func (transaction *ContractUpdateTransaction) Execute(
client *Client,
) (TransactionResponse, error) {
if client == nil || client.operator == nil {
return TransactionResponse{}, errNoClientProvided
}
if transaction.freezeError != nil {
return TransactionResponse{}, transaction.freezeError
}
if !transaction.IsFrozen() {
_, err := transaction.FreezeWith(client)
if err != nil {
return TransactionResponse{}, err
}
}
transactionID := transaction.GetTransactionID()
if !client.GetOperatorAccountID().isZero() && client.GetOperatorAccountID().equals(transactionID.AccountID) {
transaction.SignWith(
client.GetOperatorPublicKey(),
client.operator.signer,
)
}
resp, err := execute(
client,
request{
transaction: &transaction.Transaction,
},
transaction_shouldRetry,
transaction_makeRequest,
transaction_advanceRequest,
transaction_getNodeAccountID,
contractUpdateTransaction_getMethod,
transaction_mapResponseStatus,
transaction_mapResponse,
)
if err != nil {
return TransactionResponse{
TransactionID: transaction.GetTransactionID(),
NodeID: resp.transaction.NodeID,
}, err
}
hash, err := transaction.GetTransactionHash()
return TransactionResponse{
TransactionID: transaction.GetTransactionID(),
NodeID: resp.transaction.NodeID,
Hash: hash,
}, nil
}
func (transaction *ContractUpdateTransaction) onFreeze(
pbBody *proto.TransactionBody,
) bool {
pbBody.Data = &proto.TransactionBody_ContractUpdateInstance{
ContractUpdateInstance: transaction.pb,
}
return true
}
func (transaction *ContractUpdateTransaction) Freeze() (*ContractUpdateTransaction, error) {
return transaction.FreezeWith(nil)
}
func (transaction *ContractUpdateTransaction) FreezeWith(client *Client) (*ContractUpdateTransaction, error) {
transaction.initFee(client)
if err := transaction.initTransactionID(client); err != nil {
return transaction, err
}
if !transaction.onFreeze(transaction.pbBody) {
return transaction, nil
}
return transaction, transaction_freezeWith(&transaction.Transaction, client)
}
func (transaction *ContractUpdateTransaction) GetMaxTransactionFee() Hbar {
return transaction.Transaction.GetMaxTransactionFee()
}
// SetMaxTransactionFee sets the max transaction fee for this ContractUpdateTransaction.
func (transaction *ContractUpdateTransaction) SetMaxTransactionFee(fee Hbar) *ContractUpdateTransaction {
transaction.requireNotFrozen()
transaction.Transaction.SetMaxTransactionFee(fee)
return transaction
}
func (transaction *ContractUpdateTransaction) GetTransactionMemo() string {
return transaction.Transaction.GetTransactionMemo()
}
// SetTransactionMemo sets the memo for this ContractUpdateTransaction.
func (transaction *ContractUpdateTransaction) SetTransactionMemo(memo string) *ContractUpdateTransaction {
transaction.requireNotFrozen()
transaction.Transaction.SetTransactionMemo(memo)
return transaction
}
func (transaction *ContractUpdateTransaction) GetTransactionValidDuration() time.Duration {
return transaction.Transaction.GetTransactionValidDuration()
}
// SetTransactionValidDuration sets the valid duration for this ContractUpdateTransaction.
func (transaction *ContractUpdateTransaction) SetTransactionValidDuration(duration time.Duration) *ContractUpdateTransaction {
transaction.requireNotFrozen()
transaction.Transaction.SetTransactionValidDuration(duration)
return transaction
}
func (transaction *ContractUpdateTransaction) GetTransactionID() TransactionID {
return transaction.Transaction.GetTransactionID()
}
// SetTransactionID sets the TransactionID for this ContractUpdateTransaction.
func (transaction *ContractUpdateTransaction) SetTransactionID(transactionID TransactionID) *ContractUpdateTransaction {
transaction.requireNotFrozen()
transaction.Transaction.SetTransactionID(transactionID)
return transaction
}
// SetNodeAccountID sets the node AccountID for this ContractUpdateTransaction.
func (transaction *ContractUpdateTransaction) SetNodeAccountIDs(nodeID []AccountID) *ContractUpdateTransaction {
transaction.requireNotFrozen()
transaction.Transaction.SetNodeAccountIDs(nodeID)
return transaction
}
func (transaction *ContractUpdateTransaction) SetMaxRetry(count int) *ContractUpdateTransaction {
transaction.Transaction.SetMaxRetry(count)
return transaction
}