|
| 1 | +package methods |
| 2 | + |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "strconv" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/hiero-ledger/hiero-sdk-go/tck/param" |
| 13 | + "github.com/hiero-ledger/hiero-sdk-go/tck/response" |
| 14 | + "github.com/hiero-ledger/hiero-sdk-go/tck/utils" |
| 15 | + hiero "github.com/hiero-ledger/hiero-sdk-go/v2/sdk" |
| 16 | +) |
| 17 | + |
| 18 | +type ScheduleService struct { |
| 19 | + sdkService *SDKService |
| 20 | +} |
| 21 | + |
| 22 | +func (s *ScheduleService) SetSdkService(service *SDKService) { |
| 23 | + s.sdkService = service |
| 24 | +} |
| 25 | + |
| 26 | +// CreateSchedule jRPC method for createSchedule |
| 27 | +func (s *ScheduleService) CreateSchedule(_ context.Context, params param.ScheduleCreateParams) (*response.ScheduleResponse, error) { |
| 28 | + transaction := hiero.NewScheduleCreateTransaction().SetGrpcDeadline(&threeSecondsDuration) |
| 29 | + |
| 30 | + if params.ScheduledTransaction != nil { |
| 31 | + scheduledTx, err := s.buildScheduledTransaction(params.ScheduledTransaction) |
| 32 | + if err != nil { |
| 33 | + return nil, fmt.Errorf("failed to build scheduled transaction: %w", err) |
| 34 | + } |
| 35 | + _, err = transaction.SetScheduledTransaction(scheduledTx) |
| 36 | + if err != nil { |
| 37 | + return nil, fmt.Errorf("failed to set scheduled transaction: %w", err) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if params.Memo != nil { |
| 42 | + transaction.SetScheduleMemo(*params.Memo) |
| 43 | + } |
| 44 | + |
| 45 | + if err := utils.SetKeyIfPresent(params.AdminKey, transaction.SetAdminKey); err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + if params.PayerAccountId != nil { |
| 50 | + payerAccountID, err := hiero.AccountIDFromString(*params.PayerAccountId) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("failed to parse payer account ID: %w", err) |
| 53 | + } |
| 54 | + transaction.SetPayerAccountID(payerAccountID) |
| 55 | + } |
| 56 | + |
| 57 | + if params.ExpirationTime != nil { |
| 58 | + expirationTime, err := strconv.ParseInt(*params.ExpirationTime, 10, 64) |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("failed to parse expiration time: %w", err) |
| 61 | + } |
| 62 | + transaction.SetExpirationTime(time.Unix(expirationTime, 0)) |
| 63 | + } |
| 64 | + |
| 65 | + if params.WaitForExpiry != nil { |
| 66 | + transaction.SetWaitForExpiry(*params.WaitForExpiry) |
| 67 | + } |
| 68 | + |
| 69 | + if params.CommonTransactionParams != nil { |
| 70 | + err := params.CommonTransactionParams.FillOutTransaction(transaction, s.sdkService.Client) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + txResponse, err := transaction.Execute(s.sdkService.Client) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + receipt, err := txResponse.SetValidateStatus(true).GetReceipt(s.sdkService.Client) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + var scheduleId string |
| 86 | + if receipt.Status == hiero.StatusSuccess { |
| 87 | + scheduleId = receipt.ScheduleID.String() |
| 88 | + } |
| 89 | + |
| 90 | + fmt.Println(receipt.ScheduledTransactionID) |
| 91 | + return &response.ScheduleResponse{ |
| 92 | + ScheduleId: scheduleId, |
| 93 | + TransactionId: receipt.ScheduledTransactionID.String(), |
| 94 | + Status: receipt.Status.String(), |
| 95 | + }, nil |
| 96 | +} |
| 97 | + |
| 98 | +// SignSchedule jRPC method for signSchedule |
| 99 | +func (s *ScheduleService) SignSchedule(_ context.Context, params param.ScheduleSignParams) (*response.ScheduleResponse, error) { |
| 100 | + transaction := hiero.NewScheduleSignTransaction().SetGrpcDeadline(&threeSecondsDuration) |
| 101 | + |
| 102 | + if params.ScheduleId != nil { |
| 103 | + scheduleID, err := hiero.ScheduleIDFromString(*params.ScheduleId) |
| 104 | + if err != nil { |
| 105 | + return nil, fmt.Errorf("failed to parse schedule ID: %w", err) |
| 106 | + } |
| 107 | + transaction.SetScheduleID(scheduleID) |
| 108 | + } |
| 109 | + |
| 110 | + if params.CommonTransactionParams != nil { |
| 111 | + err := params.CommonTransactionParams.FillOutTransaction(transaction, s.sdkService.Client) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + txResponse, err := transaction.Execute(s.sdkService.Client) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + receipt, err := txResponse.SetValidateStatus(true).GetReceipt(s.sdkService.Client) |
| 122 | + if err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + |
| 126 | + return &response.ScheduleResponse{ |
| 127 | + Status: receipt.Status.String(), |
| 128 | + }, nil |
| 129 | +} |
| 130 | + |
| 131 | +// buildScheduledTransaction creates the appropriate transaction based on method name |
| 132 | +func (s *ScheduleService) buildScheduledTransaction(scheduledTx *param.ScheduledTransaction) (hiero.TransactionInterface, error) { |
| 133 | + switch scheduledTx.Method { |
| 134 | + case "transferCrypto": |
| 135 | + var params param.TransferCryptoParams |
| 136 | + if err := json.Unmarshal(scheduledTx.Params, ¶ms); err != nil { |
| 137 | + return nil, fmt.Errorf("failed to unmarshal transferCrypto params: %w", err) |
| 138 | + } |
| 139 | + accountService := &AccountService{sdkService: s.sdkService} |
| 140 | + return accountService.buildTransferCrypto(params) |
| 141 | + |
| 142 | + case "approveAllowance": |
| 143 | + var params param.AccountAllowanceApproveParams |
| 144 | + if err := json.Unmarshal(scheduledTx.Params, ¶ms); err != nil { |
| 145 | + return nil, fmt.Errorf("failed to unmarshal approveAllowance params: %w", err) |
| 146 | + } |
| 147 | + accountService := &AccountService{sdkService: s.sdkService} |
| 148 | + return accountService.buildApproveAllowance(params) |
| 149 | + |
| 150 | + case "mintToken": |
| 151 | + var params param.MintTokenParams |
| 152 | + if err := json.Unmarshal(scheduledTx.Params, ¶ms); err != nil { |
| 153 | + return nil, fmt.Errorf("failed to unmarshal mintToken params: %w", err) |
| 154 | + } |
| 155 | + tokenService := &TokenService{sdkService: s.sdkService} |
| 156 | + return tokenService.buildMintToken(params) |
| 157 | + |
| 158 | + case "burnToken": |
| 159 | + var params param.BurnTokenParams |
| 160 | + if err := json.Unmarshal(scheduledTx.Params, ¶ms); err != nil { |
| 161 | + return nil, fmt.Errorf("failed to unmarshal burnToken params: %w", err) |
| 162 | + } |
| 163 | + tokenService := &TokenService{sdkService: s.sdkService} |
| 164 | + return tokenService.buildBurnToken(params) |
| 165 | + |
| 166 | + case "submitMessage": |
| 167 | + var params param.SubmitTopicMessageParams |
| 168 | + if err := json.Unmarshal(scheduledTx.Params, ¶ms); err != nil { |
| 169 | + return nil, fmt.Errorf("failed to unmarshal submitMessage params: %w", err) |
| 170 | + } |
| 171 | + topicService := &TopicService{sdkService: s.sdkService} |
| 172 | + return topicService.buildSubmitTopicMessage(params) |
| 173 | + |
| 174 | + case "createTopic": |
| 175 | + var params param.CreateTopicParams |
| 176 | + if err := json.Unmarshal(scheduledTx.Params, ¶ms); err != nil { |
| 177 | + return nil, fmt.Errorf("failed to unmarshal createTopic params: %w", err) |
| 178 | + } |
| 179 | + topicService := &TopicService{sdkService: s.sdkService} |
| 180 | + return topicService.buildCreateTopic(params) |
| 181 | + |
| 182 | + case "createAccount": |
| 183 | + var params param.CreateAccountParams |
| 184 | + if err := json.Unmarshal(scheduledTx.Params, ¶ms); err != nil { |
| 185 | + return nil, fmt.Errorf("failed to unmarshal createAccount params: %w", err) |
| 186 | + } |
| 187 | + accountService := &AccountService{sdkService: s.sdkService} |
| 188 | + return accountService.buildCreateAccount(params) |
| 189 | + |
| 190 | + default: |
| 191 | + return nil, fmt.Errorf("unsupported scheduled transaction method: %s (only transferCrypto, approveAllowance, mintToken, burnToken, submitMessage, createTopic, and createAccount are supported)", scheduledTx.Method) |
| 192 | + } |
| 193 | +} |
0 commit comments