0

I'm trying to create a rust wasm crate that takes a TransactionInstruction from js, mutates it, and return it.

I'm trying to use serde-wasm-bindgen to handle the js-wasm interop.

As a test, I have this test wasm function that just takes a TransactionInstruction, deserializes it and then serializes and returns it:

use solana_sdk::instruction::Instruction;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn return_ix(ix: JsValue) -> Result<JsValue, JsValue> {
    let deser: Instruction = serde_wasm_bindgen::from_value(ix)?;
    Ok(serde_wasm_bindgen::to_value(&deser)?)
}

Testing this out on a simple web app with the following js:

import { Keypair, SystemProgram } from "@solana/web3.js";
import * as wasmTest from "wasm-test";

const IX = SystemProgram.createAccount({
  fromPubkey: Keypair.generate().publicKey,
  newAccountPubkey: Keypair.generate().publicKey,
  lamports: 1,
  space: 0,
  programId: Keypair.generate().publicKey,
})

console.log(IX);
const RET = wasmTest.return_ix(IX);
console.log(RET);

However, this throws Uncaught Error: missing field program_id, presumably because the TransactionInstruction object has the field in camelCase programId instead of snake_case program_id

Wondering if there's a way to make these 2 libraries work together without rewriting the solana_sdk structs to have #[serde(rename_all = "camelCase")]

1
  • 1
    As you've noticed, the WASM and JS worlds are currently separated, so they're not meant to interoperate. Feel free to hack on the monorepo and see if you can get things to work though!
    – Jon C
    Commented Jun 20, 2023 at 16:50

1 Answer 1

0

The kaizen project has implemented rust bindings for @solana/web3.js in the solana-web3-sys crate, with TryFrom implemented between the solana-program rust types and @solana/web3.js js types

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.