Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
7 changes: 5 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions instruction/src/account_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use solana_pubkey::Pubkey;
///
/// [`Instruction`]: crate::Instruction
#[repr(C)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen)]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
Expand Down
37 changes: 36 additions & 1 deletion instruction/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
//! The `Instructions` struct is a legacy workaround
//! from when wasm-bindgen lacked Vec<T> support
//! (ref: https://github.com/rustwasm/wasm-bindgen/issues/111)
use {crate::Instruction, wasm_bindgen::prelude::*};
#![allow(non_snake_case)]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use the attribute if you wanted:

    #[wasm_bindgen(js_name = pushAccount)]
    pub fn push_account(mut self, account_meta: AccountMeta) -> Self

Not sure why one is better than the other though tbh

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically I've found that js_name is more useful for renaming functions totally. Last time I checked this, maybe a year ago, there was no default way to just have functions renamed into camel-case, so I'd prefer to go with this.

use {
crate::{AccountMeta, Instruction},
solana_pubkey::Pubkey,
wasm_bindgen::prelude::*,
};

#[wasm_bindgen]
#[derive(Default)]
Expand All @@ -26,3 +31,33 @@ impl From<Instructions> for std::vec::Vec<Instruction> {
instructions.instructions
}
}

#[wasm_bindgen]
impl Instruction {
/// Create a new `Instruction`
#[wasm_bindgen(constructor)]
pub fn constructor(program_id: Pubkey) -> Self {
Instruction::new_with_bytes(program_id, &[], std::vec::Vec::new())
}

pub fn setData(&mut self, data: &[u8]) {
self.data = data.to_vec();
}

pub fn addAccount(&mut self, account_meta: AccountMeta) {
self.accounts.push(account_meta);
}
}

#[wasm_bindgen]
impl AccountMeta {
/// Create a new writable `AccountMeta`
pub fn newWritable(pubkey: Pubkey, is_signer: bool) -> Self {
AccountMeta::new(pubkey, is_signer)
}

/// Create a new readonly `AccountMeta`
pub fn newReadonly(pubkey: Pubkey, is_signer: bool) -> Self {
AccountMeta::new_readonly(pubkey, is_signer)
}
}
7 changes: 5 additions & 2 deletions sdk-wasm-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ rustdoc-args = ["--cfg=docsrs"]
crate-type = ["cdylib"]

[dependencies]
solana-hash = { workspace = true }
solana-instruction = { workspace = true, features = ["std"] }
solana-program = { workspace = true }
solana-sdk = { workspace = true }
solana-keypair = { workspace = true }
solana-pubkey = { workspace = true, features = ["curve25519", "sha2"] }
solana-transaction = { workspace = true, features = ["bincode", "verify"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = { workspace = true }
console_log = { workspace = true }
getrandom = { version = "0.1.1", features = ["wasm-bindgen"] }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like getRandom does not inherit the newer version in the workspace. Any reason for that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we need it for randomness to work in wasm with the current versions that we use. At least, we needed it a year or so ago, so maybe all of the newer versions work now. I can take a look at it in follow-up work!

log = { workspace = true }
wasm-bindgen = { workspace = true }

Expand Down
17 changes: 3 additions & 14 deletions sdk-wasm-js/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
//! solana-program Javascript interface
#![cfg(target_arch = "wasm32")]

#[deprecated(since = "2.2.0", note = "Use solana_instruction::wasm instead.")]
pub use solana_instruction::wasm as instructions;
use {::log::Level, wasm_bindgen::prelude::*};
pub use {
solana_program::*,
solana_sdk::{
// These imports exist in both solana_sdk and solana_program, so we use
// direct imports to suppress ambiguous re-export warnings.
declare_deprecated_id,
declare_id,
entrypoint,
entrypoint_deprecated,
example_mocks,
hash,
program_stubs,
pubkey,
*,
},
Comment on lines -7 to -20
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guessing this is a breaking change that requires a major version bump?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep!

solana_hash::*, solana_instruction::*, solana_keypair::*, solana_pubkey::*,
solana_transaction::*,
};

// This module is intentionally left empty. The wasm system instruction impl can be
Expand Down
21 changes: 13 additions & 8 deletions sdk-wasm-js/tests/transaction.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { expect } from "chai";
import {
solana_program_init,
AccountMeta,
Pubkey,
Keypair,
Hash,
SystemInstruction,
Instruction,
Instructions,
Transaction,
} from "crate";
solana_program_init();

describe("Transaction", function () {
it("SystemInstruction::Transfer", () => {
it("Instruction", () => {
const payer = Keypair.fromBytes(
new Uint8Array([
241, 230, 222, 64, 184, 48, 232, 92, 156, 210, 229, 183, 154, 251, 5,
Expand All @@ -32,20 +33,24 @@ describe("Transaction", function () {
])
);

const programId = new Pubkey("11111111111111111111111111111111");
const dst = new Pubkey("11111111111111111111111111111112");
const instructionData = new Uint8Array([2, 0, 0, 0, 123, 0, 0, 0, 0, 0, 0, 0]);

const recent_blockhash = new Hash(
const recentBlockhash = new Hash(
"EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k"
);

let instructions = new Instructions();
instructions.push(
SystemInstruction.transfer(src.pubkey(), dst, BigInt(123))
);
let instruction = new Instruction(programId);
instruction.setData(instructionData);
instruction.addAccount(AccountMeta.newWritable(src.pubkey(), true))
instruction.addAccount(AccountMeta.newWritable(dst, false))
instructions.push(instruction);

let transaction = new Transaction(instructions, payer.pubkey());
transaction.partialSign(payer, recent_blockhash);
transaction.partialSign(src, recent_blockhash);
transaction.partialSign(payer, recentBlockhash);
transaction.partialSign(src, recentBlockhash);
expect(transaction.isSigned()).to.be.true;
transaction.verify();

Expand Down
Loading