Skip to content

Commit 712b051

Browse files
committed
full redo of javascript quickstart
1 parent fdad639 commit 712b051

File tree

11 files changed

+2381
-417
lines changed

11 files changed

+2381
-417
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@
7272
},
7373
"doc-snippets": {
7474
"extract": {
75-
"include": "./**/*.{ts,tsx,json,yaml,txt,md,graphql,cue}",
75+
"include": "./**/*.{ts,tsx,js,json,yaml,txt,md,graphql,cue}",
7676
"ignore": [
7777
"./**/node_modules/**",
78-
"./**/.polywrap/**"
78+
"./**/.polywrap/**",
79+
"./**/build/**"
7980
],
8081
"dir": "./snippets"
8182
},

snippets/quick-start/js/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

snippets/quick-start/js/.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v18.15.0
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "templates-app-typescript",
3+
"description": "Polywrap App TypeScript Template",
4+
"private": true,
5+
"version": "0.12.0",
6+
"type": "module",
7+
"scripts": {
8+
"codegen": "npx polywrap codegen",
9+
"test": "node ./src/index.js"
10+
},
11+
"dependencies": {
12+
"@polywrap/client-js": "~0.12.0"
13+
},
14+
"devDependencies": {
15+
"polywrap": "0.12.0"
16+
}
17+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// $start: quickstart-js-init-client
2+
import { PolywrapClient } from "@polywrap/client-js";
3+
4+
const client = new PolywrapClient();
5+
// $end
6+
7+
async function main() {
8+
// $start: quickstart-js-1st-invoke
9+
const result = await client.invoke({
10+
uri: "wrapscan.io/polywrap/sha3@1.0",
11+
method: "sha3_256",
12+
args: {
13+
message: "Hello Polywrap!",
14+
},
15+
});
16+
17+
console.log(result);
18+
// $end
19+
20+
// $start: quickstart-js-uniswap
21+
const wethResult = await client.invoke({
22+
uri: "wrapscan.io/polywrap/uniswap-v3@1.0",
23+
method: "fetchToken",
24+
args: {
25+
address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
26+
chainId: "MAINNET",
27+
},
28+
});
29+
30+
// Log the invocation error and stop execution if the invocation fails
31+
if (!wethResult.ok) {
32+
throw wethResult.error;
33+
}
34+
35+
console.log("WETH:", wethResult.value);
36+
37+
const usdcResult = await client.invoke({
38+
uri: "wrapscan.io/polywrap/uniswap-v3@1.0",
39+
method: "fetchToken",
40+
args: {
41+
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
42+
chainId: "MAINNET",
43+
},
44+
});
45+
46+
// Log the invocation error and stop execution if the invocation fails
47+
if (!usdcResult.ok) {
48+
throw usdcResult.error;
49+
}
50+
51+
console.log("USDC:", usdcResult.value);
52+
53+
const poolAddressResult = await client.invoke({
54+
uri: "wrapscan.io/polywrap/uniswap-v3@1.0",
55+
method: "getPoolAddress",
56+
args: {
57+
tokenA: wethResult.value,
58+
tokenB: usdcResult.value,
59+
fee: "MEDIUM",
60+
},
61+
});
62+
63+
// Log the invocation error and stop execution if the invocation fails
64+
if (!poolAddressResult.ok) {
65+
throw poolAddressResult.error;
66+
}
67+
68+
console.log("Pool address:", poolAddressResult.value);
69+
// $end
70+
71+
// $start: quickstart-js-resolve
72+
// We first want to resolve the IPFS WRAP URI of the UniswapV3 Wrap
73+
const resolutionResult = await client.tryResolveUri({
74+
uri: "wrapscan.io/polywrap/wrapscan-uri-resolver@1.0",
75+
});
76+
77+
if (!resolutionResult.ok) {
78+
throw resolutionResult.error;
79+
}
80+
81+
console.log(resolutionResult.value);
82+
// $end
83+
84+
// $start: quickstart-js-get-schema
85+
// Extract the IPFS CID from the resolution result's URI
86+
const cid = resolutionResult.value.uri.uri.replace("wrap://ipfs/", "");
87+
88+
// Since the CID is a directory, we need to add a path to the Wrap's manifest file
89+
const catResult = await client.invoke({
90+
uri: "wrapscan.io/polywrap/ipfs-http-client@1.0",
91+
method: "cat",
92+
args: {
93+
cid: cid + "/wrap.info",
94+
ipfsProvider: "https://ipfs.wrappers.io",
95+
},
96+
});
97+
98+
console.log(catResult);
99+
100+
if (!catResult.ok) {
101+
throw catResult.error;
102+
}
103+
104+
// Turn the returned buffer into a string and log it
105+
const schema = new TextDecoder().decode(catResult.value);
106+
107+
console.log(schema);
108+
// $end
109+
}
110+
111+
main()
112+
.then(() => process.exit(0))
113+
.catch((error) => {
114+
console.error(error);
115+
process.exit(1);
116+
});

0 commit comments

Comments
 (0)