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
33 changes: 22 additions & 11 deletions crates/core/src/rpc/surfnet_cheatcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,9 @@ impl SurfnetCheatcodes for SurfnetCheatcodesRpc {
Err(e) => return e.into(),
};

let is_native_mint = mint == spl_token_interface::native_mint::id();
let token_amount = update.amount.unwrap_or(0);

let token_program_id = match some_token_program_str {
Some(token_program_str) => match verify_pubkey(&token_program_str) {
Ok(res) => res,
Expand All @@ -1245,6 +1248,19 @@ impl SurfnetCheatcodes for SurfnetCheatcodesRpc {
.inner;
svm_locker.write_account_update(get_mint_result);

let minimum_rent = svm_locker.with_svm_reader(|svm_reader| {
svm_reader.inner.minimum_balance_for_rent_exemption(
TokenAccount::get_packed_len_for_token_program_id(&token_program_id),
)
});

let (rent_exempt_reserve, initial_lamports) = if is_native_mint {
// For native mint, we need to allocate enough lamports to cover the wrapped SOL amount
(Some(minimum_rent), minimum_rent + token_amount) // 1 SOL wrapped
} else {
(None, minimum_rent)
};

let SvmAccessContext {
slot,
inner: mut token_account,
Expand All @@ -1253,21 +1269,14 @@ impl SurfnetCheatcodes for SurfnetCheatcodesRpc {
.get_account(
&remote_ctx,
&associated_token_account,
Some(Box::new(move |svm_locker| {
let minimum_rent = svm_locker.with_svm_reader(|svm_reader| {
svm_reader.inner.minimum_balance_for_rent_exemption(
TokenAccount::get_packed_len_for_token_program_id(
&token_program_id,
),
)
});

let default = TokenAccount::new(&token_program_id, owner, mint);
Some(Box::new(move |_| {
let default =
TokenAccount::new(&token_program_id, owner, mint, rent_exempt_reserve);
let data = default.pack_into_vec();
GetAccountResult::FoundAccount(
associated_token_account,
Account {
lamports: minimum_rent,
lamports: initial_lamports,
owner: token_program_id,
executable: false,
rent_epoch: 0,
Expand All @@ -1288,6 +1297,8 @@ impl SurfnetCheatcodes for SurfnetCheatcodesRpc {

let final_account_bytes = token_account_data.pack_into_vec();
token_account.apply_update(|account| {
// If this is a native mint, we need to adjust the lamports to match the wrapped SOL amount + rent
account.lamports = initial_lamports;
account.data = final_account_bytes.clone();
Ok(())
})?;
Expand Down
29 changes: 28 additions & 1 deletion crates/core/src/surfnet/svm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,33 @@ impl SurfnetSvm {
let native_mint_account = inner
.get_account(&spl_token_interface::native_mint::ID)
.unwrap();

let account_associated_data = {
let mint = StateWithExtensions::<spl_token_2022_interface::state::Mint>::unpack(
&native_mint_account.data,
)
.unwrap();
let unix_timestamp = inner.get_sysvar::<Clock>().unix_timestamp;
let interest_bearing_config = mint
.get_extension::<InterestBearingConfig>()
.map(|x| (*x, unix_timestamp))
.ok();
let scaled_ui_amount_config = mint
.get_extension::<ScaledUiAmountConfig>()
.map(|x| (*x, unix_timestamp))
.ok();
let account_associated_data = HashMap::from([(
spl_token_interface::native_mint::ID,
AccountAdditionalDataV3 {
spl_token_additional_data: Some(SplTokenAdditionalDataV2 {
decimals: mint.base.decimals,
interest_bearing_config,
scaled_ui_amount_config,
}),
},
)]);
account_associated_data
};
let parsed_mint_account = MintAccount::unpack(&native_mint_account.data).unwrap();

// Load native mint into owned account and token mint indexes
Expand Down Expand Up @@ -347,7 +374,7 @@ impl SurfnetSvm {
slot_time: DEFAULT_SLOT_TIME_MS,
start_time: SystemTime::now(),
accounts_by_owner,
account_associated_data: HashMap::new(),
account_associated_data,
token_accounts: HashMap::new(),
token_mints,
token_accounts_by_owner: HashMap::new(),
Expand Down
9 changes: 8 additions & 1 deletion crates/core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,19 +591,26 @@ impl TokenAccount {
}
}

pub fn new(token_program_id: &Pubkey, owner: Pubkey, mint: Pubkey) -> Self {
pub fn new(
token_program_id: &Pubkey,
owner: Pubkey,
mint: Pubkey,
is_native: Option<u64>,
) -> Self {
if token_program_id == &spl_token_2022_interface::id() {
Self::SplToken2022(spl_token_2022_interface::state::Account {
mint,
owner,
state: spl_token_2022_interface::state::AccountState::Initialized,
is_native: is_native.into(),
..Default::default()
})
} else {
Self::SplToken(spl_token_interface::state::Account {
mint,
owner,
state: spl_token_interface::state::AccountState::Initialized,
is_native: is_native.into(),
..Default::default()
})
}
Expand Down