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")]