|
19 | 19 | package schedule |
20 | 20 |
|
21 | 21 | import ( |
| 22 | + "context" |
22 | 23 | "fmt" |
| 24 | + "strings" |
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" |
27 | 31 | "github.com/onflow/flowkit/v2/output" |
28 | 32 |
|
| 33 | + "github.com/onflow/flow-cli/common/branding" |
29 | 34 | "github.com/onflow/flow-cli/internal/command" |
30 | 35 | "github.com/onflow/flow-cli/internal/util" |
31 | 36 | ) |
@@ -71,54 +76,166 @@ func listRun( |
71 | 76 |
|
72 | 77 | accountInput := args[0] |
73 | 78 |
|
74 | | - // Resolve account address from input (could be address or account name) |
75 | 79 | address, err := util.ResolveAddressOrAccountNameForNetworks(accountInput, state, []string{"mainnet", "testnet", "emulator"}) |
76 | 80 | if err != nil { |
77 | 81 | return nil, fmt.Errorf("failed to resolve account: %w", err) |
78 | 82 | } |
79 | 83 |
|
80 | | - logger.Info(fmt.Sprintf("Network: %s", globalFlags.Network)) |
81 | | - logger.Info(fmt.Sprintf("Account: %s (%s)", accountInput, address.String())) |
82 | | - logger.Info("Retrieving scheduled transactions...") |
| 84 | + chainID, err := util.NetworkToChainID(globalFlags.Network) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + if chainID == flowsdk.Mainnet { |
| 90 | + return nil, fmt.Errorf("transaction scheduling is not yet supported on mainnet") |
| 91 | + } |
83 | 92 |
|
84 | | - // TODO: Implement list logic for scheduled transactions |
| 93 | + schedulerAddress, err := getContractAddress(FlowTransactionScheduler, chainID) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + schedulerUtilsAddress, err := getContractAddress(FlowTransactionSchedulerUtils, chainID) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + networkStr := branding.GrayStyle.Render(globalFlags.Network) |
| 104 | + accountStr := branding.PurpleStyle.Render(accountInput) |
| 105 | + addressStr := branding.GrayStyle.Render(address.String()) |
| 106 | + |
| 107 | + logger.Info("Listing scheduled transactions...") |
| 108 | + logger.Info("") |
| 109 | + logger.Info(fmt.Sprintf("🌐 Network: %s", networkStr)) |
| 110 | + logger.Info(fmt.Sprintf("📝 Account: %s (%s)", accountStr, addressStr)) |
| 111 | + |
| 112 | + script := fmt.Sprintf(`import FlowTransactionScheduler from %s |
| 113 | +import FlowTransactionSchedulerUtils from %s |
| 114 | +
|
| 115 | +access(all) fun main(managerAddress: Address): [FlowTransactionScheduler.TransactionData] { |
| 116 | + // Use the helper function to borrow the Manager |
| 117 | + let manager = FlowTransactionSchedulerUtils.borrowManager(at: managerAddress) |
| 118 | + ?? panic("Could not borrow Manager from account") |
| 119 | +
|
| 120 | + let transactionIds = manager.getTransactionIDs() |
| 121 | + var transactions: [FlowTransactionScheduler.TransactionData] = [] |
| 122 | +
|
| 123 | + // Get transaction data through the Manager instead of directly from FlowTransactionScheduler |
| 124 | + for id in transactionIds { |
| 125 | + if let txData = manager.getTransactionData(id) { |
| 126 | + transactions.append(txData) |
| 127 | + } |
| 128 | + } |
| 129 | +
|
| 130 | + return transactions |
| 131 | +}`, schedulerAddress, schedulerUtilsAddress) |
| 132 | + |
| 133 | + value, err := flow.ExecuteScript( |
| 134 | + context.Background(), |
| 135 | + flowkit.Script{ |
| 136 | + Code: []byte(script), |
| 137 | + Args: []cadence.Value{cadence.NewAddress(address)}, |
| 138 | + }, |
| 139 | + flowkit.LatestScriptQuery, |
| 140 | + ) |
| 141 | + if err != nil { |
| 142 | + return nil, fmt.Errorf("failed to execute script: %w", err) |
| 143 | + } |
| 144 | + |
| 145 | + transactions, err := parseTransactionList(value) |
| 146 | + if err != nil { |
| 147 | + return nil, fmt.Errorf("failed to parse transaction list: %w", err) |
| 148 | + } |
| 149 | + |
| 150 | + logger.Info("") |
| 151 | + if len(transactions) == 0 { |
| 152 | + warningIcon := branding.GrayStyle.Render("ℹ") |
| 153 | + warningMsg := branding.GrayStyle.Render("No scheduled transactions found") |
| 154 | + logger.Info(fmt.Sprintf("%s %s", warningIcon, warningMsg)) |
| 155 | + } else { |
| 156 | + successIcon := branding.GreenStyle.Render("✅") |
| 157 | + successMsg := branding.GreenStyle.Render(fmt.Sprintf("Found %d scheduled transaction(s)", len(transactions))) |
| 158 | + logger.Info(fmt.Sprintf("%s %s", successIcon, successMsg)) |
| 159 | + } |
85 | 160 |
|
86 | 161 | return &listResult{ |
87 | | - success: true, |
88 | | - message: "Scheduled transactions retrieved successfully", |
89 | | - transactions: []string{"0x1234567890abcdef", "0xfedcba0987654321"}, // Mock data for now |
| 162 | + transactions: transactions, |
90 | 163 | }, nil |
91 | 164 | } |
92 | 165 |
|
| 166 | +func parseTransactionList(value cadence.Value) ([]*TransactionData, error) { |
| 167 | + array, ok := value.(cadence.Array) |
| 168 | + if !ok { |
| 169 | + return nil, fmt.Errorf("expected array value, got %T", value) |
| 170 | + } |
| 171 | + |
| 172 | + var transactions []*TransactionData |
| 173 | + for _, item := range array.Values { |
| 174 | + optional := cadence.NewOptional(item) |
| 175 | + txData, err := ParseTransactionData(optional) |
| 176 | + if err != nil { |
| 177 | + return nil, fmt.Errorf("failed to parse transaction: %w", err) |
| 178 | + } |
| 179 | + if txData != nil { |
| 180 | + transactions = append(transactions, txData) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return transactions, nil |
| 185 | +} |
| 186 | + |
93 | 187 | type listResult struct { |
94 | | - success bool |
95 | | - message string |
96 | | - transactions []string |
| 188 | + transactions []*TransactionData |
97 | 189 | } |
98 | 190 |
|
99 | 191 | func (r *listResult) JSON() any { |
| 192 | + var txList []map[string]any |
| 193 | + for _, tx := range r.transactions { |
| 194 | + txList = append(txList, map[string]any{ |
| 195 | + "id": tx.ID, |
| 196 | + "priority": tx.Priority, |
| 197 | + "execution_effort": tx.ExecutionEffort, |
| 198 | + "status": tx.Status, |
| 199 | + "fees": tx.Fees, |
| 200 | + "scheduled_timestamp": tx.ScheduledTimestamp, |
| 201 | + "handler_type_identifier": tx.HandlerTypeIdentifier, |
| 202 | + "handler_address": tx.HandlerAddress, |
| 203 | + }) |
| 204 | + } |
100 | 205 | return map[string]any{ |
101 | | - "success": r.success, |
102 | | - "message": r.message, |
103 | | - "transactions": r.transactions, |
| 206 | + "transactions": txList, |
| 207 | + "count": len(r.transactions), |
104 | 208 | } |
105 | 209 | } |
106 | 210 |
|
107 | 211 | func (r *listResult) String() string { |
108 | 212 | if len(r.transactions) == 0 { |
109 | | - return "No scheduled transactions found" |
| 213 | + return "" |
110 | 214 | } |
111 | 215 |
|
112 | | - result := fmt.Sprintf("Found %d scheduled transaction(s):\n", len(r.transactions)) |
113 | | - for i, txID := range r.transactions { |
114 | | - result += fmt.Sprintf(" %d. %s\n", i+1, txID) |
| 216 | + var output strings.Builder |
| 217 | + |
| 218 | + // Display each transaction with details |
| 219 | + for i, tx := range r.transactions { |
| 220 | + if i > 0 { |
| 221 | + output.WriteString("\n") |
| 222 | + } |
| 223 | + |
| 224 | + // Transaction header line |
| 225 | + txLabel := branding.GrayStyle.Render("Transaction") |
| 226 | + txID := branding.PurpleStyle.Render(fmt.Sprintf("%d", tx.ID)) |
| 227 | + output.WriteString(fmt.Sprintf("%s %s\n", txLabel, txID)) |
| 228 | + |
| 229 | + // Transaction details using shared formatting |
| 230 | + output.WriteString(FormatTransactionDetails(tx)) |
115 | 231 | } |
116 | | - return result |
| 232 | + |
| 233 | + return output.String() |
117 | 234 | } |
118 | 235 |
|
119 | 236 | func (r *listResult) Oneliner() string { |
120 | 237 | if len(r.transactions) == 0 { |
121 | 238 | return "No scheduled transactions found" |
122 | 239 | } |
123 | | - return fmt.Sprintf("Found %d scheduled transactions", len(r.transactions)) |
| 240 | + return fmt.Sprintf("Found %d scheduled transaction(s)", len(r.transactions)) |
124 | 241 | } |
0 commit comments