Skip to content

Commit 6074a13

Browse files
authored
Merge pull request #905 from onflow/bastian/add-all-contracts-endpoint
Add endpoint to download ZIP file of all contracts
2 parents c5cce13 + 4077c1d commit 6074a13

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

server/utils/emulator.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
package utils
2020

2121
import (
22+
"archive/zip"
2223
"encoding/json"
2324
"net/http"
2425
"slices"
2526
"strconv"
2627
"strings"
2728

2829
"github.com/gorilla/mux"
30+
"github.com/onflow/cadence/common"
2931
"github.com/onflow/cadence/runtime"
3032
flowgo "github.com/onflow/flow-go/model/flow"
3133

@@ -72,6 +74,8 @@ func NewEmulatorAPIServer(emulator emulator.Emulator, adapter *adapters.AccessAd
7274

7375
router.HandleFunc("/emulator/computationReport", r.ComputationReport).Methods("GET")
7476

77+
router.HandleFunc("/emulator/allContracts", r.AllContractsZip).Methods("GET")
78+
7579
return r
7680
}
7781

@@ -327,3 +331,43 @@ func (m EmulatorAPIServer) Logs(w http.ResponseWriter, r *http.Request) {
327331
return
328332
}
329333
}
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

Comments
 (0)