|
| 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