Skip to content

Commit

Permalink
Merge pull request #328 from polywrap/create-wraps-tutorial
Browse files Browse the repository at this point in the history
New "Create Wraps" Tutorial
  • Loading branch information
dOrgJelli authored Nov 3, 2023
2 parents f0593fe + 9eae6ed commit 20cdd84
Show file tree
Hide file tree
Showing 110 changed files with 22,651 additions and 343 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"npm-run-all": "^4.1.5",
"onchange": "^7.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
"typescript": "^4.7.4",
"polywrap": "0.12.1"
},
"browserslist": {
"production": [
Expand Down
28 changes: 19 additions & 9 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,30 @@ const sidebars = {
type: 'category',
label: 'Create Wraps',
items: [
'tutorials/create-wraps/introduction',
{
type: 'category',
label: 'Tutorial',
label: 'Part One',
items: [
'tutorials/create-wraps/tutorial/project-setup',
'tutorials/create-wraps/tutorial/project-folder',
'tutorials/create-wraps/tutorial/build-deploy-test',
'tutorials/create-wraps/tutorial/adding-new-methods',
'tutorials/create-wraps/tutorial/writing-tests-with-workflows',
'tutorials/create-wraps/tutorial/deploy-locally-and-test',
'tutorials/create-wraps/part-one/project-setup',
'tutorials/create-wraps/part-one/project-manifest',
'tutorials/create-wraps/part-one/schema-and-codegen',
'tutorials/create-wraps/part-one/first-method',
'tutorials/create-wraps/part-one/import-and-subinvoke',
'tutorials/create-wraps/part-one/interfaces-and-plugins',
'tutorials/create-wraps/part-one/integration-testing',
'tutorials/create-wraps/part-one/deployment'
]
},
'tutorials/create-wraps/polywrap-manifest',
'tutorials/create-wraps/default-plugins'
{
type: 'category',
label: 'Part Two',
items: [
'tutorials/create-wraps/part-two/polywrap-manifest',
'tutorials/create-wraps/part-two/default-plugins',
'tutorials/create-wraps/part-two/writing-tests-with-workflows'
]
}
],
},
{
Expand Down
5 changes: 5 additions & 0 deletions snippets/create-wraps-tutorial/assemblyscript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build
node_modules
wrap
.polywrap
workflows/output.json
1 change: 1 addition & 0 deletions snippets/create-wraps-tutorial/assemblyscript/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v18.15.0
16 changes: 16 additions & 0 deletions snippets/create-wraps-tutorial/assemblyscript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Polywrap Wasm Wrapper Template
A simple starter template for an AssemblyScript Wasm wrapper. For more information on how this project works, and a step by step on how to extend its behavior, see the documentation [here](https://docs.polywrap.io/).

# How To Run

## Install Dependencies
`nvm install && nvm use`
`yarn`

## Codegen & Build
`yarn codegen`
`yarn build`

## Test
`yarn test:e2e`
`yarn test:workflow`
15 changes: 15 additions & 0 deletions snippets/create-wraps-tutorial/assemblyscript/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
collectCoverage: false,
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["**/__tests__/**/?(*.)+(spec|test).[jt]s?(x)"],
globals: {
"ts-jest": {
tsconfig: "tsconfig.json",
diagnostics: false,
},
},
testPathIgnorePatterns: [
"/.polywrap/"
],
};
28 changes: 28 additions & 0 deletions snippets/create-wraps-tutorial/assemblyscript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "template-wasm-as",
"description": "Polywrap AssemblyScript Wrapper Template",
"private": true,
"version": "0.12.1",
"scripts": {
"codegen": "npx polywrap codegen",
"build": "npx polywrap build",
"test:env:up": "npx polywrap infra up --modules=eth-ens-ipfs",
"test:env:down": "npx polywrap infra down --modules=eth-ens-ipfs",
"deploy": "npx polywrap deploy",
"test": "yarn test:e2e && yarn test:workflow",
"test:e2e": "yarn test:e2e:codegen && jest --passWithNoTests --runInBand --verbose",
"test:e2e:codegen": "npx polywrap codegen -m ./src/__tests__/types/polywrap.app.yaml -g ./src/__tests__/types/wrap",
"test:workflow": "npx polywrap test -o ./workflows/output.json"
},
"devDependencies": {
"@types/jest": "26.0.8",
"jest": "26.6.3",
"polywrap": "0.12.1",
"ts-jest": "26.5.4",
"typescript": "4.9.5"
},
"dependencies": {
"@polywrap/wasm-as": "0.12.1",
"assemblyscript": "0.19.23"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
format: "0.1"
stages:
ipfs_deploy:
package: ipfs
uri: fs/./build
config:
gatewayUri: "https://ipfs.wrappers.io"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
format: 0.1.0
readme: ./README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import { Module } into Sha3 from "wrapscan.io/polywrap/sha3@1.0.0"
#import { Module } into Http from "wrapscan.io/polywrap/http@1.0"

type Module {
obscure(data: [String!]!, chaosLevel: Int): String!
enlighten(question: String!, apiKey: String!): String!
}
10 changes: 10 additions & 0 deletions snippets/create-wraps-tutorial/assemblyscript/polywrap.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: template-wasm-rs
format: 0.1.0
validation: "./workflows/validator.cue"
jobs:
sampleMethod:
steps:
- uri: fs/build
method: sampleMethod
args:
arg: "polywrap"
9 changes: 9 additions & 0 deletions snippets/create-wraps-tutorial/assemblyscript/polywrap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
format: 0.3.0
project:
name: template-wasm-as
type: wasm/assemblyscript
source:
module: ./src/index.ts
schema: ./polywrap.graphql
extensions:
docs: ./polywrap.docs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as App from "../types/wrap";
import path from "path";

jest.setTimeout(60000);

describe("Oracle Wrap End to End Tests", () => {

let oracle: App.Oracle;
let wrapperUri: string;
let apiKey: string = process.env.PPLX_API_KEY || "";

beforeAll(() => {
const wrapperPath: string = path.join(__dirname, "..", "..", "..");
wrapperUri = `fs/${wrapperPath}/build`;
oracle = new App.Oracle(undefined, undefined, wrapperUri)
})

it("calls obscure", async () => {
const result = await oracle.obscure({
data: ["Hello", "World"],
chaosLevel: 3
});

if (!result.ok) throw result.error;
expect(typeof result.value === "string").toBeTruthy();
expect(result.value.length).toEqual(128);
});

it("calls enlighten", async () => {
const question = "What is the meaning of life?";
const result = await oracle.enlighten({ question, apiKey });

if (!result.ok) throw result.error;
expect(typeof result.value === "string").toBeTruthy();
});
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
format: 0.3.0
project:
name: sample-typescript-type-generation
type: app/typescript
source:
schema: ./schema.graphql
import_abis:
- uri: "wrap://ens/sample.eth"
abi: "../../../build/wrap.info"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import * into Oracle from "wrap://ens/sample.eth"
71 changes: 71 additions & 0 deletions snippets/create-wraps-tutorial/assemblyscript/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
Args_enlighten,
Args_obscure,
Http_Module,
Http_Request,
Http_Response,
Http_ResponseType,
ModuleBase,
Sha3_Module
} from './wrap';

export class Module extends ModuleBase {
obscure(args: Args_obscure): string {
// handle default values
const chaosLevel: i32 = (args.chaosLevel == null || args.chaosLevel!!.unwrap() < 1)
? 1
: args.chaosLevel!!.unwrap();

let obscured: string = "";
for (let i = 0; i < args.data.length; ++i) {
let data = args.data[i];
for (let j = 0; j < chaosLevel; ++j) {
data = Sha3_Module.keccak_256({ message: data }).expect("hash failed");
}
obscured += data;
}
return obscured;
}

enlighten(args: Args_enlighten): string {
const request: Http_Request = {
headers: new Map<string, string>()
.set('accept', 'application/json')
.set('content-type', 'application/json')
.set("Authorization", `Bearer ${args.apiKey}`),
responseType: Http_ResponseType.TEXT,
body: `{
"model": "mistral-7b-instruct",
"messages": [
{
"role": "system",
"content": "Be precise and concise."
},
{
"role": "user",
"content": "${args.question}"
}
]
}`
};

const response: Http_Response | null = Http_Module.post({
url: "https://api.perplexity.ai/chat/completions",
request,
}).unwrap();

if (response == null) {
throw new Error("request failed with null response");
}

if (response.status != 200) {
throw new Error(`request failed with status ${response.status}`);
}

if (response.body == null) {
throw new Error("request failed with null body");
}

return response.body!!;
}
}
12 changes: 12 additions & 0 deletions snippets/create-wraps-tutorial/assemblyscript/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "assemblyscript/std/assembly.json",
"compilerOptions": {
"outDir": "build"
},
"include": [
"./src/**/*.ts",
],
"exclude": [
"./src/__tests__/e2e/**/*.ts"
]
}
28 changes: 28 additions & 0 deletions snippets/create-wraps-tutorial/assemblyscript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"compilerOptions": {
"lib": [
"es2020",
"es2015",
"es5",
"dom"
],
"esModuleInterop": true,
"outDir": "build",
"moduleResolution": "node",
"declaration": true,
"preserveSymlinks": true,
"preserveWatchOutput": true,
"pretty": false,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"module": "commonjs",
"sourceMap": true,
"target": "es5",
"resolveJsonModule": true,
"strictNullChecks": true
},
"typeAcquisition": { "include": ["jest"] },
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package e2e

sampleMethod: {
$0: {
data: {
value: "polywrap"
},
error?: _|_,
}
}

Loading

0 comments on commit 20cdd84

Please sign in to comment.