From eb55c821274a0bf41c553ad7d5fb799a047c198b Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sat, 5 Oct 2024 12:49:23 +0400 Subject: [PATCH] move wasm impl for SystemInstruction to system-instruction crate --- Cargo.lock | 2 + programs/sbf/Cargo.lock | 2 + sdk/program/src/wasm/system_instruction.rs | 115 +------------------- sdk/system-instruction/Cargo.toml | 4 + sdk/system-instruction/src/lib.rs | 2 + sdk/system-instruction/src/wasm.rs | 117 +++++++++++++++++++++ 6 files changed, 130 insertions(+), 112 deletions(-) create mode 100644 sdk/system-instruction/src/wasm.rs diff --git a/Cargo.lock b/Cargo.lock index 4e708cc6069d10..fa2265d55bf5ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8146,6 +8146,7 @@ version = "2.1.0" dependencies = [ "anyhow", "borsh 1.5.1", + "js-sys", "num-traits", "serde", "serde_derive", @@ -8160,6 +8161,7 @@ dependencies = [ "static_assertions", "strum", "strum_macros", + "wasm-bindgen", ] [[package]] diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index 2fc434cc5522fd..91aeb5a6913ef5 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -6835,12 +6835,14 @@ dependencies = [ name = "solana-system-instruction" version = "2.1.0" dependencies = [ + "js-sys", "num-traits", "serde", "serde_derive", "solana-decode-error", "solana-instruction", "solana-pubkey", + "wasm-bindgen", ] [[package]] diff --git a/sdk/program/src/wasm/system_instruction.rs b/sdk/program/src/wasm/system_instruction.rs index 94dd636788092c..e320db37177a6f 100644 --- a/sdk/program/src/wasm/system_instruction.rs +++ b/sdk/program/src/wasm/system_instruction.rs @@ -1,112 +1,3 @@ -//! `SystemInstruction` Javascript interface -#![cfg(target_arch = "wasm32")] -#![allow(non_snake_case)] -use { - crate::{instruction::Instruction, pubkey::Pubkey, system_instruction::*}, - wasm_bindgen::prelude::*, -}; - -#[wasm_bindgen] -impl SystemInstruction { - 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) - } - - 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) - } - - pub fn assign(pubkey: &Pubkey, owner: &Pubkey) -> Instruction { - assign(pubkey, owner) - } - - pub fn assignWithSeed( - pubkey: &Pubkey, - base: &Pubkey, - seed: &str, - owner: &Pubkey, - ) -> Instruction { - assign_with_seed(pubkey, base, seed, owner) - } - - pub fn transfer(from_pubkey: &Pubkey, to_pubkey: &Pubkey, lamports: u64) -> Instruction { - transfer(from_pubkey, to_pubkey, lamports) - } - - 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, - ) - } - - pub fn allocate(pubkey: &Pubkey, space: u64) -> Instruction { - allocate(pubkey, space) - } - - pub fn allocateWithSeed( - address: &Pubkey, - base: &Pubkey, - seed: &str, - space: u64, - owner: &Pubkey, - ) -> Instruction { - allocate_with_seed(address, base, seed, space, owner) - } - - 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() - } - - pub fn advanceNonceAccount(nonce_pubkey: &Pubkey, authorized_pubkey: &Pubkey) -> Instruction { - advance_nonce_account(nonce_pubkey, authorized_pubkey) - } - - 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) - } - - pub fn authorizeNonceAccount( - nonce_pubkey: &Pubkey, - authorized_pubkey: &Pubkey, - new_authority: &Pubkey, - ) -> Instruction { - authorize_nonce_account(nonce_pubkey, authorized_pubkey, new_authority) - } -} +//! Left empty because deleting this module would technically be a breaking change, +//! but there was never anything you could import from here. +// TODO: delete this in next breaking change. diff --git a/sdk/system-instruction/Cargo.toml b/sdk/system-instruction/Cargo.toml index 6972db5ae5e332..33c8cfb188f707 100644 --- a/sdk/system-instruction/Cargo.toml +++ b/sdk/system-instruction/Cargo.toml @@ -23,6 +23,10 @@ solana-instruction = { workspace = true, features = ["bincode", "std"] } solana-logger = { workspace = true, optional = true } solana-pubkey = { workspace = true, default-features = false, features = ["serde"] } +[target.'cfg(target_arch = "wasm32")'.dependencies] +js-sys = { workspace = true } +wasm-bindgen = { workspace = true } + [dev-dependencies] anyhow = { workspace = true } borsh = { workspace = true } diff --git a/sdk/system-instruction/src/lib.rs b/sdk/system-instruction/src/lib.rs index 9ea97150352a72..35d49aa2930b32 100644 --- a/sdk/system-instruction/src/lib.rs +++ b/sdk/system-instruction/src/lib.rs @@ -50,6 +50,8 @@ use { solana_instruction::{AccountMeta, Instruction}, solana_pubkey::Pubkey, }; +#[cfg(target_arch = "wasm32")] +mod wasm; // inline some constants to avoid dependencies const RECENT_BLOCKHASHES_ID: Pubkey = diff --git a/sdk/system-instruction/src/wasm.rs b/sdk/system-instruction/src/wasm.rs new file mode 100644 index 00000000000000..e9d4475a60b1d0 --- /dev/null +++ b/sdk/system-instruction/src/wasm.rs @@ -0,0 +1,117 @@ +//! `SystemInstruction` Javascript interface +#![allow(non_snake_case)] +use { + crate::{ + advance_nonce_account, allocate, allocate_with_seed, assign, assign_with_seed, + authorize_nonce_account, create_account, create_account_with_seed, create_nonce_account, + transfer, transfer_with_seed, withdraw_nonce_account, SystemInstruction, + }, + solana_instruction::Instruction, + solana_pubkey::Pubkey, + wasm_bindgen::prelude::*, +}; + +#[wasm_bindgen] +impl SystemInstruction { + 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) + } + + 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) + } + + pub fn assign(pubkey: &Pubkey, owner: &Pubkey) -> Instruction { + assign(pubkey, owner) + } + + pub fn assignWithSeed( + pubkey: &Pubkey, + base: &Pubkey, + seed: &str, + owner: &Pubkey, + ) -> Instruction { + assign_with_seed(pubkey, base, seed, owner) + } + + pub fn transfer(from_pubkey: &Pubkey, to_pubkey: &Pubkey, lamports: u64) -> Instruction { + transfer(from_pubkey, to_pubkey, lamports) + } + + 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, + ) + } + + pub fn allocate(pubkey: &Pubkey, space: u64) -> Instruction { + allocate(pubkey, space) + } + + pub fn allocateWithSeed( + address: &Pubkey, + base: &Pubkey, + seed: &str, + space: u64, + owner: &Pubkey, + ) -> Instruction { + allocate_with_seed(address, base, seed, space, owner) + } + + 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() + } + + pub fn advanceNonceAccount(nonce_pubkey: &Pubkey, authorized_pubkey: &Pubkey) -> Instruction { + advance_nonce_account(nonce_pubkey, authorized_pubkey) + } + + 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) + } + + pub fn authorizeNonceAccount( + nonce_pubkey: &Pubkey, + authorized_pubkey: &Pubkey, + new_authority: &Pubkey, + ) -> Instruction { + authorize_nonce_account(nonce_pubkey, authorized_pubkey, new_authority) + } +}