-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathsync_native.rs
More file actions
148 lines (128 loc) · 4.45 KB
/
sync_native.rs
File metadata and controls
148 lines (128 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
mod setup;
use {
crate::setup::TOKEN_PROGRAM_ID,
mollusk_svm::{result::Check, sysvar::Sysvars, Mollusk},
pinocchio_token_interface::{
native_mint,
state::{
account::Account as TokenAccount, account_state::AccountState, load_mut_unchecked,
load_unchecked,
},
},
solana_account::Account,
solana_program_pack::Pack,
solana_program_test::tokio,
solana_pubkey::Pubkey,
solana_rent::Rent,
solana_sdk_ids::bpf_loader_upgradeable,
};
fn create_token_account(
mint: &Pubkey,
owner: &Pubkey,
is_native: bool,
amount: u64,
program_owner: &Pubkey,
) -> Account {
let space = size_of::<TokenAccount>();
let mut lamports = Rent::default().minimum_balance(space);
let mut data: Vec<u8> = vec![0u8; space];
let token = unsafe { load_mut_unchecked::<TokenAccount>(data.as_mut_slice()).unwrap() };
token.set_account_state(AccountState::Initialized);
token.mint = *mint.as_array();
token.owner = *owner.as_array();
token.set_amount(amount);
token.set_native(is_native);
if is_native {
token.set_native_amount(lamports);
lamports = lamports.saturating_add(amount);
}
Account {
lamports,
data,
owner: *program_owner,
executable: false,
..Default::default()
}
}
/// Creates a Mollusk instance with the default feature set, excluding the
/// `bpf_account_data_direct_mapping` feature.
fn mollusk() -> Mollusk {
let mut mollusk = Mollusk::default();
mollusk.add_program(
&TOKEN_PROGRAM_ID,
"pinocchio_token_program",
&bpf_loader_upgradeable::id(),
);
mollusk
}
#[tokio::test]
async fn sync_native() {
let native_mint = Pubkey::new_from_array(native_mint::ID);
let authority_key = Pubkey::new_unique();
// native account
// - amount: 1_000_000_000
// - lamports: 2_000_000_000
let source_account_key = Pubkey::new_unique();
let mut source_account =
create_token_account(&native_mint, &authority_key, true, 0, &TOKEN_PROGRAM_ID);
source_account.lamports += 2_000_000_000;
let instruction = spl_token_interface::instruction::sync_native_with_rent_sysvar(
&TOKEN_PROGRAM_ID,
&source_account_key,
)
.unwrap();
// Executes the sync_native instruction.
let result = mollusk().process_and_validate_instruction_chain(
&[(&instruction, &[Check::success()])],
&[
(source_account_key, source_account),
Sysvars::default().keyed_account_for_rent_sysvar(),
],
);
result.resulting_accounts.iter().for_each(|(key, account)| {
if *key == source_account_key {
let token_account = spl_token_interface::state::Account::unpack(&account.data).unwrap();
assert_eq!(token_account.amount, 2_000_000_000);
}
});
}
#[test]
fn sync_native_with_rent_change() {
let native_mint = Pubkey::new_from_array(native_mint::ID);
let authority_key = Pubkey::new_unique();
// native account
// - amount: 1_000_000_000
// - lamports: 2_000_000_000
let source_account_key = Pubkey::new_unique();
let lamports = 2_000_000_000;
let source_account = create_token_account(
&native_mint,
&authority_key,
true,
lamports,
&TOKEN_PROGRAM_ID,
);
let instruction =
spl_token_interface::instruction::sync_native(&TOKEN_PROGRAM_ID, &source_account_key)
.unwrap();
// Executes the sync_native instruction.
let mut rent = Rent::default();
rent.lamports_per_byte_year *= 2;
let space = size_of::<TokenAccount>();
let new_rent_exempt_reserve = rent.minimum_balance(space);
let old_rent_exempt_reserve = source_account.lamports - lamports;
let rent_difference = new_rent_exempt_reserve - old_rent_exempt_reserve;
let mut test_mollusk = mollusk();
test_mollusk.sysvars.rent = rent;
let result = test_mollusk.process_and_validate_instruction_chain(
&[(&instruction, &[Check::success()])],
&[(source_account_key, source_account)],
);
result.resulting_accounts.iter().for_each(|(key, account)| {
if *key == source_account_key {
let token_account = unsafe { load_unchecked::<TokenAccount>(&account.data).unwrap() };
assert_eq!(token_account.amount(), lamports - rent_difference);
assert_eq!(token_account.native_amount(), Some(new_rent_exempt_reserve));
}
});
}