package main
import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"github.com/onflow/flow/protobuf/go/flow/access"
)
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)}
req := &access.ExecuteScriptAtLatestBlockRequest{
Script: script,
Arguments: args,
}
valueResp, err := client.ExecuteScriptAtLatestBlock(ctx, req)
respJSON, err := json.Marshal(valueResp)
if err != nil {
log.Fatal("err: ", err)
}
fmt.Println("ExecuteScriptAtLatestBlock response:", string(respJSON))
}