Skip to content

Commit 18d5d2b

Browse files
authored
interface: include onramp in deposit/withdraw (#396)
1 parent e1f5bd1 commit 18d5d2b

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

clients/js/src/instructions.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type DepositStakeInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
8383
[
8484
ReadonlyAccount<PoolAddress>,
8585
WritableAccount<PoolStakeAddress>,
86+
ReadonlyAccount<PoolOnRampAddress>,
8687
WritableAccount<PoolMintAddress>,
8788
ReadonlyAccount<PoolStakeAuthorityAddress>,
8889
ReadonlyAccount<PoolMintAuthorityAddress>,
@@ -102,6 +103,7 @@ type WithdrawStakeInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
102103
[
103104
ReadonlyAccount<PoolAddress>,
104105
WritableAccount<PoolStakeAddress>,
106+
ReadonlyAccount<PoolOnRampAddress>,
105107
WritableAccount<PoolMintAddress>,
106108
ReadonlyAccount<PoolStakeAuthorityAddress>,
107109
ReadonlyAccount<PoolMintAuthorityAddress>,
@@ -247,8 +249,9 @@ export async function depositStakeInstruction(
247249
userLamportAccount: Address,
248250
): Promise<DepositStakeInstruction> {
249251
const programAddress = SINGLE_POOL_PROGRAM_ID;
250-
const [stake, mint, stakeAuthority, mintAuthority] = await Promise.all([
252+
const [stake, onramp, mint, stakeAuthority, mintAuthority] = await Promise.all([
251253
findPoolStakeAddress(programAddress, pool),
254+
findPoolOnRampAddress(programAddress, pool),
252255
findPoolMintAddress(programAddress, pool),
253256
findPoolStakeAuthorityAddress(programAddress, pool),
254257
findPoolMintAuthorityAddress(programAddress, pool),
@@ -261,6 +264,7 @@ export async function depositStakeInstruction(
261264
accounts: [
262265
{ address: pool, role: AccountRole.READONLY },
263266
{ address: stake, role: AccountRole.WRITABLE },
267+
{ address: onramp, role: AccountRole.READONLY },
264268
{ address: mint, role: AccountRole.WRITABLE },
265269
{ address: stakeAuthority, role: AccountRole.READONLY },
266270
{ address: mintAuthority, role: AccountRole.READONLY },
@@ -284,8 +288,9 @@ export async function withdrawStakeInstruction(
284288
tokenAmount: bigint,
285289
): Promise<WithdrawStakeInstruction> {
286290
const programAddress = SINGLE_POOL_PROGRAM_ID;
287-
const [stake, mint, stakeAuthority, mintAuthority] = await Promise.all([
291+
const [stake, onramp, mint, stakeAuthority, mintAuthority] = await Promise.all([
288292
findPoolStakeAddress(programAddress, pool),
293+
findPoolOnRampAddress(programAddress, pool),
289294
findPoolMintAddress(programAddress, pool),
290295
findPoolStakeAuthorityAddress(programAddress, pool),
291296
findPoolMintAuthorityAddress(programAddress, pool),
@@ -303,6 +308,7 @@ export async function withdrawStakeInstruction(
303308
accounts: [
304309
{ address: pool, role: AccountRole.READONLY },
305310
{ address: stake, role: AccountRole.WRITABLE },
311+
{ address: onramp, role: AccountRole.READONLY },
306312
{ address: mint, role: AccountRole.WRITABLE },
307313
{ address: stakeAuthority, role: AccountRole.READONLY },
308314
{ address: mintAuthority, role: AccountRole.READONLY },

program/src/instruction.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ pub fn deposit_stake(
315315
let accounts = vec![
316316
AccountMeta::new_readonly(*pool_address, false),
317317
AccountMeta::new(find_pool_stake_address(program_id, pool_address), false),
318+
AccountMeta::new_readonly(find_pool_onramp_address(program_id, pool_address), false),
318319
AccountMeta::new(find_pool_mint_address(program_id, pool_address), false),
319320
AccountMeta::new_readonly(
320321
find_pool_stake_authority_address(program_id, pool_address),
@@ -393,6 +394,7 @@ pub fn withdraw_stake(
393394
let accounts = vec![
394395
AccountMeta::new_readonly(*pool_address, false),
395396
AccountMeta::new(find_pool_stake_address(program_id, pool_address), false),
397+
AccountMeta::new_readonly(find_pool_onramp_address(program_id, pool_address), false),
396398
AccountMeta::new(find_pool_mint_address(program_id, pool_address), false),
397399
AccountMeta::new_readonly(
398400
find_pool_stake_authority_address(program_id, pool_address),

program/tests/accounts.rs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ mod helpers;
55

66
use {
77
helpers::*,
8-
solana_instruction::AccountMeta,
98
solana_program_test::*,
109
solana_pubkey::pubkey,
1110
solana_sdk::{
@@ -36,7 +35,7 @@ async fn build_instructions(
3635
context: &mut ProgramTestContext,
3736
accounts: &SinglePoolAccounts,
3837
test_mode: TestMode,
39-
include_onramp: bool,
38+
remove_onramp: bool,
4039
) -> (Vec<Instruction>, usize) {
4140
let initialize_instructions = if test_mode == TestMode::Initialize {
4241
let slot = context.genesis_config().epoch_schedule.first_normal_slot + 1;
@@ -131,29 +130,15 @@ async fn build_instructions(
131130
vec![]
132131
};
133132

134-
if include_onramp {
133+
if remove_onramp {
135134
let instruction = match test_mode {
136135
TestMode::Deposit => deposit_instructions.last_mut().unwrap(),
137136
TestMode::Withdraw => withdraw_instructions.last_mut().unwrap(),
138137
TestMode::Initialize => unreachable!(),
139138
};
140139

141-
if instruction.accounts[2].pubkey == accounts.mint {
142-
instruction.accounts.insert(
143-
2,
144-
AccountMeta {
145-
pubkey: accounts.onramp_account,
146-
is_writable: false,
147-
is_signer: false,
148-
},
149-
);
150-
} else {
151-
panic!(
152-
"this test enforces forwards-compat pre-instruction builder change. \
153-
if you are adding onramp to builders, refactor `include_onramp` to \
154-
be an `exclude_onramp` and test backwards-compat instead"
155-
);
156-
}
140+
assert_eq!(instruction.accounts[2].pubkey, accounts.onramp_account);
141+
instruction.accounts.remove(2);
157142
}
158143

159144
// ints hardcoded to guard against instructions moving with code changes
@@ -172,16 +157,16 @@ async fn build_instructions(
172157

173158
// test that account addresses are checked properly
174159
#[test_case(TestMode::Initialize, false; "initialize")]
175-
#[test_case(TestMode::Deposit, false; "deposit_legacy")]
176-
#[test_case(TestMode::Withdraw, false; "withdraw_legacy")]
177-
#[test_case(TestMode::Deposit, true; "deposit_onramp")]
178-
#[test_case(TestMode::Withdraw, true; "withdraw_onramp")]
160+
#[test_case(TestMode::Deposit, true; "deposit_legacy")]
161+
#[test_case(TestMode::Withdraw, true; "withdraw_legacy")]
162+
#[test_case(TestMode::Deposit, false; "deposit_onramp")]
163+
#[test_case(TestMode::Withdraw, false; "withdraw_onramp")]
179164
#[tokio::test]
180-
async fn fail_account_checks(test_mode: TestMode, include_onramp: bool) {
165+
async fn fail_account_checks(test_mode: TestMode, remove_onramp: bool) {
181166
let mut context = program_test(false).start_with_context().await;
182167
let accounts = SinglePoolAccounts::default();
183168
let (instructions, i) =
184-
build_instructions(&mut context, &accounts, test_mode, include_onramp).await;
169+
build_instructions(&mut context, &accounts, test_mode, remove_onramp).await;
185170
let bad_pubkey = pubkey!("BAD1111111111111111111111111111111111111111");
186171

187172
for j in 0..instructions[i].accounts.len() {
@@ -195,8 +180,7 @@ async fn fail_account_checks(test_mode: TestMode, include_onramp: bool) {
195180

196181
// while onramp is optional, an incorrect onramp misaligns all subsequent accounts
197182
// this is not a problem for the program and causes the mint to fail to validate, but requires tweaking this test
198-
// NOTE once deposit/withdraw require onramp, delete this block
199-
if include_onramp && instruction_pubkey == accounts.pool {
183+
if !remove_onramp && instruction_pubkey == accounts.pool {
200184
if let Some(onramp_account) = instructions[i]
201185
.accounts
202186
.iter_mut()

0 commit comments

Comments
 (0)