|
19 | 19 | package schedule |
20 | 20 |
|
21 | 21 | import ( |
| 22 | + "context" |
22 | 23 | "fmt" |
23 | 24 |
|
| 25 | + flowsdk "github.com/onflow/flow-go-sdk" |
24 | 26 | "github.com/spf13/cobra" |
25 | 27 |
|
26 | 28 | "github.com/onflow/flowkit/v2" |
| 29 | + "github.com/onflow/flowkit/v2/accounts" |
27 | 30 | "github.com/onflow/flowkit/v2/output" |
| 31 | + "github.com/onflow/flowkit/v2/transactions" |
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 | ) |
@@ -74,35 +79,104 @@ func setupRun( |
74 | 79 |
|
75 | 80 | address := signer.Address |
76 | 81 |
|
77 | | - // Log network and account information |
78 | | - logger.Info(fmt.Sprintf("Network: %s", globalFlags.Network)) |
79 | | - logger.Info(fmt.Sprintf("Signer: %s (%s)", setupFlags.Signer, address.String())) |
80 | | - logger.Info("Setting up Flow Transaction Scheduler Manager resource...") |
| 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(FlowTransactionSchedulerUtils, chainID) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + // Log setup information with styled output |
| 98 | + networkStr := branding.GrayStyle.Render(globalFlags.Network) |
| 99 | + addressStr := branding.PurpleStyle.Render(address.HexWithPrefix()) |
| 100 | + signerStr := branding.GrayStyle.Render(setupFlags.Signer) |
| 101 | + |
| 102 | + logger.Info(fmt.Sprintf("🌐 Network: %s", networkStr)) |
| 103 | + logger.Info(fmt.Sprintf("📝 Signer: %s (%s)", signerStr, addressStr)) |
| 104 | + logger.Info("") |
| 105 | + logger.Info("⚡ Setting up Transaction Scheduler Manager...") |
| 106 | + |
| 107 | + // Transaction checks if manager exists and only creates it if needed |
| 108 | + setupTx := fmt.Sprintf(`import FlowTransactionSchedulerUtils from %s |
| 109 | +
|
| 110 | +transaction() { |
| 111 | + prepare(signer: auth(BorrowValue, SaveValue) &Account) { |
| 112 | + // Check if Manager already exists |
| 113 | + if signer.storage.borrow<&{FlowTransactionSchedulerUtils.Manager}>(from: FlowTransactionSchedulerUtils.managerStoragePath) == nil { |
| 114 | + // Create and save Manager |
| 115 | + signer.storage.save( |
| 116 | + <-FlowTransactionSchedulerUtils.createManager(), |
| 117 | + to: FlowTransactionSchedulerUtils.managerStoragePath |
| 118 | + ) |
| 119 | + } |
| 120 | + } |
| 121 | +}`, contractAddress) |
| 122 | + |
| 123 | + _, txResult, err := flow.SendTransaction( |
| 124 | + context.Background(), |
| 125 | + transactions.AccountRoles{ |
| 126 | + Proposer: *signer, |
| 127 | + Authorizers: []accounts.Account{*signer}, |
| 128 | + Payer: *signer, |
| 129 | + }, |
| 130 | + flowkit.Script{ |
| 131 | + Code: []byte(setupTx), |
| 132 | + Args: nil, |
| 133 | + }, |
| 134 | + 1000, |
| 135 | + ) |
| 136 | + |
| 137 | + if err != nil { |
| 138 | + return nil, fmt.Errorf("failed to setup transaction scheduler: %w", err) |
| 139 | + } |
| 140 | + |
| 141 | + if txResult.Error != nil { |
| 142 | + return nil, fmt.Errorf("setup transaction failed: %s", txResult.Error.Error()) |
| 143 | + } |
| 144 | + |
| 145 | + // Log success with styled output |
| 146 | + logger.Info("") |
| 147 | + successIcon := branding.GreenStyle.Render("✅") |
| 148 | + successMsg := branding.GreenStyle.Render("Transaction Scheduler Manager is setup") |
| 149 | + logger.Info(fmt.Sprintf("%s %s", successIcon, successMsg)) |
81 | 150 |
|
82 | | - // TODO: Implement setup logic for Transaction Scheduler Manager resource |
| 151 | + logger.Info("") |
| 152 | + noteIcon := branding.GrayStyle.Render("📝") |
| 153 | + noteText := branding.GrayStyle.Render("Note: If the manager already existed, no changes were made") |
| 154 | + logger.Info(fmt.Sprintf("%s %s", noteIcon, noteText)) |
83 | 155 |
|
84 | 156 | return &setupResult{ |
85 | | - success: true, |
86 | | - message: "Transaction Scheduler Manager resource created successfully", |
| 157 | + success: true, |
| 158 | + transactionID: txResult.TransactionID.String(), |
87 | 159 | }, nil |
88 | 160 | } |
89 | 161 |
|
90 | 162 | type setupResult struct { |
91 | | - success bool |
92 | | - message string |
| 163 | + success bool |
| 164 | + transactionID string |
93 | 165 | } |
94 | 166 |
|
95 | 167 | func (r *setupResult) JSON() any { |
96 | 168 | return map[string]any{ |
97 | | - "success": r.success, |
98 | | - "message": r.message, |
| 169 | + "success": r.success, |
| 170 | + "transactionID": r.transactionID, |
| 171 | + "message": "Transaction Scheduler Manager is ready", |
99 | 172 | } |
100 | 173 | } |
101 | 174 |
|
102 | 175 | func (r *setupResult) String() string { |
103 | | - return r.message |
| 176 | + // Return empty string since we already logged everything in the command |
| 177 | + return "" |
104 | 178 | } |
105 | 179 |
|
106 | 180 | func (r *setupResult) Oneliner() string { |
107 | | - return r.message |
| 181 | + return "Transaction Scheduler Manager is ready" |
108 | 182 | } |
0 commit comments