Struggle with executing a Cadence Script

Hello!
I’m just trying to execute a cadence script via API call from here

I want to know how should I execute script for example to know about “Get All Info about a single Delegator” from here:

How should I pass request body schema?

Hey there!

Pre-requisites

You can get the address of the 0xIDENTITYTABLEADDRESS from Flow Staking Contract Reference | Flow Blockchain. For mainnet, that’s 0x8624b52f9ddcd04a

I also recommend you use an app like Postman or Insomnia and load the OpenAPI spec. Here’s the URL to import: https://raw.githubusercontent.com/onflow/flow/master/openapi/access.yaml

It will make it easier to test and debug your requests.

Simple Example

In response to your question, here’s an example script sent through the REST API. In this case, we will run the getNodeIDs() method so we can get a list of NodeIDs and use one of the IDs to show you how to pass in an argument later.

Cadence script:

import FlowIDTableStaking from 0x8624b52f9ddcd04a

pub fun main(): [String] {
    return FlowIDTableStaking.getNodeIDs()
}

To be sent via the API, it needs to be Base64 encoded (using a site like: https://www.base64encode.org/)

aW1wb3J0IEZsb3dJRFRhYmxlU3Rha2luZyBmcm9tIDB4ODYyNGI1MmY5ZGRjZDA0YQoKcHViIGZ1biBtYWluKCk6IFtTdHJpbmddIHsKICAgIHJldHVybiBGbG93SURUYWJsZVN0YWtpbmcuZ2V0Tm9kZUlEcygpCn0=

You would then construct the request as follows:

curl --request POST \
  --url https://rest-mainnet.onflow.org/v1/scripts \
  --header 'Content-Type: application/json' \
  --data '{
  "script": "aW1wb3J0IEZsb3dJRFRhYmxlU3Rha2luZyBmcm9tIDB4ODYyNGI1MmY5ZGRjZDA0YQoKcHViIGZ1biBtYWluKCk6IFtTdHJpbmddIHsKICAgIHJldHVybiBGbG93SURUYWJsZVN0YWtpbmcuZ2V0Tm9kZUlEcygpCn0=",
  "arguments": []
}'

The API would return a response like:

eyJ2YWx1ZSI6W3sidmFsdWUiOiIyMjk0YzMyYmViNDZiMTJjZDdiYzY3ZTQwZWRhMzY3ZjZmMzg0ODg1OTRjMjkzNDc2YjQxYTAzNTY5NGE2YzgxIiwidHlwZSI6IlN0cmluZyJ9LHsidmFsdWUiOiI1ZTEzYmE3NTZhN2NjMzQxOGE1YzMzMzZiZjA1OTMzZWY1YmNmNjYzMzQyZTM0OGNlZGM4NjAwMGRhMTEzY2ExIiwidHlwZSI6IlN0cmluZyJ9LHsidmFsdWUiOiIzOGFmYTY4YmU5ZjVhOWU0Y2FhYWJkZDcyOTFmY2U0MDBiYTA2NGRhZmIzNDYzYmU0ZTFmMTVjNzBjMWEzY2NmIiwidHlwZSI6IlN0cmluZyJ9...

Which if you Base64 decode, would be:

