Skip to content

Commit 096f8c2

Browse files
jshiohahaAursen
andauthored
Add withNativeMints in node-litesvm (#252)
* expose create-native-mints in node-litesvm * remove token-litesvem changes and dependency * dependency updates after rebase * try adding extra newline to resolve diff * run cargo fmt * bump MACOSX_DEPLOYMENT_TARGET * bump MACOSX_DEPLOYMENT_TARGET to 14.0 * parameterize lto workflow matrix strategy * try turning off lto for failing builds * Revert node-build workflow --------- Co-authored-by: Aursen <[email protected]>
1 parent 40ce84c commit 096f8c2

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

crates/node-litesvm/litesvm/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ export class LiteSVM {
182182
return this;
183183
}
184184

185+
/**
186+
* Adds the native mint accounts for SPL Token and Token-2022, if the programs are loaded.
187+
* @returns The modified LiteSVM instance
188+
*/
189+
withNativeMints(): LiteSVM {
190+
this.inner.withNativeMints();
191+
return this;
192+
}
193+
185194
/**
186195
* Changes the capacity of the transaction history.
187196
* @param capacity - How many transactions to store in history.

crates/node-litesvm/litesvm/internal.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ export declare class LiteSvm {
341341
setSlotHistory(history: SlotHistory): void
342342
getStakeHistory(): StakeHistory
343343
setStakeHistory(history: StakeHistory): void
344+
withNativeMints(): void
344345
}
345346

346347
/** Configuration of network rent. */

crates/node-litesvm/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use {
4040
mod account;
4141
mod compute_budget;
4242
mod feature_set;
43+
mod native_mint;
4344
mod sysvar;
4445
mod transaction_error;
4546
mod transaction_metadata;
@@ -393,4 +394,25 @@ impl LiteSvm {
393394
pub fn set_stake_history(&mut self, history: &StakeHistory) {
394395
self.0.set_sysvar::<StakeHistoryOriginal>(&history.0)
395396
}
397+
398+
#[napi]
399+
pub fn with_native_mints(&mut self) {
400+
if self
401+
.0
402+
.accounts_db()
403+
.inner
404+
.contains_key(&native_mint::inline_spl::SPL_TOKEN_PROGRAM_ID)
405+
{
406+
native_mint::create_native_mint(&mut self.0);
407+
}
408+
409+
if self
410+
.0
411+
.accounts_db()
412+
.inner
413+
.contains_key(&native_mint::inline_spl::SPL_TOKEN_2022_PROGRAM_ID)
414+
{
415+
native_mint::create_native_mint_2022(&mut self.0);
416+
}
417+
}
396418
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use {litesvm::LiteSVM, solana_account::Account, solana_address::Address, solana_rent::Rent};
2+
3+
// Avoid importing external dependencies
4+
pub(crate) mod inline_spl {
5+
use super::*;
6+
7+
pub const SPL_TOKEN_PROGRAM_ID: Address =
8+
Address::from_str_const("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
9+
pub const SPL_TOKEN_2022_PROGRAM_ID: Address =
10+
Address::from_str_const("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb");
11+
}
12+
13+
fn create_native_mint_with_program_id(svm: &mut LiteSVM, address: Address, token_program: Address) {
14+
let account = Account {
15+
lamports: svm.get_sysvar::<Rent>().minimum_balance(82),
16+
data: vec![
17+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20+
],
21+
owner: token_program,
22+
executable: false,
23+
rent_epoch: 0,
24+
};
25+
26+
svm.set_account(address, account).unwrap();
27+
}
28+
29+
pub fn create_native_mint(svm: &mut LiteSVM) {
30+
create_native_mint_with_program_id(
31+
svm,
32+
Address::from_str_const("So11111111111111111111111111111111111111112"),
33+
inline_spl::SPL_TOKEN_PROGRAM_ID,
34+
);
35+
}
36+
37+
pub fn create_native_mint_2022(svm: &mut LiteSVM) {
38+
create_native_mint_with_program_id(
39+
svm,
40+
Address::from_str_const("9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP"),
41+
inline_spl::SPL_TOKEN_2022_PROGRAM_ID,
42+
);
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { NATIVE_MINT, NATIVE_MINT_2022 } from "@solana/spl-token";
2+
import { PublicKey } from "@solana/web3.js";
3+
import { LiteSVM } from "litesvm";
4+
import assert from "node:assert/strict";
5+
import { test } from "node:test";
6+
7+
test("create native mints", () => {
8+
let svm = LiteSVM.default();
9+
10+
assert.strictEqual(svm.getAccount(NATIVE_MINT), null, "SPL Token native mint should not exist");
11+
assert.strictEqual(svm.getAccount(NATIVE_MINT_2022), null, "Token-2022 native mint should not exist");
12+
13+
svm = svm.withSysvars()
14+
.withDefaultPrograms()
15+
.withNativeMints();
16+
17+
const validateData = (data: Uint8Array, mint: PublicKey) => {
18+
assert.ok(data.filter(x => x !== 0).length > 0, `${mint.toBase58()} data should not be empty`);
19+
};
20+
21+
const nativeMint = svm.getAccount(NATIVE_MINT);
22+
assert.ok(nativeMint, "SPL Token native mint should exist");
23+
validateData(nativeMint.data, NATIVE_MINT);
24+
assert.ok(nativeMint.lamports > 0, "SPL Token native mint should have lamports");
25+
26+
const nativeMint2022 = svm.getAccount(NATIVE_MINT_2022);
27+
assert.ok(nativeMint2022, "Token-2022 native mint should exist");
28+
validateData(nativeMint2022.data, NATIVE_MINT_2022);
29+
assert.ok(nativeMint2022.lamports > 0, "Token-2022 native mint should have lamports");
30+
});

0 commit comments

Comments
 (0)