🔮

Oracle Calculator

Learn how script oracles work

Step 1 of 5

1

Create Calculator Oracle

Submit CreateScriptOracle message to create the calculator

🔮 ORACLE CREATION: Submit a CreateScriptOracle message with a JSON Logic script program. This oracle is stateless (initialState: null) and supports add, subtract, multiply, and divide methods. The script uses nested if statements to route to the correct operation based on the "method" parameter.

📨 Message Type: CreateScriptOracle

{
  "cid": "oracle-uuid-here",
  "scriptProgram": {
    "if": [
      {
        "==": [
          {
            "var": "method"
          },
          "add"
        ]
      },
      {
        "+": [
          {
            "var": "args.a"
          },
          {
            "var": "args.b"
          }
        ]
      },
      {
        "if": [
          {
            "==": [
              {
                "var": "method"
              },
              "subtract"
            ]
          },
          {
            "-": [
              {
                "var": "args.a"
              },
              {
                "var": "args.b"
              }
            ]
          },
          {
            "if": [
              {
                "==": [
                  {
                    "var": "method"
                  },
                  "multiply"
                ]
              },
              {
                "*": [
                  {
                    "var": "args.a"
                  },
                  {
                    "var": "args.b"
                  }
                ]
              },
              {
                "if": [
                  {
                    "==": [
                      {
                        "var": "method"
                      },
                      "divide"
                    ]
                  },
                  {
                    "/": [
                      {
                        "var": "args.a"
                      },
                      {
                        "var": "args.b"
                      }
                    ]
                  },
                  {
                    "error": "Unknown method"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "initialState": null,
  "accessControl": {
    "Public": {}
  }
}

Oracle State

null (stateless oracle)

Oracle Script Program

The calculator oracle uses nested if statements in JSON Logic to route method calls to the appropriate arithmetic operation:

{
  "if": [
    {
      "==": [
        {
          "var": "method"
        },
        "add"
      ]
    },
    {
      "+": [
        {
          "var": "args.a"
        },
        {
          "var": "args.b"
        }
      ]
    },
    {
      "if": [
        {
          "==": [
            {
              "var": "method"
            },
            "subtract"
          ]
        },
        {
          "-": [
            {
              "var": "args.a"
            },
            {
              "var": "args.b"
            }
          ]
        },
        {
          "if": [
            {
              "==": [
                {
                  "var": "method"
                },
                "multiply"
              ]
            },
            {
              "*": [
                {
                  "var": "args.a"
                },
                {
                  "var": "args.b"
                }
              ]
            },
            {
              "if": [
                {
                  "==": [
                    {
                      "var": "method"
                    },
                    "divide"
                  ]
                },
                {
                  "/": [
                    {
                      "var": "args.a"
                    },
                    {
                      "var": "args.b"
                    }
                  ]
                },
                {
                  "error": "Unknown method"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Key Concepts Demonstrated

🔮

Script Oracles

Oracles are separate entities created with CreateScriptOracle and invoked with InvokeScriptOracle

📜

JSON Logic Scripts

Oracle programs use JSON Logic with if statements to route method calls and perform computations

Stateless Oracles

Oracles can be stateless (initialState: null) for pure computations like arithmetic

🔗

State Machine Integration

State machines can call oracles using _oracleCall in their effects for external computations