package main
import (
"context"
"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)
}
// Get Latest (sealed) block
latestResp, err := client.GetLatestBlock(ctx, &access.GetLatestBlockRequest{IsSealed: true})
if err != nil {
log.Fatal("Get latest block failed: ", err)
}
fmt.Println("Latest block height: ", latestResp.Block.Height)
// execute script at latest block ID
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)}
valueHeightResp, err := client.ExecuteScriptAtBlockID(ctx, &access.ExecuteScriptAtBlockIDRequest{Script: script, Arguments: args, BlockId: latestResp.Block.Id})
if err != nil {
log.Fatal("execute script at block ID failed: ", err)
}
fmt.Printf("executed script , returned %s \n", string(valueHeightResp.Value))
}