-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #328 from polywrap/create-wraps-tutorial
New "Create Wraps" Tutorial
- Loading branch information
Showing
110 changed files
with
22,651 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
build | ||
node_modules | ||
wrap | ||
.polywrap | ||
workflows/output.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v18.15.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
snippets/create-wraps-tutorial/assemblyscript/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
snippets/create-wraps-tutorial/assemblyscript/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
snippets/create-wraps-tutorial/assemblyscript/polywrap.deploy.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
2 changes: 2 additions & 0 deletions
2
snippets/create-wraps-tutorial/assemblyscript/polywrap.docs.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
format: 0.1.0 | ||
readme: ./README.md |
7 changes: 7 additions & 0 deletions
7
snippets/create-wraps-tutorial/assemblyscript/polywrap.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
snippets/create-wraps-tutorial/assemblyscript/polywrap.test.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
37 changes: 37 additions & 0 deletions
37
snippets/create-wraps-tutorial/assemblyscript/src/__tests__/e2e/integration.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
|
9 changes: 9 additions & 0 deletions
9
snippets/create-wraps-tutorial/assemblyscript/src/__tests__/types/polywrap.app.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
1 change: 1 addition & 0 deletions
1
snippets/create-wraps-tutorial/assemblyscript/src/__tests__/types/schema.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
71
snippets/create-wraps-tutorial/assemblyscript/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
snippets/create-wraps-tutorial/assemblyscript/tsconfig.build.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
snippets/create-wraps-tutorial/assemblyscript/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] }, | ||
} |
11 changes: 11 additions & 0 deletions
11
snippets/create-wraps-tutorial/assemblyscript/workflows/validator.cue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package e2e | ||
|
||
sampleMethod: { | ||
$0: { | ||
data: { | ||
value: "polywrap" | ||
}, | ||
error?: _|_, | ||
} | ||
} | ||
|
Oops, something went wrong.