|
19 | 19 | package utils |
20 | 20 |
|
21 | 21 | import ( |
| 22 | + "archive/zip" |
22 | 23 | "encoding/json" |
23 | 24 | "net/http" |
24 | 25 | "slices" |
25 | 26 | "strconv" |
26 | 27 | "strings" |
27 | 28 |
|
28 | 29 | "github.com/gorilla/mux" |
| 30 | + "github.com/onflow/cadence/common" |
29 | 31 | "github.com/onflow/cadence/runtime" |
30 | 32 | flowgo "github.com/onflow/flow-go/model/flow" |
31 | 33 |
|
@@ -72,6 +74,8 @@ func NewEmulatorAPIServer(emulator emulator.Emulator, adapter *adapters.AccessAd |
72 | 74 |
|
73 | 75 | router.HandleFunc("/emulator/computationReport", r.ComputationReport).Methods("GET") |
74 | 76 |
|
| 77 | + router.HandleFunc("/emulator/allContracts", r.AllContractsZip).Methods("GET") |
| 78 | + |
75 | 79 | return r |
76 | 80 | } |
77 | 81 |
|
@@ -327,3 +331,43 @@ func (m EmulatorAPIServer) Logs(w http.ResponseWriter, r *http.Request) { |
327 | 331 | return |
328 | 332 | } |
329 | 333 | } |
| 334 | + |
| 335 | +func (m EmulatorAPIServer) AllContractsZip(w http.ResponseWriter, r *http.Request) { |
| 336 | + w.Header().Set("Content-Disposition", "attachment; filename=contracts.zip") |
| 337 | + w.Header().Set("Content-Type", "application/zip") |
| 338 | + w.WriteHeader(http.StatusOK) |
| 339 | + |
| 340 | + zipW := zip.NewWriter(w) |
| 341 | + defer func() { |
| 342 | + err := zipW.Close() |
| 343 | + if err != nil { |
| 344 | + w.WriteHeader(http.StatusInternalServerError) |
| 345 | + } |
| 346 | + }() |
| 347 | + |
| 348 | + for accountIndex := 1; ; accountIndex++ { |
| 349 | + account, err := m.emulator.GetAccountByIndex(uint(accountIndex)) |
| 350 | + if err != nil { |
| 351 | + break |
| 352 | + } |
| 353 | + |
| 354 | + for name, code := range account.Contracts { |
| 355 | + location := common.AddressLocation{ |
| 356 | + Address: common.Address(account.Address), |
| 357 | + Name: name, |
| 358 | + } |
| 359 | + |
| 360 | + f, err := zipW.Create(location.ID() + cadenceFileSuffix) |
| 361 | + if err != nil { |
| 362 | + w.WriteHeader(http.StatusInternalServerError) |
| 363 | + return |
| 364 | + } |
| 365 | + |
| 366 | + _, err = f.Write(code) |
| 367 | + if err != nil { |
| 368 | + w.WriteHeader(http.StatusInternalServerError) |
| 369 | + return |
| 370 | + } |
| 371 | + } |
| 372 | + } |
| 373 | +} |
0 commit comments