-
Notifications
You must be signed in to change notification settings - Fork 175
/
scripts.go
36 lines (29 loc) · 1.13 KB
/
scripts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package rest
import (
"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/request"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/access"
)
// ExecuteScript handler sends the script from the request to be executed.
func ExecuteScript(r *request.Request, backend access.API, _ models.LinkGenerator) (interface{}, error) {
req, err := r.GetScriptRequest()
if err != nil {
return nil, NewBadRequestError(err)
}
if req.BlockID != flow.ZeroID {
return backend.ExecuteScriptAtBlockID(r.Context(), req.BlockID, req.Script.Source, req.Script.Args)
}
// default to sealed height
if req.BlockHeight == request.SealedHeight || req.BlockHeight == request.EmptyHeight {
return backend.ExecuteScriptAtLatestBlock(r.Context(), req.Script.Source, req.Script.Args)
}
if req.BlockHeight == request.FinalHeight {
finalBlock, _, err := backend.GetLatestBlockHeader(r.Context(), false)
if err != nil {
return nil, err
}
req.BlockHeight = finalBlock.Height
}
return backend.ExecuteScriptAtBlockHeight(r.Context(), req.BlockHeight, req.Script.Source, req.Script.Args)
}