github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/system_delete_transaction.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 ) 28 29 // Delete a file or smart contract - can only be done with a Hedera admin. 30 // When it is deleted, it immediately disappears from the system as seen by the user, 31 // but is still stored internally until the expiration time, at which time it 32 // is truly and permanently deleted. 33 // Until that time, it can be undeleted by the Hedera admin. 34 // When a smart contract is deleted, the cryptocurrency account within it continues 35 // to exist, and is not affected by the expiration time here. 36 type SystemDeleteTransaction struct { 37 Transaction 38 contractID *ContractID 39 fileID *FileID 40 expirationTime *time.Time 41 } 42 43 // NewSystemDeleteTransaction creates a SystemDeleteTransaction transaction which can be 44 // used to construct and execute a System Delete Transaction. 45 func NewSystemDeleteTransaction() *SystemDeleteTransaction { 46 tx := SystemDeleteTransaction{ 47 Transaction: _NewTransaction(), 48 } 49 tx._SetDefaultMaxTransactionFee(NewHbar(2)) 50 51 return &tx 52 } 53 54 func _SystemDeleteTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *SystemDeleteTransaction { 55 expiration := time.Date( 56 time.Now().Year(), time.Now().Month(), time.Now().Day(), 57 time.Now().Hour(), time.Now().Minute(), 58 int(pb.GetSystemDelete().ExpirationTime.Seconds), time.Now().Nanosecond(), time.Now().Location(), 59 ) 60 return &SystemDeleteTransaction{ 61 Transaction: tx, 62 contractID: _ContractIDFromProtobuf(pb.GetSystemDelete().GetContractID()), 63 fileID: _FileIDFromProtobuf(pb.GetSystemDelete().GetFileID()), 64 expirationTime: &expiration, 65 } 66 } 67 68 // SetExpirationTime sets the time at which this transaction will expire. 69 func (tx *SystemDeleteTransaction) SetExpirationTime(expiration time.Time) *SystemDeleteTransaction { 70 tx._RequireNotFrozen() 71 tx.expirationTime = &expiration 72 return tx 73 } 74 75 // GetExpirationTime returns the time at which this transaction will expire. 76 func (tx *SystemDeleteTransaction) GetExpirationTime() int64 { 77 if tx.expirationTime != nil { 78 return tx.expirationTime.Unix() 79 } 80 81 return 0 82 } 83 84 // SetContractID sets the ContractID of the contract which will be deleted. 85 func (tx *SystemDeleteTransaction) SetContractID(contractID ContractID) *SystemDeleteTransaction { 86 tx._RequireNotFrozen() 87 tx.contractID = &contractID 88 return tx 89 } 90 91 // GetContractID returns the ContractID of the contract which will be deleted. 92 func (tx *SystemDeleteTransaction) GetContractID() ContractID { 93 if tx.contractID == nil { 94 return ContractID{} 95 } 96 97 return *tx.contractID 98 } 99 100 // SetFileID sets the FileID of the file which will be deleted. 101 func (tx *SystemDeleteTransaction) SetFileID(fileID FileID) *SystemDeleteTransaction { 102 tx._RequireNotFrozen() 103 tx.fileID = &fileID 104 return tx 105 } 106 107 // GetFileID returns the FileID of the file which will be deleted. 108 func (tx *SystemDeleteTransaction) GetFileID() FileID { 109 if tx.fileID == nil { 110 return FileID{} 111 } 112 113 return *tx.fileID 114 } 115 116 // ---- Required Interfaces ---- // 117 118 // Sign uses the provided privateKey to sign the transaction. 119 func (tx *SystemDeleteTransaction) Sign(privateKey PrivateKey) *SystemDeleteTransaction { 120 tx.Transaction.Sign(privateKey) 121 return tx 122 } 123 124 // SignWithOperator signs the transaction with client's operator privateKey. 125 func (tx *SystemDeleteTransaction) SignWithOperator(client *Client) (*SystemDeleteTransaction, error) { 126 _, err := tx.Transaction.signWithOperator(client, tx) 127 if err != nil { 128 return nil, err 129 } 130 return tx, nil 131 } 132 133 // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map 134 // with the publicKey as the map key. 135 func (tx *SystemDeleteTransaction) SignWith( 136 publicKey PublicKey, 137 signer TransactionSigner, 138 ) *SystemDeleteTransaction { 139 tx.Transaction.SignWith(publicKey, signer) 140 return tx 141 } 142 143 // AddSignature adds a signature to the transaction. 144 func (tx *SystemDeleteTransaction) AddSignature(publicKey PublicKey, signature []byte) *SystemDeleteTransaction { 145 tx.Transaction.AddSignature(publicKey, signature) 146 return tx 147 } 148 149 // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.) 150 func (tx *SystemDeleteTransaction) SetGrpcDeadline(deadline *time.Duration) *SystemDeleteTransaction { 151 tx.Transaction.SetGrpcDeadline(deadline) 152 return tx 153 } 154 155 func (tx *SystemDeleteTransaction) Freeze() (*SystemDeleteTransaction, error) { 156 return tx.FreezeWith(nil) 157 } 158 159 func (tx *SystemDeleteTransaction) FreezeWith(client *Client) (*SystemDeleteTransaction, error) { 160 _, err := tx.Transaction.freezeWith(client, tx) 161 return tx, err 162 } 163 164 // SetMaxTransactionFee sets the max transaction fee for this SystemDeleteTransaction. 165 func (tx *SystemDeleteTransaction) SetMaxTransactionFee(fee Hbar) *SystemDeleteTransaction { 166 tx.Transaction.SetMaxTransactionFee(fee) 167 return tx 168 } 169 170 // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received 171 func (tx *SystemDeleteTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *SystemDeleteTransaction { 172 tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID) 173 return tx 174 } 175 176 // SetTransactionMemo sets the memo for this SystemDeleteTransaction. 177 func (tx *SystemDeleteTransaction) SetTransactionMemo(memo string) *SystemDeleteTransaction { 178 tx.Transaction.SetTransactionMemo(memo) 179 return tx 180 } 181 182 // SetTransactionValidDuration sets the valid duration for this SystemDeleteTransaction. 183 func (tx *SystemDeleteTransaction) SetTransactionValidDuration(duration time.Duration) *SystemDeleteTransaction { 184 tx.Transaction.SetTransactionValidDuration(duration) 185 return tx 186 } 187 188 // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not 189 func (tx *SystemDeleteTransaction) ToBytes() ([]byte, error) { 190 bytes, err := tx.Transaction.toBytes(tx) 191 if err != nil { 192 return nil, err 193 } 194 return bytes, nil 195 } 196 197 // SetTransactionID sets the TransactionID for this SystemDeleteTransaction. 198 func (tx *SystemDeleteTransaction) SetTransactionID(transactionID TransactionID) *SystemDeleteTransaction { 199 tx.Transaction.SetTransactionID(transactionID) 200 return tx 201 } 202 203 // SetNodeAccountIDs sets the _Node AccountID for this SystemDeleteTransaction. 204 func (tx *SystemDeleteTransaction) SetNodeAccountIDs(nodeID []AccountID) *SystemDeleteTransaction { 205 tx.Transaction.SetNodeAccountIDs(nodeID) 206 return tx 207 } 208 209 // SetMaxRetry sets the max number of errors before execution will fail. 210 func (tx *SystemDeleteTransaction) SetMaxRetry(count int) *SystemDeleteTransaction { 211 tx.Transaction.SetMaxRetry(count) 212 return tx 213 } 214 215 // SetMaxBackoff The maximum amount of time to wait between retries. 216 // Every retry attempt will increase the wait time exponentially until it reaches this time. 217 func (tx *SystemDeleteTransaction) SetMaxBackoff(max time.Duration) *SystemDeleteTransaction { 218 tx.Transaction.SetMaxBackoff(max) 219 return tx 220 } 221 222 // SetMinBackoff sets the minimum amount of time to wait between retries. 223 func (tx *SystemDeleteTransaction) SetMinBackoff(min time.Duration) *SystemDeleteTransaction { 224 tx.Transaction.SetMinBackoff(min) 225 return tx 226 } 227 228 func (tx *SystemDeleteTransaction) SetLogLevel(level LogLevel) *SystemDeleteTransaction { 229 tx.Transaction.SetLogLevel(level) 230 return tx 231 } 232 233 func (tx *SystemDeleteTransaction) Execute(client *Client) (TransactionResponse, error) { 234 return tx.Transaction.execute(client, tx) 235 } 236 237 func (tx *SystemDeleteTransaction) Schedule() (*ScheduleCreateTransaction, error) { 238 return tx.Transaction.schedule(tx) 239 } 240 241 // ----------- Overridden functions ---------------- 242 243 func (tx *SystemDeleteTransaction) getName() string { 244 return "SystemDeleteTransaction" 245 } 246 247 func (tx *SystemDeleteTransaction) validateNetworkOnIDs(client *Client) error { 248 if client == nil || !client.autoValidateChecksums { 249 return nil 250 } 251 252 if tx.contractID != nil { 253 if err := tx.contractID.ValidateChecksum(client); err != nil { 254 return err 255 } 256 } 257 258 if tx.fileID != nil { 259 if err := tx.fileID.ValidateChecksum(client); err != nil { 260 return err 261 } 262 } 263 264 return nil 265 } 266 267 func (tx *SystemDeleteTransaction) build() *services.TransactionBody { 268 return &services.TransactionBody{ 269 TransactionFee: tx.transactionFee, 270 Memo: tx.Transaction.memo, 271 TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()), 272 TransactionID: tx.transactionID._ToProtobuf(), 273 Data: &services.TransactionBody_SystemDelete{ 274 SystemDelete: tx.buildProtoBody(), 275 }, 276 } 277 } 278 279 func (tx *SystemDeleteTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) { 280 return &services.SchedulableTransactionBody{ 281 TransactionFee: tx.transactionFee, 282 Memo: tx.Transaction.memo, 283 Data: &services.SchedulableTransactionBody_SystemDelete{ 284 SystemDelete: tx.buildProtoBody(), 285 }, 286 }, nil 287 } 288 289 func (tx *SystemDeleteTransaction) buildProtoBody() *services.SystemDeleteTransactionBody { 290 body := &services.SystemDeleteTransactionBody{} 291 292 if tx.expirationTime != nil { 293 body.ExpirationTime = &services.TimestampSeconds{ 294 Seconds: tx.expirationTime.Unix(), 295 } 296 } 297 298 if tx.contractID != nil { 299 body.Id = &services.SystemDeleteTransactionBody_ContractID{ 300 ContractID: tx.contractID._ToProtobuf(), 301 } 302 } 303 304 if tx.fileID != nil { 305 body.Id = &services.SystemDeleteTransactionBody_FileID{ 306 FileID: tx.fileID._ToProtobuf(), 307 } 308 } 309 310 return body 311 } 312 313 func (tx *SystemDeleteTransaction) getMethod(channel *_Channel) _Method { 314 if channel._GetContract() == nil { 315 return _Method{ 316 transaction: channel._GetFile().SystemDelete, 317 } 318 } 319 320 return _Method{ 321 transaction: channel._GetContract().SystemDelete, 322 } 323 } 324 325 func (tx *SystemDeleteTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) { 326 return tx.buildScheduled() 327 }