Skip to content

Commit 485f987

Browse files
committed
add endpoint to download ZIP file of all contracts
1 parent 1310af4 commit 485f987

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

server/utils/emulator.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
package utils
2020

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

2728
"github.com/gorilla/mux"
29+
"github.com/onflow/cadence/common"
2830
"github.com/onflow/cadence/runtime"
2931
flowgo "github.com/onflow/flow-go/model/flow"
3032

@@ -71,6 +73,8 @@ func NewEmulatorAPIServer(emulator emulator.Emulator, adapter *adapters.AccessAd
7173

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

76+
router.HandleFunc("/emulator/all-contracts", r.AllContractsZip).Methods("GET")
77+
7478
return r
7579
}
7680

@@ -313,3 +317,41 @@ func (m EmulatorAPIServer) Logs(w http.ResponseWriter, r *http.Request) {
313317
return
314318
}
315319
}
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

Comments
 (0)