Skip to content

Commit c999be8

Browse files
committed
feat(ts-sdk): add test case decoding with wasm
Signed-off-by: Eric Hegnes <eric@hegnes.com>
1 parent 2d899d6 commit c999be8

File tree

12 files changed

+749
-8
lines changed

12 files changed

+749
-8
lines changed

app2/src/lib/components/model/PacketComponent.svelte

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<script lang="ts">
2-
import BlockHashComponent from "$lib/components/model/BlockHashComponent.svelte"
32
import ChainComponent from "$lib/components/model/ChainComponent.svelte"
43
import ErrorComponent from "$lib/components/model/ErrorComponent.svelte"
54
import HeightComponent from "$lib/components/model/HeightComponent.svelte"
65
import PacketTracesComponent from "$lib/components/model/PacketTracesComponent.svelte"
7-
import TransactionHashComponent from "$lib/components/model/TransactionHashComponent.svelte"
8-
import DateTimeComponent from "$lib/components/ui/DateTimeComponent.svelte"
96
import Label from "$lib/components/ui/Label.svelte"
107
import LongMonoWord from "$lib/components/ui/LongMonoWord.svelte"
118
import Skeleton from "$lib/components/ui/Skeleton.svelte"
129
import * as AppRuntime from "$lib/runtime"
1310
import { chains } from "$lib/stores/chains.svelte"
1411
import { packetDetails } from "$lib/stores/packets.svelte"
12+
import ucs03Wasm from "$wasm/ucs03-zkgm-packet_bg.wasm?url"
1513
import { getChain } from "@unionlabs/sdk/schema"
1614
import * as Ucs03 from "@unionlabs/sdk/Ucs03"
15+
import { WasmTest } from "@unionlabs/sdk/WasmTest"
1716
import { Effect, Option } from "effect"
1817
import { pipe } from "effect/Function"
19-
import { getOrUndefined } from "effect/Option"
2018
import * as S from "effect/Schema"
21-
import { fromHex } from "viem"
19+
import { fromHex, hexToBytes } from "viem"
2220
import A from "../ui/A.svelte"
2321
2422
const sourceChain = $derived(
@@ -42,6 +40,25 @@ const destinationChain = $derived(
4240
),
4341
),
4442
)
43+
44+
const wasmDetails = AppRuntime.runPromiseExit$(() =>
45+
pipe(
46+
WasmTest,
47+
Effect.andThen((wasm) =>
48+
pipe(
49+
packetDetails.data,
50+
Effect.flatMap((data) =>
51+
pipe(
52+
data.data,
53+
hexToBytes,
54+
wasm.decodePacket,
55+
)
56+
),
57+
)
58+
),
59+
Effect.provide(WasmTest.Default(ucs03Wasm)),
60+
)
61+
)
4562
</script>
4663

