Skip to main content

ExecuteScriptAtBlockID Access API Method

Loading...

Updated on
Oct 04, 2023

ExecuteScriptAtBlockID Access API Method

Parameters

Script
string
REQUIRED
Loading...
Arguments
string
REQUIRED
Loading...
BlockId
string
REQUIRED
Loading...

Returns

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