|
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" |
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" |
| 35 | + "github.com/onflow/flow-cli/internal/util" |
30 | 36 | ) |
31 | 37 |
|
32 | 38 | type flagsGet struct{} |
@@ -65,51 +71,139 @@ func getRun( |
65 | 71 | return nil, fmt.Errorf("transaction ID is required as an argument") |
66 | 72 | } |
67 | 73 |
|
68 | | - transactionID := args[0] |
| 74 | + transactionIDStr := args[0] |
69 | 75 |
|
70 | | - logger.Info(fmt.Sprintf("Network: %s", globalFlags.Network)) |
71 | | - logger.Info(fmt.Sprintf("Transaction ID: %s", transactionID)) |
72 | | - logger.Info("Retrieving scheduled transaction details...") |
| 76 | + // Parse transaction ID as UInt64 |
| 77 | + transactionID, err := strconv.ParseUint(transactionIDStr, 10, 64) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("invalid transaction ID: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + chainID, err := util.NetworkToChainID(globalFlags.Network) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + // Check if network is supported |
| 88 | + if chainID == flowsdk.Mainnet { |
| 89 | + return nil, fmt.Errorf("transaction scheduling is not yet supported on mainnet") |
| 90 | + } |
| 91 | + |
| 92 | + contractAddress, err := getContractAddress(FlowTransactionScheduler, chainID) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + networkStr := branding.GrayStyle.Render(globalFlags.Network) |
| 98 | + txIDStr := branding.PurpleStyle.Render(transactionIDStr) |
| 99 | + |
| 100 | + logger.Info("Getting scheduled transaction details") |
| 101 | + logger.Info("") |
| 102 | + logger.Info(fmt.Sprintf("🌐 Network: %s", networkStr)) |
| 103 | + logger.Info(fmt.Sprintf("🔍 Transaction ID: %s", txIDStr)) |
| 104 | + |
| 105 | + script := fmt.Sprintf(`import FlowTransactionScheduler from %s |
| 106 | +
|
| 107 | +access(all) fun main(transactionID: UInt64): FlowTransactionScheduler.TransactionData? { |
| 108 | + // Get the transaction data directly from the FlowTransactionScheduler contract |
| 109 | + return FlowTransactionScheduler.getTransactionData(id: transactionID) |
| 110 | +}`, contractAddress) |
| 111 | + |
| 112 | + value, err := flow.ExecuteScript( |
| 113 | + context.Background(), |
| 114 | + flowkit.Script{ |
| 115 | + Code: []byte(script), |
| 116 | + Args: []cadence.Value{cadence.NewUInt64(transactionID)}, |
| 117 | + }, |
| 118 | + flowkit.LatestScriptQuery, |
| 119 | + ) |
| 120 | + if err != nil { |
| 121 | + return nil, fmt.Errorf("failed to execute script: %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + txData, err := ParseTransactionData(value) |
| 125 | + if err != nil { |
| 126 | + return nil, fmt.Errorf("failed to parse transaction data: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + if txData == nil { |
| 130 | + logger.Info("") |
| 131 | + return nil, fmt.Errorf("scheduled transaction not found") |
| 132 | + } |
73 | 133 |
|
74 | | - // TODO: Implement get logic for scheduled transaction |
| 134 | + // Log success |
| 135 | + logger.Info("") |
| 136 | + successIcon := branding.GreenStyle.Render("✅") |
| 137 | + successMsg := branding.GreenStyle.Render("Transaction data retrieved successfully") |
| 138 | + logger.Info(fmt.Sprintf("%s %s", successIcon, successMsg)) |
75 | 139 |
|
76 | | - return &getResult{ |
77 | | - success: true, |
78 | | - message: "Scheduled transaction details retrieved successfully", |
79 | | - transactionID: transactionID, |
80 | | - status: "Scheduled", |
81 | | - scheduledAt: "2024-01-01T00:00:00Z", |
82 | | - executeAt: "2024-01-01T12:00:00Z", |
83 | | - }, nil |
| 140 | + return &getResult{data: txData}, nil |
84 | 141 | } |
85 | 142 |
|
86 | 143 | type getResult struct { |
87 | | - success bool |
88 | | - message string |
89 | | - transactionID string |
90 | | - status string |
91 | | - scheduledAt string |
92 | | - executeAt string |
| 144 | + data *TransactionData |
93 | 145 | } |
94 | 146 |
|
95 | 147 | func (r *getResult) JSON() any { |
96 | 148 | return map[string]any{ |
97 | | - "success": r.success, |
98 | | - "message": r.message, |
99 | | - "transaction_id": r.transactionID, |
100 | | - "status": r.status, |
101 | | - "scheduled_at": r.scheduledAt, |
102 | | - "execute_at": r.executeAt, |
| 149 | + "id": r.data.ID, |
| 150 | + "priority": r.data.Priority, |
| 151 | + "execution_effort": r.data.ExecutionEffort, |
| 152 | + "status": r.data.Status, |
| 153 | + "fees": r.data.Fees, |
| 154 | + "scheduled_timestamp": r.data.ScheduledTimestamp, |
| 155 | + "handler_type_identifier": r.data.HandlerTypeIdentifier, |
| 156 | + "handler_address": r.data.HandlerAddress, |
103 | 157 | } |
104 | 158 | } |
105 | 159 |
|
106 | 160 | func (r *getResult) String() string { |
107 | | - return fmt.Sprintf(`Transaction ID: %s |
108 | | -Status: %s |
109 | | -Scheduled At: %s |
110 | | -Execute At: %s`, r.transactionID, r.status, r.scheduledAt, r.executeAt) |
| 161 | + var output string |
| 162 | + |
| 163 | + // ID |
| 164 | + idLabel := branding.GrayStyle.Render(" ID:") |
| 165 | + idValue := branding.PurpleStyle.Render(fmt.Sprintf("%d", r.data.ID)) |
| 166 | + output += fmt.Sprintf("%s %s\n", idLabel, idValue) |
| 167 | + |
| 168 | + // Status |
| 169 | + statusLabel := branding.GrayStyle.Render(" Status:") |
| 170 | + statusValue := branding.GreenStyle.Render(GetStatusString(r.data.Status)) |
| 171 | + output += fmt.Sprintf("%s %s\n", statusLabel, statusValue) |
| 172 | + |
| 173 | + // Priority |
| 174 | + priorityLabel := branding.GrayStyle.Render(" Priority:") |
| 175 | + priorityValue := branding.PurpleStyle.Render(GetPriorityString(r.data.Priority)) |
| 176 | + output += fmt.Sprintf("%s %s\n", priorityLabel, priorityValue) |
| 177 | + |
| 178 | + // Execution Effort |
| 179 | + effortLabel := branding.GrayStyle.Render(" Execution Effort:") |
| 180 | + effortValue := branding.PurpleStyle.Render(fmt.Sprintf("%d", r.data.ExecutionEffort)) |
| 181 | + output += fmt.Sprintf("%s %s\n", effortLabel, effortValue) |
| 182 | + |
| 183 | + // Fees |
| 184 | + feesLabel := branding.GrayStyle.Render(" Fees:") |
| 185 | + feesValue := branding.PurpleStyle.Render(fmt.Sprintf("%s FLOW", r.data.Fees)) |
| 186 | + output += fmt.Sprintf("%s %s\n", feesLabel, feesValue) |
| 187 | + |
| 188 | + // Scheduled Timestamp |
| 189 | + timestampLabel := branding.GrayStyle.Render(" Scheduled Timestamp:") |
| 190 | + timestampValue := branding.PurpleStyle.Render(r.data.ScheduledTimestamp) |
| 191 | + output += fmt.Sprintf("%s %s\n", timestampLabel, timestampValue) |
| 192 | + |
| 193 | + // Handler Type |
| 194 | + handlerTypeLabel := branding.GrayStyle.Render(" Handler Type:") |
| 195 | + handlerTypeValue := branding.PurpleStyle.Render(r.data.HandlerTypeIdentifier) |
| 196 | + output += fmt.Sprintf("%s %s\n", handlerTypeLabel, handlerTypeValue) |
| 197 | + |
| 198 | + // Handler Address |
| 199 | + handlerAddrLabel := branding.GrayStyle.Render(" Handler Address:") |
| 200 | + handlerAddrValue := branding.PurpleStyle.Render(r.data.HandlerAddress) |
| 201 | + output += fmt.Sprintf("%s %s\n", handlerAddrLabel, handlerAddrValue) |
| 202 | + |
| 203 | + return output |
111 | 204 | } |
112 | 205 |
|
113 | 206 | func (r *getResult) Oneliner() string { |
114 | | - return fmt.Sprintf("Transaction %s - Status: %s", r.transactionID, r.status) |
| 207 | + statusStr := GetStatusString(r.data.Status) |
| 208 | + return fmt.Sprintf("Transaction %d - Status: %s", r.data.ID, statusStr) |
115 | 209 | } |
0 commit comments