Skip to content

Commit

Permalink
chore: Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
FranklinWaller committed Oct 30, 2023
1 parent 9591ef9 commit 7ba6212
Show file tree
Hide file tree
Showing 18 changed files with 467 additions and 82 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Pull request checks
on:
push:
branches:
- main

jobs:
verify_build:
runs-on: ubuntu-latest
permissions:
contents: read
pages: write
id-token: write
steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'

- name: 📥 Install dependencies
run: npm ci

- name: 🔨 Build projects
run: npm run build

- name: 🧪 Run unit tests
run: npm run test

- name: 📄 Setup Pages
uses: actions/configure-pages@v3

- name: 📚 Create docs
run: npm run docs

- name: 🆙 Upload artifact
uses: actions/upload-pages-artifact@v1
with:
path: './docs'

- name: 📄 Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2


1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
dist
tmp
/out-tsc
docs

# dependencies
node_modules
Expand Down
94 changes: 91 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,105 @@
<p align="center">
<a href="https://seda.xyz/">
<img width="90%" alt="seda-overlay" src="https://www.seda.xyz/images/footer/footer-image.png">
<img width="90%" alt="seda-sdk" src="https://www.seda.xyz/images/footer/footer-image.png">
</a>
</p>

<h1 align="center">
SEDA SDK
</h1>

