Skip to main content

ExecuteScriptAtLatestBlock Access API Method

Executes a ready-only Cadence script against the execution state at the block with the given ID

Updated on
Oct 4, 2023

ExecuteScriptAtLatestBlock Access API Method

Parameters

Script
string
REQUIRED
The Base64 encoded content of the Cadence script
Arguments
string
REQUIRED
A list of arguments each encoded as Base64 passed in the JSON-Cadence interchange format

Returns

value
string
The result of the executed Cadence script
Request
1
package main
2
3
import (
4
"context"
5
"encoding/json"
6
"fmt"
7
"log"
8
"strconv"
9
10
"github.com/onflow/flow/protobuf/go/flow/access"
11
)
12
13
14
func main() {
15
16
// Requires authenticating before making the request. Refer to Intro Page on how Authentication can be done.
17
client, err := getAccessClientWithBasicAuth("ENDPOINT-NAME", "TOKEN_GOES_HERE")
18
ctx := context.Background()
19
if err != null {
20
log.Fatalf("err: %v", err)
21
}
22
23
// Execute Script at Latest Block
24
script := []byte(`
25
pub fun main(a: Int): Int {
26
return a + 10
27
}
28
`)
29
30
// Convert integer to JSON-CDC (Cadence JSON)
31
arg := 10
32
// JSON-CDC representation of an integer is a JSON object with "type" and "value" fields
33
jsonCDC := fmt.Sprintf(`{"type":"Int","value":"%s"}`, strconv.Itoa(arg))
34
35
// Create script arguments
36
args := [][]byte{[]byte(jsonCDC)}
37
38
req := &access.ExecuteScriptAtLatestBlockRequest{
39
Script: script,
40
Arguments: args,
41
}
42
43
valueResp, err := client.ExecuteScriptAtLatestBlock(ctx, req)
44
respJSON, err := json.Marshal(valueResp)
45
if err != null {
46
log.Fatal("err: ", err)
47
}
48
fmt.Println("ExecuteScriptAtLatestBlock response:", string(respJSON))
49
50
}
Don't have an account yet?
Create your QuickNode endpoint in seconds and start building
Get started for free