Skip to content

build: Add js Cargo feature flag for WASM builds #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"clients/rust",
"interface",
"interface-wasm-js",
]

[workspace.package]
Expand All @@ -27,3 +28,4 @@ solana-program = { version = "2.2.1", default-features = false }
solana-program-entrypoint = "2.2.1"
solana-program-error = "2.2.1"
solana-pubkey = { version = "2.2.1", default-features = false }
solana-system-interface = { path = "interface" }
27 changes: 27 additions & 0 deletions interface-wasm-js/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "solana-system-interface-wasm-js"
version = "1.0.0"
description = "Instructions and constructors for the System program"
readme = "README.md"
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"]
all-features = true
rustdoc-args = ["--cfg=docsrs"]

[dependencies]
solana-instruction = { workspace = true, features = ["bincode", "std"] }
solana-pubkey = { workspace = true, default-features = false }
solana-system-interface = { workspace = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = { version = "0.3.72" }
wasm-bindgen = { version = "0.2" }

[lib]
crate-type = ["cdylib"]
12 changes: 12 additions & 0 deletions interface-wasm-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<p align="center">
<a href="https://solana.com">
<img alt="Solana" src="https://i.imgur.com/IKyzQ6T.png" width="250" />
</a>
</p>

# Solana System Interface WASM JS

Use the Solana System Interface WASM JS crate to build a JS package with `wasm-pack` usable in Node or browser environments.
See the [Solana System Interface Crate](https://crates.io/crates/solana-system-interface) instead for on-chain programs and client-side applications.

Still have questions? Ask us on [Stack Exchange](https://sola.na/sse)
121 changes: 121 additions & 0 deletions interface-wasm-js/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//! `SystemInstruction` Javascript interface
#![allow(non_snake_case)]
use {
solana_instruction::Instruction,
solana_pubkey::Pubkey,
solana_system_interface::instruction::{
advance_nonce_account, allocate_with_seed, assign_with_seed, authorize_nonce_account,
create_account, create_account_with_seed, create_nonce_account, transfer_with_seed,
withdraw_nonce_account,
},
wasm_bindgen::prelude::*,
};

#[wasm_bindgen]
pub fn createAccount(
from_pubkey: &Pubkey,
to_pubkey: &Pubkey,
lamports: u64,
space: u64,
owner: &Pubkey,
) -> Instruction {
create_account(from_pubkey, to_pubkey, lamports, space, owner)
}

#[wasm_bindgen]
pub fn createAccountWithSeed(
from_pubkey: &Pubkey,
to_pubkey: &Pubkey,
base: &Pubkey,
seed: &str,
lamports: u64,
space: u64,
owner: &Pubkey,
) -> Instruction {
create_account_with_seed(from_pubkey, to_pubkey, base, seed, lamports, space, owner)
}

#[wasm_bindgen]
pub fn assign(pubkey: &Pubkey, owner: &Pubkey) -> Instruction {
solana_system_interface::instruction::assign(pubkey, owner)
}

#[wasm_bindgen]
pub fn assignWithSeed(pubkey: &Pubkey, base: &Pubkey, seed: &str, owner: &Pubkey) -> Instruction {
assign_with_seed(pubkey, base, seed, owner)
}

#[wasm_bindgen]
pub fn transfer(from_pubkey: &Pubkey, to_pubkey: &Pubkey, lamports: u64) -> Instruction {
solana_system_interface::instruction::transfer(from_pubkey, to_pubkey, lamports)
}

#[wasm_bindgen]
pub fn transferWithSeed(
from_pubkey: &Pubkey,
from_base: &Pubkey,
from_seed: String,
from_owner: &Pubkey,
to_pubkey: &Pubkey,
lamports: u64,
) -> Instruction {
transfer_with_seed(
from_pubkey,
from_base,
from_seed,
from_owner,
to_pubkey,
lamports,
)
}

#[wasm_bindgen]
pub fn allocate(pubkey: &Pubkey, space: u64) -> Instruction {
solana_system_interface::instruction::allocate(pubkey, space)
}

#[wasm_bindgen]
pub fn allocateWithSeed(
address: &Pubkey,
base: &Pubkey,
seed: &str,
space: u64,
owner: &Pubkey,
) -> Instruction {
allocate_with_seed(address, base, seed, space, owner)
}

#[wasm_bindgen]
pub fn createNonceAccount(
from_pubkey: &Pubkey,
nonce_pubkey: &Pubkey,
authority: &Pubkey,
lamports: u64,
) -> js_sys::Array {
let instructions = create_nonce_account(from_pubkey, nonce_pubkey, authority, lamports);
instructions.into_iter().map(JsValue::from).collect()
}

#[wasm_bindgen]
pub fn advanceNonceAccount(nonce_pubkey: &Pubkey, authorized_pubkey: &Pubkey) -> Instruction {
advance_nonce_account(nonce_pubkey, authorized_pubkey)
}

#[wasm_bindgen]
pub fn withdrawNonceAccount(
nonce_pubkey: &Pubkey,
authorized_pubkey: &Pubkey,
to_pubkey: &Pubkey,
lamports: u64,
) -> Instruction {
withdraw_nonce_account(nonce_pubkey, authorized_pubkey, to_pubkey, lamports)
}

#[wasm_bindgen]
pub fn authorizeNonceAccount(
nonce_pubkey: &Pubkey,
authorized_pubkey: &Pubkey,
new_authority: &Pubkey,
) -> Instruction {
authorize_nonce_account(nonce_pubkey, authorized_pubkey, new_authority)
}
6 changes: 1 addition & 5 deletions interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ solana-msg = { workspace = true }
solana-program-error = { workspace = true }
solana-pubkey = { workspace = true, default-features = false }

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.72"
wasm-bindgen = "0.2"

[dev-dependencies]
anyhow = "1.0.89"
borsh = { version = "1.5.1", features = ["derive", "unstable__schema"] }
Expand Down Expand Up @@ -59,4 +55,4 @@ frozen-abi = [
serde = ["dep:serde", "dep:serde_derive", "solana-pubkey/serde"]

[lib]
crate-type = ["cdylib", "rlib"]
crate-type = ["rlib"]
2 changes: 0 additions & 2 deletions interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

pub mod error;
pub mod instruction;
#[cfg(target_arch = "wasm32")]
mod wasm;

#[cfg(test)]
static_assertions::const_assert!(MAX_PERMITTED_DATA_LENGTH <= u32::MAX as u64);
Expand Down
118 changes: 0 additions & 118 deletions interface/src/wasm.rs

This file was deleted.