[![Build Status][actions-badge]][actions-url]
[![GitHub Stars][github-stars-badge]](https://github.com/sedaprotocol/seda-sdk)
[![GitHub Contributors][github-contributors-badge]](https://github.com/sedaprotocol/seda-sdk/graphs/contributors)
[![Discord chat][discord-badge]][discord-url]
[![Twitter][twitter-badge]][twitter-url]

[actions-badge]: https://github.com/sedaprotocol/seda-sdk/actions/workflows/push.yml/badge.svg
[actions-url]: https://github.com/sedaprotocol/seda-sdk/actions/workflows/push.yml+branch%3Amain
[github-stars-badge]: https://img.shields.io/github/stars/sedaprotocol/seda-sdk.svg?style=flat-square&label=github%20stars
[github-contributors-badge]: https://img.shields.io/github/contributors/sedaprotocol/seda-sdk.svg?style=flat-square
[discord-badge]: https://img.shields.io/discord/500028886025895936.svg?logo=discord&style=flat-square
[discord-url]: https://discord.gg/seda
[twitter-badge]: https://img.shields.io/twitter/url/https/twitter.com/SedaProtocol.svg?style=social&label=Follow%20%40SedaProtocol
[twitter-url]: https://twitter.com/SedaProtocol

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

* `vm` - Virtual Machine which can run Data Request WASM binaries
* `as-sdk` - [AssemblyScript](https://www.assemblyscript.org/) SDK
* [vm](./libs/vm/README.md) - Virtual Machine which can run Data Request WASM binaries
* [as-sdk](./libs/as-sdk/README.md) - [AssemblyScript](https://www.assemblyscript.org/) SDK
* [cli](./libs/cli/README.md) - Command Line Interface for uploading and listing Data Request binaries

# Quick getting started

The easiest way to get started it by using our [starter kit](https://github.com/sedaprotocol/seda-sdk-starter-template) this has all the tools installed that you need:

* AssemblyScript
* SEDA SDK
* SEDA CLI
* SEDA VM
* WASI

In our `assembly/index.ts` we have the following example:

```TypeScript
import { Process, httpFetch } from "@seda-protocol/as-sdk/assembly";
import { JSON } from "json-as/assembly";

// Our SWAPI JSON schema, since in AssemblyScript we need to define our shape beforehand
// @ts-expect-error
@json
class SwPlanet {
name!: string
}

function main(): void {
// HTTP Fetch to the SWAPI
const response = httpFetch("https://swapi.dev/api/planets/1/");

// Returns either fulfilled or rejected based on the status code
const fulfilled = response.fulfilled;

if (fulfilled !== null) {
// Converts our buffer to a string
const data = String.UTF8.decode(fulfilled.bytes.buffer);

// Parses the JSON to our schema
const planet = JSON.parse<SwPlanet>(data);

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

main();
```

And in order to test this we have to use a JS testing suite (In our starting kit we use Jest, but any suite should work). We use the `@seda-protocol/vm` package for this. Which runs the binary in the context of a SEDA Data Request:

```JavaScript
import { callVm } from "@seda-protocol/vm";
import { readFile } from "node:fs/promises";

const WASM_PATH = "build/debug.wasm";

describe("index.ts", () => {
it("should be able to run", async () => {
const wasmBinary = await readFile(WASM_PATH);

// Calls our SEDA VM
const vmResult = await callVm({
// Arguments passed to the VM
args: [],
// Environment variables passed to the VM
envs: {},
// The WASM binary in bytes
binary: new Uint8Array(wasmBinary),
});

expect(vmResult.exitCode).toBe(0);
expect(vmResult.resultAsString).toBe("Tatooine");
});
});
```
6 changes: 0 additions & 6 deletions libs/as-sdk-integration-tests/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { httpFetch, Process } from '../../as-sdk/assembly/index';
import { testInvalidUint8JsonArray, testValidUint8JsonArray } from './json-utils-test';

const args = Process.args()[0];

if (args === 'testHttpRejection') {
testHttpRejection();
} else if (args === 'testHttpSuccess') {
testHttpSuccess();
} else if (args === 'testValidUint8JsonArray') {
testValidUint8JsonArray();
} else if (args === 'testInvalidUint8JsonArray') {
testInvalidUint8JsonArray();
}

export function testHttpRejection(): void {
Expand Down
26 changes: 0 additions & 26 deletions libs/as-sdk-integration-tests/assembly/json-utils-test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Process from './process';
import { Process } from '../../as-sdk/assembly/index';

@inline
export function assert(expected: bool, message: string): void {
Expand Down
2 changes: 1 addition & 1 deletion libs/as-sdk-integration-tests/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../../../node_modules/assemblyscript/std/assembly.json",
"include": ["./**/*.ts"]
"include": ["./**/*.ts", "../../as-sdk/assembly/json-utils.ts"]
}
32 changes: 0 additions & 32 deletions libs/as-sdk-integration-tests/src/jsonutils.test.ts

This file was deleted.

31 changes: 31 additions & 0 deletions libs/as-sdk/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
# AssemblyScript SDK

SDK for creating Data Requests on the SEDA chain

For API documentation please see our docs: https://sedaprotocol.github.io/seda-sdk/

## Example

```ts
import { Process, httpFetch } from "@seda-protocol/as-sdk/assembly";
import { JSON } from "json-as/assembly";

// @ts-expect-error
@json
class SwPlanet {
name!: string
}

function main(): void {
const response = httpFetch("https://swapi.dev/api/planets/1/");
const fulfilled = response.fulfilled;

if (fulfilled !== null) {
const data = String.UTF8.decode(fulfilled.bytes.buffer);
const planet = JSON.parse<SwPlanet>(data);

Process.exit_with_message(0, planet.name);
} else {
Process.exit_with_message(1, "Error while fetching");
}
}

main();
```
51 changes: 46 additions & 5 deletions libs/as-sdk/assembly/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,35 @@ import { call_result_write, http_fetch } from './bindings/env';
import { jsonArrToUint8Array } from './json-utils';
import { PromiseStatus, FromBuffer } from './promise';

/**
* Response of an httpFetch call
*/
export class HttpResponse implements FromBuffer<HttpResponse> {
/**
* Raw result of the HTTP fetch. (usually not used)
*/
public result: Uint8Array | null = null;
/**
* The response body result. This can be used to convert to JSON, text, etc.
*/
public bytes: Uint8Array = new Uint8Array(0);
/**
* The length of the content
*/
public contentLength: i64 = 0;
/**
* The URL that was fetched (when it needs to follow redirects)
*/
public url: string = '';
/**
* The HTTP status code
*/
public status: i64 = 0;
/**
* The headers returned
*/
public headers: Map<string, string> = new Map();

json(): JSON.Value {
const body = String.UTF8.decode(this.bytes.buffer);
return JSON.parse(body);
}

fromBuffer(buffer: Uint8Array): HttpResponse {
const response = new HttpResponse();
const value = <JSON.Obj>JSON.parse(buffer);
Expand Down Expand Up @@ -61,6 +77,19 @@ export type HttpFetchMethod = string;

/**
* HTTP Fetch options
* @example
* ```ts
* const headers = new Map<string, string>();
* headers.set('x-header', 'example');
*
* const options = new HttpFetchOptions();
* options.method = "Post";
* options.headers = headers;
* // Do something with the body
* options.body = new Uint8Array(10);
*
* const response = httpFetch("https://swapi.dev/api/planets/1/", options);
* ```
*/
export class HttpFetchOptions {
/**
Expand Down Expand Up @@ -136,6 +165,18 @@ class HttpFetch {
* @param {string} url The URL which to call
* @param {HttpFetchOptions} options Options to modify the behaviour of the HTTP call
* @returns {PromiseStatus<HttpResponse, HttpResponse>} Returns a HttpResponse instance for both fulfilled and rejected case with info about the HTTP call
* @example
* ```ts
* const response = httpFetch("https://swapi.dev/api/planets/1/");
* const fulfilled = response.fulfilled;
*
* if (fulfilled !== null) {
* const data = String.UTF8.decode(fulfilled.bytes.buffer);
* // Do something with data
* } else {
* // Error occured
* }
* ```
*/
export function httpFetch(
url: string,
Expand Down
5 changes: 1 addition & 4 deletions libs/as-sdk/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import Process from './process';
import * as testutils from './test-utils';

export { JSON, JSONDecoder, JSONEncoder, DecoderState, JSONHandler, ThrowingJSONHandler } from 'assemblyscript-json/assembly';
export { httpFetch, HttpFetchMethod, HttpFetchOptions, HttpResponse } from './http';
export { PromiseStatus } from './promise';
export { Process, testutils };
export { jsonArrToUint8Array } from './json-utils';
export { Process };
Loading

0 comments on commit 7ba6212

Please sign in to comment.