4764
{#if Option.isSome(packetDetails.error)}
@@ -222,6 +239,11 @@ const destinationChain = $derived(
222239
<LongMonoWord class="mt-2">{packetDetails.data.value.data}</LongMonoWord>
223240
</div>
224241

242+
<div class="p-4">
243+
<Label>Raw Packet Data (WASM)</Label>
244+
<pre>{wasmDetails.current}</pre>
245+
</div>
246+
225247
{#if Option.isSome(packetDetails.data.value.acknowledgement)}
226248
<div class="p-4">
227249
<Label>Acknowledgement</Label>

app2/svelte.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const config = {
2020
alias: {
2121
"@unionlabs/sdk": "../ts-sdk/src/index.js",
2222
"@unionlabs/sdk/*": "../ts-sdk/src/*",
23+
"@unionlabs/sdk/ucs03.wasm": "../ts-sdk/src/internal/wasm/ucs03-zkgm-packet_bg.wasm",
2324
"@unionlabs/sdk-evm": "../ts-sdk-evm/src/index.js",
2425
"@unionlabs/sdk-evm/*": "../ts-sdk-evm/src/*",
2526
"@unionlabs/sdk-cosmos": "../ts-sdk-cosmos/src/index.js",

app2/vite.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { sveltekit } from "@sveltejs/kit/vite"
22
import tailwindcss from "@tailwindcss/vite"
33
import { svelteTesting } from "@testing-library/svelte/vite"
4+
import * as Path from "node:path"
45
import { defineConfig } from "vite"
56

67
export default defineConfig({
@@ -10,6 +11,16 @@ export default defineConfig({
1011
},
1112
plugins: [sveltekit(), tailwindcss()],
1213
build: { sourcemap: true },
14+
assetsInclude: ["**/*.wasm"],
15+
resolve: {
16+
alias: {
17+
$wasm: Path.resolve(__dirname, "../ts-sdk/src/internal/wasm"),
18+
// "@unionlabs/sdk/ucs03.wasm": Path.resolve(
19+
// __dirname,
20+
// "../ts-sdk/src/internal/wasm/ucs03-zkgm-packet_bg.wasm",
21+
// ),
22+
},
23+
},
1324
server: {
1425
allowedHosts: true,
1526
},

tools/rust/buildWasm.nix

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ let
1919
contractFileNameWithoutExt,
2020
}:
2121
''
22-
${pkgs.binaryen}/bin/wasm-opt target/wasm32-unknown-unknown/wasm-release/${contractFileNameWithoutExt}.wasm -Oz -o ${(crateCargoToml crateDirFromRoot).package.name}
22+
${pkgs.binaryen}/bin/wasm-opt \
23+
target/wasm32-unknown-unknown/wasm-release/${contractFileNameWithoutExt}.wasm \
24+
-Oz -o ${(crateCargoToml crateDirFromRoot).package.name}.wasm
2325
24-
${pkgs.lib.meta.getExe pkgsUnstable.wasm-bindgen-cli_0_2_100} ${(crateCargoToml crateDirFromRoot).package.name} --out-dir $out --typescript --target web
26+
${pkgs.lib.meta.getExe pkgsUnstable.wasm-bindgen-cli_0_2_100} \
27+
${(crateCargoToml crateDirFromRoot).package.name}.wasm \
28+
--out-dir $out \
29+
--typescript \
30+
--target web
2531
'';
2632
cargoBuildExtraArgs =
2733
features:

ts-sdk/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
".": "./src/index.ts",
2323
"./*": "./src/*.ts",
2424
"./examples/*": "./examples/*.ts",
25-
"./internal/*": null
25+
"./internal/*": null,
26+
"./ucs03.wasm": "./src/internal/wasm/ucs03-zkgm-packet_bg.wasm"
2627
},
2728
"scripts": {
2829
"build": "pnpm build-esm && pnpm build-annotate && pnpm build-cjs && build-utils pack-v3",

ts-sdk/src/WasmTest.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Effect } from "effect"
2+
3+
function isNodeRuntime(): boolean {
4+
return typeof process !== "undefined" && !!process.versions?.node
5+
}
6+
7+
export class WasmTest extends Effect.Service<WasmTest>()("WasmTest", {
8+
scoped: Effect.fn(function*(workerUrl?: string) {
9+
const wasm = yield* Effect.tryPromise(() => import("./internal/wasm/ucs03-zkgm-packet.js"))
10+
11+
yield* Effect.log({ wasm })
12+
13+
const wasmUrl = workerUrl ?? new URL(
14+
"./internal/wasm/ucs03-zkgm-packet_bg.wasm",
15+
import.meta.url,
16+
)
17+
18+
if (isNodeRuntime()) {
19+
const { readFile } = yield* Effect.tryPromise(() => import("node:fs/promises"))
20+
const bytes = yield* Effect.tryPromise(() => readFile(wasmUrl))
21+
yield* Effect.tryPromise(() => wasm.default({ module_or_path: bytes }))
22+
} else {
23+
console.log("TRYING TO INSTANTIATE IN BROWSER")
24+
yield* Effect.tryPromise(() => wasm.default(workerUrl))
25+
}
26+
27+
const decodePacket = Effect.fn("decodePacket")(
28+
(packet: Uint8Array) => Effect.try(() => wasm.decode_packet(packet)),
29+
)
30+
31+
const encodePacket = Effect.fn("encodePacket")(
32+
(packet: Uint8Array) => Effect.try(() => wasm.encode_packet(packet)),
33+
)
34+
35+
return {
36+
wasm,
37+
decodePacket,
38+
encodePacket,
39+
} as const
40+
}),
41+
}) {}

ts-sdk/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,11 @@ export * as Staking from "./Staking.js"
181181
* @since 2.0.0
182182
*/
183183
// export * as GasPrice from "./GasPrice.js"
184+
//
185+
186+
/**
187+
* This module provides a WASM-bound decode/encode functionality.
188+
*
189+
* @since 2.0.0
190+
*/
191+
export * as WasmTest from "./WasmTest.js"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
export function decode_packet(packet: Uint8Array): any;
4+
export function encode_packet(packet: any): any;
5+
6+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
7+
8+
export interface InitOutput {
9+
readonly memory: WebAssembly.Memory;
10+
readonly decode_packet: (a: number, b: number, c: number) => void;
11+
readonly encode_packet: (a: number, b: number) => void;
12+
readonly allocate: (a: number) => number;
13+
readonly deallocate: (a: number) => void;
14+
readonly interface_version_8: () => void;
15+
readonly requires_cosmwasm_1_1: () => void;
16+
readonly requires_cosmwasm_1_2: () => void;
17+
readonly requires_cosmwasm_1_3: () => void;
18+
readonly requires_cosmwasm_1_4: () => void;
19+
readonly requires_cosmwasm_2_0: () => void;
20+
readonly requires_iterator: () => void;
21+
readonly requires_staking: () => void;
22+
readonly requires_stargate: () => void;
23+
readonly commit_hash: (a: number) => void;
24+
readonly __wbindgen_export_0: (a: number) => void;
25+
readonly __wbindgen_export_1: (a: number, b: number) => number;
26+
readonly __wbindgen_export_2: (a: number, b: number, c: number, d: number) => number;
27+
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
28+
}
29+
30+
export type SyncInitInput = BufferSource | WebAssembly.Module;
31+
/**
32+
* Instantiates the given `module`, which can either be bytes or
33+
* a precompiled `WebAssembly.Module`.
34+
*
35+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
36+
*
37+
* @returns {InitOutput}
38+
*/
39+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
40+
41+
/**
42+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
43+
* for everything else, calls `WebAssembly.instantiate` directly.
44+
*
45+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
46+
*
47+
* @returns {Promise<InitOutput>}
48+
*/
49+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;

0 commit comments

Comments
 (0)