package main
import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
)
func main() {
// Requires authenticating before making the request. Refer to Intro Page on how Authentication can be done.
client, err := getAccessClientWithBasicAuth("ENDPOINT-NAME", "TOKEN_GOES_HERE")
ctx := context.Background()
if err != nil {
log.Fatalf("err: %v", err)
}
// Execute script at latest block
script := []byte(`
pub fun main(a: Int): Int {
return a + 10
}
`)
// Convert integer to JSON-CDC (Cadence JSON)
arg := 10
// JSON-CDC representation of an integer is a JSON object with "type" and "value" fields
jsonCDC := fmt.Sprintf(`{"type":"Int","value":"%s"}`, strconv.Itoa(arg))
// Create script arguments
args := [][]byte{[]byte(jsonCDC)}
// Execute script at Block Height
valueHeightResp, err := client.ExecuteScriptAtBlockHeight(ctx, &access.ExecuteScriptAtBlockHeightRequest{Script: script, Arguments: args, BlockHeight: 54774078})
respJSON, err := json.Marshal(valueHeightResp)
if err != nil {
log.Fatal("execute script at block height failed: ", err)
}
fmt.Println("ExecuteScriptAtBlockHeight response:", string(respJSON))
}