Skip to content

Commit df1e8da

Browse files
committed
fix(core): set is_native when setting native token accounts
1 parent 99fa247 commit df1e8da

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

crates/core/src/rpc/surfnet_cheatcodes.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,9 @@ impl SurfnetCheatcodes for SurfnetCheatcodesRpc {
12191219
Err(e) => return e.into(),
12201220
};
12211221

1222+
let is_native_mint = mint == spl_token_interface::native_mint::id();
1223+
let token_amount = update.amount.unwrap_or(0);
1224+
12221225
let token_program_id = match some_token_program_str {
12231226
Some(token_program_str) => match verify_pubkey(&token_program_str) {
12241227
Ok(res) => res,
@@ -1245,6 +1248,19 @@ impl SurfnetCheatcodes for SurfnetCheatcodesRpc {
12451248
.inner;
12461249
svm_locker.write_account_update(get_mint_result);
12471250

1251+
let minimum_rent = svm_locker.with_svm_reader(|svm_reader| {
1252+
svm_reader.inner.minimum_balance_for_rent_exemption(
1253+
TokenAccount::get_packed_len_for_token_program_id(&token_program_id),
1254+
)
1255+
});
1256+
1257+
let (rent_exempt_reserve, initial_lamports) = if is_native_mint {
1258+
// For native mint, we need to allocate enough lamports to cover the wrapped SOL amount
1259+
(Some(minimum_rent), minimum_rent + token_amount) // 1 SOL wrapped
1260+
} else {
1261+
(None, minimum_rent)
1262+
};
1263+
12481264
let SvmAccessContext {
12491265
slot,
12501266
inner: mut token_account,
@@ -1253,21 +1269,14 @@ impl SurfnetCheatcodes for SurfnetCheatcodesRpc {
12531269
.get_account(
12541270
&remote_ctx,
12551271
&associated_token_account,
1256-
Some(Box::new(move |svm_locker| {
1257-
let minimum_rent = svm_locker.with_svm_reader(|svm_reader| {
1258-
svm_reader.inner.minimum_balance_for_rent_exemption(
1259-
TokenAccount::get_packed_len_for_token_program_id(
1260-
&token_program_id,
1261-
),
1262-
)
1263-
});
1264-
1265-
let default = TokenAccount::new(&token_program_id, owner, mint);
1272+
Some(Box::new(move |_| {
1273+
let default =
1274+
TokenAccount::new(&token_program_id, owner, mint, rent_exempt_reserve);
12661275
let data = default.pack_into_vec();
12671276
GetAccountResult::FoundAccount(
12681277
associated_token_account,
12691278
Account {
1270-
lamports: minimum_rent,
1279+
lamports: initial_lamports,
12711280
owner: token_program_id,
12721281
executable: false,
12731282
rent_epoch: 0,
@@ -1288,6 +1297,8 @@ impl SurfnetCheatcodes for SurfnetCheatcodesRpc {
12881297

12891298
let final_account_bytes = token_account_data.pack_into_vec();
12901299
token_account.apply_update(|account| {
1300+
// If this is a native mint, we need to adjust the lamports to match the wrapped SOL amount + rent
1301+
account.lamports = initial_lamports;
12911302
account.data = final_account_bytes.clone();
12921303
Ok(())
12931304
})?;

crates/core/src/types.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,19 +591,26 @@ impl TokenAccount {
591591
}
592592
}
593593

594-
pub fn new(token_program_id: &Pubkey, owner: Pubkey, mint: Pubkey) -> Self {
594+
pub fn new(
595+
token_program_id: &Pubkey,
596+
owner: Pubkey,
597+
mint: Pubkey,
598+
is_native: Option<u64>,
599+
) -> Self {
595600
if token_program_id == &spl_token_2022_interface::id() {
596601
Self::SplToken2022(spl_token_2022_interface::state::Account {
597602
mint,
598603
owner,
599604
state: spl_token_2022_interface::state::AccountState::Initialized,
605+
is_native: is_native.into(),
600606
..Default::default()
601607
})
602608
} else {
603609
Self::SplToken(spl_token_interface::state::Account {
604610
mint,
605611
owner,
606612
state: spl_token_interface::state::AccountState::Initialized,
613+
is_native: is_native.into(),
607614
..Default::default()
608615
})
609616
}

0 commit comments

Comments
 (0)