|
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 |
|
27 | 28 | "github.com/gorilla/mux" |
| 29 | + "github.com/onflow/cadence/common" |
28 | 30 | "github.com/onflow/cadence/runtime" |
29 | 31 | flowgo "github.com/onflow/flow-go/model/flow" |
30 | 32 |
|
@@ -71,6 +73,8 @@ func NewEmulatorAPIServer(emulator emulator.Emulator, adapter *adapters.AccessAd |
71 | 73 |
|
72 | 74 | router.HandleFunc("/emulator/computationReport", r.ComputationReport).Methods("GET") |
73 | 75 |
|
| 76 | + router.HandleFunc("/emulator/all-contracts", r.AllContractsZip).Methods("GET") |
| 77 | + |
74 | 78 | return r |
75 | 79 | } |
76 | 80 |
|
@@ -313,3 +317,41 @@ func (m EmulatorAPIServer) Logs(w http.ResponseWriter, r *http.Request) { |
313 | 317 | return |
314 | 318 | } |
315 | 319 | } |
| 320 | + |
| 321 | +const cadenceFileSuffix = ".cdc" |
| 322 | + |
| 323 | +func (m EmulatorAPIServer) AllContractsZip(w http.ResponseWriter, r *http.Request) { |
| 324 | + w.Header().Set("Content-Disposition", "attachment; filename=contracts.zip") |
| 325 | + w.Header().Set("Content-Type", "application/zip") |
| 326 | + w.WriteHeader(http.StatusOK) |
| 327 | + |
| 328 | + zipW := zip.NewWriter(w) |
| 329 | + defer zipW.Close() |
| 330 | + |
| 331 | + for accountIndex := 1; ; accountIndex++ { |
| 332 | + account, err := m.emulator.GetAccountByIndex(uint(accountIndex)) |
| 333 | + if err != nil { |
| 334 | + break |
| 335 | + } |
| 336 | + |
| 337 | + for name, code := range account.Contracts { |
| 338 | + location := common.AddressLocation{ |
| 339 | + Address: common.Address(account.Address), |
| 340 | + Name: name, |
| 341 | + } |
| 342 | + |
| 343 | + f, err := zipW.Create(location.ID() + cadenceFileSuffix) |
| 344 | + if err != nil { |
| 345 | + w.WriteHeader(http.StatusInternalServerError) |
| 346 | + return |
| 347 | + } |
| 348 | + |
| 349 | + _, err = f.Write(code) |
| 350 | + if err != nil { |
| 351 | + w.WriteHeader(http.StatusInternalServerError) |
| 352 | + return |
| 353 | + } |
| 354 | + } |
| 355 | + } |
| 356 | + |
| 357 | +} |
0 commit comments