forked from solana-program/single-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeposit.rs
More file actions
544 lines (479 loc) · 16.4 KB
/
deposit.rs
File metadata and controls
544 lines (479 loc) · 16.4 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#![allow(clippy::arithmetic_side_effects)]
mod helpers;
use {
helpers::*,
solana_program_test::*,
solana_sdk::{signature::Signer, signer::keypair::Keypair, transaction::Transaction},
solana_stake_interface::state::{Authorized, Lockup},
solana_system_interface::instruction as system_instruction,
spl_associated_token_account_client::address as atoken,
spl_single_pool::{error::SinglePoolError, id, instruction},
test_case::test_case,
};
#[allow(deprecated)]
use spl_single_pool::find_default_deposit_account_address;
#[test_case(true, 0, 0, false, false, false; "activated::minimum_disabled")]
#[test_case(true, 0, 0, false, false, true; "activated::minimum_disabled::small")]
#[test_case(true, 0, 0, false, true, false; "activated::minimum_enabled")]
#[test_case(false, 0, 0, false, false, false; "activating::minimum_disabled")]
#[test_case(false, 0, 0, false, false, true; "activating::minimum_disabled::small")]
#[test_case(false, 0, 0, false, true, false; "activating::minimum_enabled")]
#[test_case(true, 100_000, 0, false, false, false; "activated::extra_none")]
#[test_case(false, 100_000, 0, false, false, false; "activating::extra_none")]
#[test_case(true, 0, 100_000, false, false, false; "activated::none_extra")]
#[test_case(false, 0, 100_000, false, false, false; "activating::none_extra")]
#[test_case(true, 100_000, 100_000, false, false, false; "activated::extra_extra")]
#[test_case(false, 100_000, 100_000, false, false, false; "activating::extra_extra")]
#[test_case(true, 0, 0, true, false, false; "activated::second")]
#[test_case(false, 0, 0, true, false, false; "activating::second")]
#[tokio::test]
async fn success(
activate: bool,
pool_extra_lamports: u64,
alice_extra_lamports: u64,
prior_deposit: bool,
enable_minimum_delegation: bool,
small_deposit: bool,
) {
let mut context = program_test(enable_minimum_delegation)
.start_with_context()
.await;
let accounts = SinglePoolAccounts::default();
accounts
.initialize_for_deposit(
&mut context,
if small_deposit { 1 } else { TEST_STAKE_AMOUNT },
if prior_deposit {
Some(TEST_STAKE_AMOUNT * 10)
} else {
None
},
)
.await;
if activate {
advance_epoch(&mut context).await;
}
if prior_deposit {
let instructions = instruction::deposit(
&id(),
&accounts.pool,
&accounts.bob_stake.pubkey(),
&accounts.bob_token,
&accounts.bob.pubkey(),
&accounts.bob.pubkey(),
);
let transaction = Transaction::new_signed_with_payer(
&instructions,
Some(&context.payer.pubkey()),
&[&context.payer, &accounts.bob],
context.last_blockhash,
);
context
.banks_client
.process_transaction(transaction)
.await
.unwrap();
}
if pool_extra_lamports > 0 {
transfer(
&mut context.banks_client,
&context.payer,
&context.last_blockhash,
&accounts.stake_account,
pool_extra_lamports,
)
.await;
}
if alice_extra_lamports > 0 {
let transaction = Transaction::new_signed_with_payer(
&[system_instruction::transfer(
&accounts.alice.pubkey(),
&accounts.alice_stake.pubkey(),
alice_extra_lamports,
)],
Some(&context.payer.pubkey()),
&[&context.payer, &accounts.alice],
context.last_blockhash,
);
context
.banks_client
.process_transaction(transaction)
.await
.unwrap();
}
let (alice_meta_before_deposit, alice_stake_before_deposit, _) =
get_stake_account(&mut context.banks_client, &accounts.alice_stake.pubkey()).await;
let alice_stake_before_deposit = alice_stake_before_deposit.unwrap().delegation.stake;
let (_, pool_stake_before, pool_lamports_before) =
get_stake_account(&mut context.banks_client, &accounts.stake_account).await;
let pool_stake_before = pool_stake_before.unwrap().delegation.stake;
let instructions = instruction::deposit(
&id(),
&accounts.pool,
&accounts.alice_stake.pubkey(),
&accounts.alice_token,
&accounts.alice.pubkey(),
&accounts.alice.pubkey(),
);
let transaction = Transaction::new_signed_with_payer(
&instructions,
Some(&context.payer.pubkey()),
&[&context.payer, &accounts.alice],
context.last_blockhash,
);
context
.banks_client
.process_transaction(transaction)
.await
.unwrap();
let wallet_lamports_after_deposit =
get_account(&mut context.banks_client, &accounts.alice.pubkey())
.await
.lamports;
let (pool_meta_after, pool_stake_after, pool_lamports_after) =
get_stake_account(&mut context.banks_client, &accounts.stake_account).await;
let pool_stake_after = pool_stake_after.unwrap().delegation.stake;
// when active, the depositor gets their rent and extra back
// but when activating, rent is added to stake
let expected_deposit = if activate {
alice_stake_before_deposit
} else {
alice_stake_before_deposit + alice_meta_before_deposit.rent_exempt_reserve
};
// deposit stake account is closed
assert!(context
.banks_client
.get_account(accounts.alice_stake.pubkey())
.await
.expect("get_account")
.is_none());
// entire stake has moved to pool
assert_eq!(pool_stake_before + expected_deposit, pool_stake_after);
// pool only gained stake, pool kept any extra lamports it had
assert_eq!(pool_lamports_after, pool_lamports_before + expected_deposit);
assert_eq!(
pool_lamports_after,
pool_stake_before
+ expected_deposit
+ pool_meta_after.rent_exempt_reserve
+ pool_extra_lamports,
);
// alice got her rent and extra back if active, or just extra back otherwise
assert_eq!(
wallet_lamports_after_deposit,
USER_STARTING_LAMPORTS - expected_deposit,
);
// alice got tokens. no rewards have been paid so tokens correspond to stake 1:1
assert_eq!(
get_token_balance(&mut context.banks_client, &accounts.alice_token).await,
expected_deposit,
);
}
#[test_case(true, false, false; "activated::minimum_disabled")]
#[test_case(true, false, true; "activated::minimum_disabled::small")]
#[test_case(true, true, false; "activated::minimum_enabled")]
#[test_case(false, false, false; "activating::minimum_disabled")]
#[test_case(false, false, true; "activating::minimum_disabled::small")]
#[test_case(false, true, false; "activating::minimum_enabled")]
#[tokio::test]
async fn success_with_seed(activate: bool, enable_minimum_delegation: bool, small_deposit: bool) {
let mut context = program_test(enable_minimum_delegation)
.start_with_context()
.await;
let accounts = SinglePoolAccounts::default();
let rent = context.banks_client.get_rent().await.unwrap();
let minimum_stake = accounts.initialize(&mut context).await;
#[allow(deprecated)]
let alice_default_stake =
find_default_deposit_account_address(&accounts.pool, &accounts.alice.pubkey());
#[allow(deprecated)]
let instructions = instruction::create_and_delegate_user_stake(
&id(),
&accounts.vote_account.pubkey(),
&accounts.alice.pubkey(),
&rent,
if small_deposit { 1 } else { minimum_stake },
);
let transaction = Transaction::new_signed_with_payer(
&instructions,
Some(&context.payer.pubkey()),
&[&context.payer, &accounts.alice],
context.last_blockhash,
);
context
.banks_client
.process_transaction(transaction)
.await
.unwrap();
if activate {
advance_epoch(&mut context).await;
}
let (_, alice_stake_before_deposit, stake_lamports) =
get_stake_account(&mut context.banks_client, &alice_default_stake).await;
let alice_stake_before_deposit = alice_stake_before_deposit.unwrap().delegation.stake;
let instructions = instruction::deposit(
&id(),
&accounts.pool,
&alice_default_stake,
&accounts.alice_token,
&accounts.alice.pubkey(),
&accounts.alice.pubkey(),
);
let transaction = Transaction::new_signed_with_payer(
&instructions,
Some(&context.payer.pubkey()),
&[&context.payer, &accounts.alice],
context.last_blockhash,
);
context
.banks_client
.process_transaction(transaction)
.await
.unwrap();
let wallet_lamports_after_deposit =
get_account(&mut context.banks_client, &accounts.alice.pubkey())
.await
.lamports;
let (_, pool_stake_after, _) =
get_stake_account(&mut context.banks_client, &accounts.stake_account).await;
let pool_stake_after = pool_stake_after.unwrap().delegation.stake;
let expected_deposit = if activate {
alice_stake_before_deposit
} else {
stake_lamports
};
// deposit stake account is closed
assert!(context
.banks_client
.get_account(alice_default_stake)
.await
.expect("get_account")
.is_none());
// stake moved to pool
assert_eq!(minimum_stake + expected_deposit, pool_stake_after);
// alice got her rent back if active, or everything otherwise
assert_eq!(
wallet_lamports_after_deposit,
USER_STARTING_LAMPORTS - expected_deposit
);
// alice got tokens. no rewards have been paid so tokens correspond to stake 1:1
assert_eq!(
get_token_balance(&mut context.banks_client, &accounts.alice_token).await,
expected_deposit,
);
}
#[test_case(true; "activated")]
#[test_case(false; "activating")]
#[tokio::test]
async fn fail_uninitialized(activate: bool) {
let mut context = program_test(false).start_with_context().await;
let accounts = SinglePoolAccounts::default();
let stake_account = Keypair::new();
let slot = context.genesis_config().epoch_schedule.first_normal_slot + 1;
context.warp_to_slot(slot).unwrap();
create_vote(
&mut context.banks_client,
&context.payer,
&context.last_blockhash,
&accounts.validator,
&accounts.voter.pubkey(),
&accounts.withdrawer.pubkey(),
&accounts.vote_account,
)
.await;
let token_account =
atoken::get_associated_token_address(&context.payer.pubkey(), &accounts.mint);
create_independent_stake_account(
&mut context.banks_client,
&context.payer,
&context.payer,
&context.last_blockhash,
&stake_account,
&Authorized::auto(&context.payer.pubkey()),
&Lockup::default(),
TEST_STAKE_AMOUNT,
)
.await;
delegate_stake_account(
&mut context.banks_client,
&context.payer,
&context.last_blockhash,
&stake_account.pubkey(),
&context.payer,
&accounts.vote_account.pubkey(),
)
.await;
if activate {
advance_epoch(&mut context).await;
}
let instructions = instruction::deposit(
&id(),
&accounts.pool,
&stake_account.pubkey(),
&token_account,
&context.payer.pubkey(),
&context.payer.pubkey(),
);
let transaction = Transaction::new_signed_with_payer(
&instructions,
Some(&context.payer.pubkey()),
&[&context.payer],
context.last_blockhash,
);
let e = context
.banks_client
.process_transaction(transaction)
.await
.unwrap_err();
check_error(e, SinglePoolError::InvalidPoolAccount);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BadDeposit {
User,
Pool,
Onramp,
}
#[test_case(true, BadDeposit::User; "activated::unauth")]
#[test_case(false, BadDeposit::User; "activating::unauth")]
#[test_case(true, BadDeposit::Pool; "activated::pool")]
#[test_case(false, BadDeposit::Pool; "activating::pool")]
#[test_case(true, BadDeposit::Onramp; "activated::onramp")]
#[tokio::test]
async fn fail_bad_account(activate: bool, deposit_source: BadDeposit) {
let mut context = program_test(false).start_with_context().await;
let accounts = SinglePoolAccounts::default();
accounts
.initialize_for_deposit(&mut context, TEST_STAKE_AMOUNT, None)
.await;
if activate {
advance_epoch(&mut context).await;
}
if deposit_source == BadDeposit::Onramp {
let minimum_delegation = get_minimum_delegation(
&mut context.banks_client,
&context.payer,
&context.last_blockhash,
)
.await;
transfer(
&mut context.banks_client,
&context.payer,
&context.last_blockhash,
&accounts.onramp_account,
minimum_delegation,
)
.await;
let instruction = instruction::replenish_pool(&id(), &accounts.vote_account.pubkey());
let transaction = Transaction::new_signed_with_payer(
&[instruction],
Some(&context.payer.pubkey()),
&[&context.payer],
context.last_blockhash,
);
context
.banks_client
.process_transaction(transaction)
.await
.unwrap();
advance_epoch(&mut context).await;
}
let deposit_source_address = match deposit_source {
BadDeposit::User => accounts.alice_stake.pubkey(),
BadDeposit::Pool => accounts.stake_account,
BadDeposit::Onramp => accounts.onramp_account,
};
let instruction = instruction::deposit_stake(
&id(),
&accounts.pool,
&deposit_source_address,
&accounts.alice_token,
&accounts.alice.pubkey(),
);
let transaction = Transaction::new_signed_with_payer(
&[instruction],
Some(&accounts.alice.pubkey()),
&[&accounts.alice],
context.last_blockhash,
);
let e = context
.banks_client
.process_transaction(transaction)
.await
.unwrap_err();
if deposit_source == BadDeposit::User {
check_error(e, SinglePoolError::WrongStakeStake);
} else {
check_error(e, SinglePoolError::InvalidPoolStakeAccountUsage);
}
}
#[test_case(true; "pool_active")]
#[test_case(false; "user_active")]
#[tokio::test]
async fn fail_activation_mismatch(pool_first: bool) {
let mut context = program_test(false).start_with_context().await;
let accounts = SinglePoolAccounts::default();
let minimum_pool_balance = get_minimum_pool_balance(
&mut context.banks_client,
&context.payer,
&context.last_blockhash,
)
.await;
create_vote(
&mut context.banks_client,
&context.payer,
&context.last_blockhash,
&accounts.validator,
&accounts.voter.pubkey(),
&accounts.withdrawer.pubkey(),
&accounts.vote_account,
)
.await;
if pool_first {
accounts.initialize(&mut context).await;
advance_epoch(&mut context).await;
}
create_independent_stake_account(
&mut context.banks_client,
&context.payer,
&context.payer,
&context.last_blockhash,
&accounts.alice_stake,
&Authorized::auto(&accounts.alice.pubkey()),
&Lockup::default(),
minimum_pool_balance,
)
.await;
delegate_stake_account(
&mut context.banks_client,
&context.payer,
&context.last_blockhash,
&accounts.alice_stake.pubkey(),
&accounts.alice,
&accounts.vote_account.pubkey(),
)
.await;
if !pool_first {
advance_epoch(&mut context).await;
accounts.initialize(&mut context).await;
}
let instructions = instruction::deposit(
&id(),
&accounts.pool,
&accounts.alice_stake.pubkey(),
&accounts.alice_token,
&accounts.alice.pubkey(),
&accounts.alice.pubkey(),
);
let transaction = Transaction::new_signed_with_payer(
&instructions,
Some(&accounts.alice.pubkey()),
&[&accounts.alice],
context.last_blockhash,
);
let e = context
.banks_client
.process_transaction(transaction)
.await
.unwrap_err();
check_error(e, SinglePoolError::WrongStakeStake);
}