Skip to main content

ExecuteScriptAtBlockHeight Access API Method

Executes a ready-only Cadence script against the execution state at the given block height.

Updated on
Oct 4, 2023

ExecuteScriptAtBlockHeight 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
BlockHeight
string
REQUIRED
The block height for which the script is to be run. The string final and sealed are also supported

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
11
func main() {
12
13
// Requires authenticating before making the request. Refer to Intro Page on how Authentication can be done.
14
client, err := getAccessClientWithBasicAuth("ENDPOINT-NAME", "TOKEN_GOES_HERE")
15
ctx := context.Background()
16
if err != null {
17
log.Fatalf("err: %v", err)
18
}
19
20
// Execute script at latest block
21
script := []byte(`
22
pub fun main(a: Int): Int {
23
return a + 10
24
}
25
`)
26
27
// Convert integer to JSON-CDC (Cadence JSON)
28
arg := 10
29
// JSON-CDC representation of an integer is a JSON object with "type" and "value" fields
30
jsonCDC := fmt.Sprintf(`{"type":"Int","value":"%s"}`, strconv.Itoa(arg))
31
32
// Create script arguments
33
args := [][]byte{[]byte(jsonCDC)}
34
35
// Execute script at Block Height
36
valueHeightResp, err := client.ExecuteScriptAtBlockHeight(ctx, &access.ExecuteScriptAtBlockHeightRequest{Script: script, Arguments: args, BlockHeight: 54774078})
37
respJSON, err := json.Marshal(valueHeightResp)
38
if err != null {
39
log.Fatal("execute script at block height failed: ", err)
40
}
41
fmt.Println("ExecuteScriptAtBlockHeight response:", string(respJSON))
42
}
Don't have an account yet?
Create your QuickNode endpoint in seconds and start building
Get started for free