|
19 | 19 | package schedule |
20 | 20 |
|
21 | 21 | import ( |
| 22 | + "context" |
22 | 23 | "fmt" |
| 24 | + "strconv" |
23 | 25 |
|
| 26 | + "github.com/onflow/cadence" |
| 27 | + flowsdk "github.com/onflow/flow-go-sdk" |
24 | 28 | "github.com/spf13/cobra" |
25 | 29 |
|
26 | 30 | "github.com/onflow/flowkit/v2" |
| 31 | + "github.com/onflow/flowkit/v2/accounts" |
27 | 32 | "github.com/onflow/flowkit/v2/output" |
| 33 | + "github.com/onflow/flowkit/v2/transactions" |
28 | 34 |
|
| 35 | + "github.com/onflow/flow-cli/common/branding" |
29 | 36 | "github.com/onflow/flow-cli/internal/command" |
30 | 37 | "github.com/onflow/flow-cli/internal/util" |
31 | 38 | ) |
@@ -71,42 +78,141 @@ func cancelRun( |
71 | 78 | return nil, fmt.Errorf("transaction ID is required as an argument") |
72 | 79 | } |
73 | 80 |
|
74 | | - transactionID := args[0] |
| 81 | + transactionIDStr := args[0] |
| 82 | + |
| 83 | + // Parse transaction ID as UInt64 |
| 84 | + transactionID, err := strconv.ParseUint(transactionIDStr, 10, 64) |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("invalid transaction ID: %w", err) |
| 87 | + } |
75 | 88 |
|
76 | 89 | signer, err := util.GetSignerAccount(state, cancelFlags.Signer) |
77 | 90 | if err != nil { |
78 | 91 | return nil, err |
79 | 92 | } |
80 | 93 |
|
81 | | - logger.Info(fmt.Sprintf("Network: %s", globalFlags.Network)) |
82 | | - logger.Info(fmt.Sprintf("Signer: %s (%s)", cancelFlags.Signer, signer.Address.String())) |
83 | | - logger.Info(fmt.Sprintf("Transaction ID: %s", transactionID)) |
| 94 | + chainID, err := util.NetworkToChainID(globalFlags.Network) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + if chainID == flowsdk.Mainnet { |
| 100 | + return nil, fmt.Errorf("transaction scheduling is not yet supported on mainnet") |
| 101 | + } |
| 102 | + |
| 103 | + schedulerUtilsAddress, err := getContractAddress(FlowTransactionSchedulerUtils, chainID) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + flowTokenAddress, err := getContractAddress(FlowToken, chainID) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + fungibleTokenAddress, err := getContractAddress(FungibleToken, chainID) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + networkStr := branding.GrayStyle.Render(globalFlags.Network) |
| 119 | + addressStr := branding.PurpleStyle.Render(signer.Address.HexWithPrefix()) |
| 120 | + signerStr := branding.GrayStyle.Render(cancelFlags.Signer) |
| 121 | + txIDStr := branding.PurpleStyle.Render(transactionIDStr) |
| 122 | + |
84 | 123 | logger.Info("Canceling scheduled transaction...") |
| 124 | + logger.Info("") |
| 125 | + logger.Info(fmt.Sprintf("🌐 Network: %s", networkStr)) |
| 126 | + logger.Info(fmt.Sprintf("📝 Signer: %s (%s)", signerStr, addressStr)) |
| 127 | + logger.Info(fmt.Sprintf("🔍 Transaction ID: %s", txIDStr)) |
| 128 | + logger.Info("") |
| 129 | + |
| 130 | + // Build transaction code |
| 131 | + cancelTx := fmt.Sprintf(`import FlowTransactionSchedulerUtils from %s |
| 132 | +import FlowToken from %s |
| 133 | +import FungibleToken from %s |
| 134 | +
|
| 135 | +transaction(transactionId: UInt64) { |
| 136 | + let manager: auth(FlowTransactionSchedulerUtils.Owner) &{FlowTransactionSchedulerUtils.Manager} |
| 137 | + let tokenReceiver: &{FungibleToken.Receiver} |
| 138 | +
|
| 139 | + prepare(signer: auth(BorrowValue) &Account) { |
| 140 | + // 1. Borrow Manager reference |
| 141 | + self.manager = signer.storage.borrow<auth(FlowTransactionSchedulerUtils.Owner) &{FlowTransactionSchedulerUtils.Manager}>( |
| 142 | + from: FlowTransactionSchedulerUtils.managerStoragePath |
| 143 | + ) ?? panic("Could not borrow Manager. Please ensure you have a Manager set up.") |
| 144 | +
|
| 145 | + // Verify transaction exists in manager |
| 146 | + assert( |
| 147 | + self.manager.getTransactionIDs().contains(transactionId), |
| 148 | + message: "Transaction with ID ".concat(transactionId.toString()).concat(" not found in manager") |
| 149 | + ) |
| 150 | +
|
| 151 | + // 2. Get FlowToken receiver to deposit refunds |
| 152 | + self.tokenReceiver = signer.capabilities.get<&{FungibleToken.Receiver}>(/public/flowTokenReceiver) |
| 153 | + .borrow() |
| 154 | + ?? panic("Could not borrow FlowToken receiver") |
| 155 | + } |
| 156 | +
|
| 157 | + execute { |
| 158 | + // Cancel the transaction and receive refunded fees |
| 159 | + let refundVault <- self.manager.cancel(id: transactionId) |
| 160 | +
|
| 161 | + // Deposit refunded fees back to the account |
| 162 | + self.tokenReceiver.deposit(from: <-refundVault) |
| 163 | + } |
| 164 | +}`, schedulerUtilsAddress, flowTokenAddress, fungibleTokenAddress) |
| 165 | + |
| 166 | + _, txResult, err := flow.SendTransaction( |
| 167 | + context.Background(), |
| 168 | + transactions.AccountRoles{ |
| 169 | + Proposer: *signer, |
| 170 | + Authorizers: []accounts.Account{*signer}, |
| 171 | + Payer: *signer, |
| 172 | + }, |
| 173 | + flowkit.Script{ |
| 174 | + Code: []byte(cancelTx), |
| 175 | + Args: []cadence.Value{cadence.NewUInt64(transactionID)}, |
| 176 | + }, |
| 177 | + 1000, |
| 178 | + ) |
| 179 | + |
| 180 | + if err != nil { |
| 181 | + return nil, fmt.Errorf("failed to cancel scheduled transaction: %w", err) |
| 182 | + } |
| 183 | + |
| 184 | + if txResult.Error != nil { |
| 185 | + return nil, fmt.Errorf("cancel transaction failed: %s", txResult.Error.Error()) |
| 186 | + } |
85 | 187 |
|
86 | | - // TODO: Implement cancel logic for scheduled transaction |
| 188 | + logger.Info("") |
| 189 | + successIcon := branding.GreenStyle.Render("✅") |
| 190 | + successMsg := branding.GreenStyle.Render("Scheduled transaction canceled successfully") |
| 191 | + logger.Info(fmt.Sprintf("%s %s", successIcon, successMsg)) |
87 | 192 |
|
88 | 193 | return &cancelResult{ |
89 | | - success: true, |
90 | | - message: fmt.Sprintf("Scheduled transaction %s canceled successfully", transactionID), |
| 194 | + success: true, |
| 195 | + transactionID: transactionIDStr, |
91 | 196 | }, nil |
92 | 197 | } |
93 | 198 |
|
94 | 199 | type cancelResult struct { |
95 | | - success bool |
96 | | - message string |
| 200 | + success bool |
| 201 | + transactionID string |
97 | 202 | } |
98 | 203 |
|
99 | 204 | func (r *cancelResult) JSON() any { |
100 | 205 | return map[string]any{ |
101 | | - "success": r.success, |
102 | | - "message": r.message, |
| 206 | + "success": r.success, |
| 207 | + "transactionID": r.transactionID, |
| 208 | + "message": "Scheduled transaction canceled successfully", |
103 | 209 | } |
104 | 210 | } |
105 | 211 |
|
106 | 212 | func (r *cancelResult) String() string { |
107 | | - return r.message |
| 213 | + return "" |
108 | 214 | } |
109 | 215 |
|
110 | 216 | func (r *cancelResult) Oneliner() string { |
111 | | - return r.message |
| 217 | + return fmt.Sprintf("Scheduled transaction %s canceled successfully", r.transactionID) |
112 | 218 | } |
0 commit comments