{
   "value":[
      {
         "value":"2294c32beb46b12cd7bc67e40eda367f6f38488594c293476b41a035694a6c81",
         "type":"String"
      },
      {
         "value":"5e13ba756a7cc3418a5c3336bf05933ef5bcf663342e348cedc86000da113ca1",
         "type":"String"
      },
      {
         "value":"38afa68be9f5a9e4caaabdd7291fce400ba064dafb3463be4e1f15c70c1a3ccf",
         "type":"String"
      },
      ...

Passing Arguments

To pass in arguments, you just need to follow the JSON-Cadence Data Interchange Format and base64 encode each one, which you can read all about here: JSON-Cadence Data Interchange Format | Flow Blockchain.

Here’s an example: let’s say we want to run a script to get the Node info. You’ll see that script needs a nodeID argument, much like the script you want to run.

import FlowIDTableStaking from 0x8624b52f9ddcd04a

// This script gets all the info about a node and returns it

pub fun main(nodeID: String): FlowIDTableStaking.NodeInfo {
    return FlowIDTableStaking.NodeInfo(nodeID: nodeID)
}

Let’s use a test NodeID from the example above (which returns a list of node IDs), and generate an argument like this to add to the arguments list.

{
"type": "String",
"value": "2294c32beb46b12cd7bc67e40eda367f6f38488594c293476b41a035694a6c81"
}

Just Base64 encode the JSON type and include it in the argument array. This is the payload you would send:

{
  "script": "aW1wb3J0IEZsb3dJRFRhYmxlU3Rha2luZyBmcm9tIDB4ODYyNGI1MmY5ZGRjZDA0YQoKcHViIGZ1biBtYWluKG5vZGVJRDogU3RyaW5nKTogRmxvd0lEVGFibGVTdGFraW5nLk5vZGVJbmZvIHsKICAgIHJldHVybiBGbG93SURUYWJsZVN0YWtpbmcuTm9kZUluZm8obm9kZUlEOiBub2RlSUQpCn0=",
  "arguments": [
"ewoidHlwZSI6ICJTdHJpbmciLAoidmFsdWUiOiAiMjI5NGMzMmJlYjQ2YjEyY2Q3YmM2N2U0MGVkYTM2N2Y2ZjM4NDg4NTk0YzI5MzQ3NmI0MWEwMzU2OTRhNmM4MSIKfQ=="
	]
}

In shell, again that would be something like:

curl --request POST \
  --url https://rest-mainnet.onflow.org/v1/scripts \
  --header 'Content-Type: application/json' \
  --data '{
  "script": "aW1wb3J0IEZsb3dJRFRhYmxlU3Rha2luZyBmcm9tIDB4ODYyNGI1MmY5ZGRjZDA0YQoKcHViIGZ1biBtYWluKG5vZGVJRDogU3RyaW5nKTogRmxvd0lEVGFibGVTdGFraW5nLk5vZGVJbmZvIHsKICAgIHJldHVybiBGbG93SURUYWJsZVN0YWtpbmcuTm9kZUluZm8obm9kZUlEOiBub2RlSUQpCn0=",
  "arguments": [
"ewoidHlwZSI6ICJTdHJpbmciLAoidmFsdWUiOiAiMjI5NGMzMmJlYjQ2YjEyY2Q3YmM2N2U0MGVkYTM2N2Y2ZjM4NDg4NTk0YzI5MzQ3NmI0MWEwMzU2OTRhNmM4MSIKfQ=="
	]
}'

Here is the Base64 decoded response from that call:

{
  "value": {
    "id": "A.8624b52f9ddcd04a.FlowIDTableStaking.NodeInfo",
    "fields": [
      {
        "value": {
          "value": "2294c32beb46b12cd7bc67e40eda367f6f38488594c293476b41a035694a6c81",
          "type": "String"
        },
        "name": "id"
      },
      {
        "value": {
          "value": "5",
          "type": "UInt8"
        },
        "name": "role"
      },
      {
        "value": {
          "value": "flo9-aws-us-east-1.flonode.alchemyapi.io:3569",
          "type": "String"
        },
        "name": "networkingAddress"
      },
      {
        "value": {
          "value": "c8bec113851ce993b56276afe355eabb618d4f344abcbb529524212048cee1440105a1f5696f4c7c28076db39e92263d6e1fe4f0f97e5c807f510579cdbde452",
          "type": "String"
        },
        "name": "networkingKey"
      },
      {
        "value": {
          "value": "9475d65e36aa53d5872c3b6266733e620b1a515a7fa760c46d1c6c90346a90fc2d51948f02b15daa4969583c665c43120d66628b2e5d1216206d920c884ec96b5f3f8baa1381855afe153481a0bc56ea77ea8254b1632284e4db273798d0f913",
          "type": "String"
        },
        "name": "stakingKey"
      },
      {
        "value": {
          "value": "0.00000000",
          "type": "UFix64"
        },
        "name": "tokensStaked"
      },
      {
        "value": {
          "value": "0.00000000",
          "type": "UFix64"
        },
        "name": "tokensCommitted"
      },
      {
        "value": {
          "value": "0.00000000",
          "type": "UFix64"
        },
        "name": "tokensUnstaking"
      },
      {
        "value": {
          "value": "0.00000000",
          "type": "UFix64"
        },
        "name": "tokensUnstaked"
      },
      {
        "value": {
          "value": "0.00000000",
          "type": "UFix64"
        },
        "name": "tokensRewarded"
      },
      {
        "value": {
          "value": [],
          "type": "Array"
        },
        "name": "delegators"
      },
      {
        "value": {
          "value": "0",
          "type": "UInt32"
        },
        "name": "delegatorIDCounter"
      },
      {
        "value": {
          "value": "0.00000000",
          "type": "UFix64"
        },
        "name": "tokensRequestedToUnstake"
      },
      {
        "value": {
          "value": "100",
          "type": "UInt64"
        },
        "name": "initialWeight"
      }
    ]
  },
  "type": "Struct"
}

Hope you get the gist!

1 Like

Thank you so much! Now I see, it’s now very clear)

1 Like