Skip to content

sedaprotocol/seda-sdk

Repository files navigation

seda-sdk

SEDA SDK

Build Status GitHub Stars GitHub Contributors Discord chat Twitter

Collection of packages which allow you to build SEDA Data Requests:

  • as-sdk - AssemblyScript SDK to help building Oracle Programs
  • dev-tools - Command Line Interface for uploading and listing Oracle Programs
  • vm - Virtual Machine which can run Oracle Programs locally for testing

Quick getting started

The easiest way to get started it by using our starter kit this has all the tools installed that you need:

  • AssemblyScript
  • JSON-AS
  • WASI
  • SEDA SDK
  • SEDA Dev Tools
  • SEDA VM

For API documentation check the TypeDocs, and for guides and examples check <LINK_HERE>.

Example

Below is an example of an Oracle Program that retrieves the name of a planet in the SWAPI database.

import { Process, httpFetch, OracleProgram, Bytes, JSON } from "@seda-protocol/as-sdk/assembly";

// The JSON schema of the response we're expecting, since in AssemblyScript we need to deserialize JSON into structured objects
@json
class SwPlanet {
  name!: string;
}

class PlanetProgram extends OracleProgram {
  execution() {
    const input = Process.getInputs().toUtf8String();

    // HTTP Fetch to the SWAPI
    const response = httpFetch(`https://swapi.dev/api/planets/${input}`);

    if (response.ok) {
      // We need to parse the response Bytes as the expected JSON.
      const planet = response.bytes.toJSON<SwPlanet>();

      // Exits the program (with an exit code of 0) and sets the Data Request result to the planet name
      Process.success(Bytes.fromUtf8String(planet.name));
    } else {
      Process.error(Bytes.fromUtf8String("Error while fetching"));
    }
  }
}

new PlanetProgram().run();

Testing

In order to test this we can use a JS testing suite (we use Bun:test in this repository and the starter kits, but any runner should work). We use the @seda-protocol/dev-tools package for this, which runs the Oracle Program in a similar environment as it would on the SEDA network:

import { testOracleProgramExecution } from "@seda-protocol/dev-tools";
import { readFile } from "node:fs/promises";

const WASM_PATH = "build/debug.wasm";

describe("Oracle Program: execution", () => {
  it("should be able to run", async () => {
    const oracleProgram = await readFile(WASM_PATH);

    // Calls our SEDA VM
    const vmResult = await testOracleProgramExecution(
      // The bytes of the Oracle Program
      oracleProgram,
      // Inputs for the Oracle Program
      Buffer.from("1")
    );

    expect(vmResult.exitCode).toBe(0);
    expect(vmResult.resultAsString).toBe("Tatooine");
  });
});