Skip to content

Commit 14bb4bd

Browse files
authored
Revert "fix(svm): M-01 Deposit Tokens Transferred from Depositor Token Accoun…" (#970)
This reverts commit d6497e3.
1 parent 33bbb4d commit 14bb4bd

14 files changed

+169
-684
lines changed

programs/svm-spoke/src/error.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub enum CommonError {
77
DisabledRoute,
88
#[msg("Invalid quote timestamp!")]
99
InvalidQuoteTimestamp,
10-
#[msg("Invalid fill deadline!")]
10+
#[msg("Ivalid fill deadline!")]
1111
InvalidFillDeadline,
1212
#[msg("Caller is not the exclusive relayer and exclusivity deadline has not passed!")]
1313
NotExclusiveRelayer,
@@ -74,8 +74,6 @@ pub enum SvmError {
7474
InvalidProductionSeed,
7575
#[msg("Invalid remaining accounts for ATA creation!")]
7676
InvalidATACreationAccounts,
77-
#[msg("Invalid delegate PDA!")]
78-
InvalidDelegatePda,
7977
}
8078

8179
// CCTP specific errors.

programs/svm-spoke/src/instructions/deposit.rs

+9-63
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ use crate::{
1111
error::{CommonError, SvmError},
1212
event::FundsDeposited,
1313
state::{Route, State},
14-
utils::{
15-
derive_seed_hash, get_current_time, get_unsafe_deposit_id, transfer_from, DepositNowSeedData, DepositSeedData,
16-
},
14+
utils::{get_current_time, get_unsafe_deposit_id, transfer_from},
1715
};
1816

1917
#[event_cpi]
@@ -25,7 +23,7 @@ use crate::{
2523
output_token: Pubkey,
2624
input_amount: u64,
2725
output_amount: u64,
28-
destination_chain_id: u64
26+
destination_chain_id: u64,
2927
)]
3028
pub struct Deposit<'info> {
3129
#[account(mut)]
@@ -38,9 +36,6 @@ pub struct Deposit<'info> {
3836
)]
3937
pub state: Account<'info, State>,
4038

41-
/// CHECK: PDA derived with seeds ["delegate", seed_hash]; used as a CPI signer.
42-
pub delegate: UncheckedAccount<'info>,
43-
4439
#[account(
4540
seeds = [b"route", input_token.as_ref(), state.seed.to_le_bytes().as_ref(), destination_chain_id.to_le_bytes().as_ref()],
4641
bump,
@@ -88,14 +83,15 @@ pub fn _deposit(
8883
fill_deadline: u32,
8984
exclusivity_parameter: u32,
9085
message: Vec<u8>,
91-
delegate_seed_hash: [u8; 32],
9286
) -> Result<()> {
9387
let state = &mut ctx.accounts.state;
88+
9489
let current_time = get_current_time(state)?;
9590

9691
if current_time.checked_sub(quote_timestamp).unwrap_or(u32::MAX) > state.deposit_quote_time_buffer {
9792
return err!(CommonError::InvalidQuoteTimestamp);
9893
}
94+
9995
if fill_deadline > current_time + state.fill_deadline_buffer {
10096
return err!(CommonError::InvalidFillDeadline);
10197
}
@@ -105,20 +101,21 @@ pub fn _deposit(
105101
if exclusivity_deadline <= MAX_EXCLUSIVITY_PERIOD_SECONDS {
106102
exclusivity_deadline += current_time;
107103
}
104+
108105
if exclusive_relayer == Pubkey::default() {
109106
return err!(CommonError::InvalidExclusiveRelayer);
110107
}
111108
}
112109

113-
// Depositor must have delegated input_amount to the delegate PDA
110+
// Depositor must have delegated input_amount to the state PDA.
114111
transfer_from(
115112
&ctx.accounts.depositor_token_account,
116113
&ctx.accounts.vault,
117114
input_amount,
118-
&ctx.accounts.delegate,
115+
state,
116+
ctx.bumps.state,
119117
&ctx.accounts.mint,
120118
&ctx.accounts.token_program,
121-
delegate_seed_hash,
122119
)?;
123120

124121
let mut applied_deposit_id = deposit_id;
@@ -162,22 +159,6 @@ pub fn deposit(
162159
exclusivity_parameter: u32,
163160
message: Vec<u8>,
164161
) -> Result<()> {
165-
let seed_hash = derive_seed_hash(
166-
&(DepositSeedData {
167-
depositor,
168-
recipient,
169-
input_token,
170-
output_token,
171-
input_amount,
172-
output_amount,
173-
destination_chain_id,
174-
exclusive_relayer,
175-
quote_timestamp,
176-
fill_deadline,
177-
exclusivity_parameter,
178-
message: &message,
179-
}),
180-
);
181162
_deposit(
182163
ctx,
183164
depositor,
@@ -193,7 +174,6 @@ pub fn deposit(
193174
fill_deadline,
194175
exclusivity_parameter,
195176
message,
196-
seed_hash,
197177
)?;
198178

199179
Ok(())
@@ -215,22 +195,7 @@ pub fn deposit_now(
215195
) -> Result<()> {
216196
let state = &mut ctx.accounts.state;
217197
let current_time = get_current_time(state)?;
218-
let seed_hash = derive_seed_hash(
219-
&(DepositNowSeedData {
220-
depositor,
221-
recipient,
222-
input_token,
223-
output_token,
224-
input_amount,
225-
output_amount,
226-
destination_chain_id,
227-
exclusive_relayer,
228-
fill_deadline_offset,
229-
exclusivity_period,
230-
message: &message,
231-
}),
232-
);
233-
_deposit(
198+
deposit(
234199
ctx,
235200
depositor,
236201
recipient,
@@ -240,12 +205,10 @@ pub fn deposit_now(
240205
output_amount,
241206
destination_chain_id,
242207
exclusive_relayer,
243-
ZERO_DEPOSIT_ID, // ZERO_DEPOSIT_ID informs internal function to use state.number_of_deposits as id.
244208
current_time,
245209
current_time + fill_deadline_offset,
246210
exclusivity_period,
247211
message,
248-
seed_hash,
249212
)?;
250213

251214
Ok(())
@@ -269,22 +232,6 @@ pub fn unsafe_deposit(
269232
) -> Result<()> {
270233
// Calculate the unsafe deposit ID as a [u8; 32]
271234
let deposit_id = get_unsafe_deposit_id(ctx.accounts.signer.key(), depositor, deposit_nonce);
272-
let seed_hash = derive_seed_hash(
273-
&(DepositSeedData {
274-
depositor,
275-
recipient,
276-
input_token,
277-
output_token,
278-
input_amount,
279-
output_amount,
280-
destination_chain_id,
281-
exclusive_relayer,
282-
quote_timestamp,
283-
fill_deadline,
284-
exclusivity_parameter,
285-
message: &message,
286-
}),
287-
);
288235
_deposit(
289236
ctx,
290237
depositor,
@@ -300,7 +247,6 @@ pub fn unsafe_deposit(
300247
fill_deadline,
301248
exclusivity_parameter,
302249
message,
303-
seed_hash,
304250
)?;
305251

306252
Ok(())

programs/svm-spoke/src/instructions/fill.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
error::{CommonError, SvmError},
1212
event::{FillType, FilledRelay, RelayExecutionEventInfo},
1313
state::{FillRelayParams, FillStatus, FillStatusAccount, State},
14-
utils::{derive_seed_hash, get_current_time, hash_non_empty_message, invoke_handler, transfer_from, FillSeedData},
14+
utils::{get_current_time, hash_non_empty_message, invoke_handler, transfer_from},
1515
};
1616

1717
#[event_cpi]
@@ -25,12 +25,13 @@ pub struct FillRelay<'info> {
2525
#[account(mut, seeds = [b"instruction_params", signer.key().as_ref()], bump, close = signer)]
2626
pub instruction_params: Option<Account<'info, FillRelayParams>>,
2727

28-
#[account(seeds = [b"state", state.seed.to_le_bytes().as_ref()], bump)]
28+
#[account(
29+
seeds = [b"state", state.seed.to_le_bytes().as_ref()],
30+
bump,
31+
constraint = !state.paused_fills @ CommonError::FillsArePaused
32+
)]
2933
pub state: Account<'info, State>,
3034

31-
/// CHECK: PDA derived with seeds ["delegate", seed_hash]; used as a CPI signer.
32-
pub delegate: UncheckedAccount<'info>,
33-
3435
#[account(
3536
mint::token_program = token_program,
3637
address = relay_data
@@ -80,15 +81,10 @@ pub struct FillRelay<'info> {
8081

8182
pub fn fill_relay<'info>(
8283
ctx: Context<'_, '_, '_, 'info, FillRelay<'info>>,
83-
relay_hash: [u8; 32],
8484
relay_data: Option<RelayData>,
8585
repayment_chain_id: Option<u64>,
8686
repayment_address: Option<Pubkey>,
8787
) -> Result<()> {
88-
// This type of constraint normally would be checked in the context, but had to move it here in the handler to avoid
89-
// exceeding maximum stack offset.
90-
require!(!ctx.accounts.state.paused_fills, CommonError::FillsArePaused);
91-
9288
let FillRelayParams { relay_data, repayment_chain_id, repayment_address } =
9389
unwrap_fill_relay_params(relay_data, repayment_chain_id, repayment_address, &ctx.accounts.instruction_params);
9490

@@ -118,17 +114,15 @@ pub fn fill_relay<'info>(
118114
_ => FillType::FastFill,
119115
};
120116

121-
let seed_hash = derive_seed_hash(&(FillSeedData { relay_hash, repayment_chain_id, repayment_address }));
122-
123-
// Relayer must have delegated output_amount to the delegate PDA
117+
// Relayer must have delegated output_amount to the state PDA
124118
transfer_from(
125119
&ctx.accounts.relayer_token_account,
126120
&ctx.accounts.recipient_token_account,
127121
relay_data.output_amount,
128-
&ctx.accounts.delegate,
122+
state,
123+
ctx.bumps.state,
129124
&ctx.accounts.mint,
130125
&ctx.accounts.token_program,
131-
seed_hash,
132126
)?;
133127

134128
// Update the fill status to Filled, set the relayer and fill deadline

programs/svm-spoke/src/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ pub mod svm_spoke {
235235
/// Authority must be the state.
236236
/// - mint (Account): The mint account for the input token.
237237
/// - token_program (Interface): The token program.
238-
/// - delegate (Account): The account used to delegate the input amount of the input token.
239238
///
240239
/// ### Parameters
241240
/// - depositor: The account credited with the deposit. Can be different from the signer.
@@ -412,10 +411,9 @@ pub mod svm_spoke {
412411
/// - token_program (Interface): The token program.
413412
/// - associated_token_program (Interface): The associated token program.
414413
/// - system_program (Interface): The system program.
415-
/// - delegate (Account): The account used to delegate the output amount of the output token.
416414
///
417415
/// ### Parameters:
418-
/// - relay_hash: The hash identifying the deposit to be filled. Caller must pass this in. Computed as hash of
416+
/// - _relay_hash: The hash identifying the deposit to be filled. Caller must pass this in. Computed as hash of
419417
/// the flattened relay_data & destination_chain_id.
420418
/// - relay_data: Struct containing all the data needed to identify the deposit to be filled. Should match
421419
/// all the same-named parameters emitted in the origin chain FundsDeposited event.
@@ -442,12 +440,12 @@ pub mod svm_spoke {
442440
/// is passed, the caller must load them via the instruction_params account.
443441
pub fn fill_relay<'info>(
444442
ctx: Context<'_, '_, '_, 'info, FillRelay<'info>>,
445-
relay_hash: [u8; 32],
443+
_relay_hash: [u8; 32],
446444
relay_data: Option<RelayData>,
447445
repayment_chain_id: Option<u64>,
448446
repayment_address: Option<Pubkey>,
449447
) -> Result<()> {
450-
instructions::fill_relay(ctx, relay_hash, relay_data, repayment_chain_id, repayment_address)
448+
instructions::fill_relay(ctx, relay_data, repayment_chain_id, repayment_address)
451449
}
452450

453451
/// Closes the FillStatusAccount PDA to reclaim relayer rent.

programs/svm-spoke/src/utils/delegate_utils.rs

-45
This file was deleted.

programs/svm-spoke/src/utils/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pub mod bitmap_utils;
22
pub mod cctp_utils;
3-
pub mod delegate_utils;
43
pub mod deposit_utils;
54
pub mod merkle_proof_utils;
65
pub mod message_utils;
@@ -9,7 +8,6 @@ pub mod transfer_utils;
98

109
pub use bitmap_utils::*;
1110
pub use cctp_utils::*;
12-
pub use delegate_utils::*;
1311
pub use deposit_utils::*;
1412
pub use merkle_proof_utils::*;
1513
pub use message_utils::*;

programs/svm-spoke/src/utils/transfer_utils.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
use crate::{error::SvmError, program::SvmSpoke};
21
use anchor_lang::prelude::*;
32
use anchor_spl::token_interface::{transfer_checked, Mint, TokenAccount, TokenInterface, TransferChecked};
43

4+
use crate::State;
5+
56
pub fn transfer_from<'info>(
67
from: &InterfaceAccount<'info, TokenAccount>,
78
to: &InterfaceAccount<'info, TokenAccount>,
89
amount: u64,
9-
delegate: &UncheckedAccount<'info>,
10+
state: &Account<'info, State>,
11+
state_bump: u8,
1012
mint: &InterfaceAccount<'info, Mint>,
1113
token_program: &Interface<'info, TokenInterface>,
12-
delegate_seed_hash: [u8; 32],
1314
) -> Result<()> {
14-
let (pda, bump) = Pubkey::find_program_address(&[b"delegate", &delegate_seed_hash], &SvmSpoke::id());
15-
if pda != delegate.key() {
16-
return err!(SvmError::InvalidDelegatePda);
17-
}
18-
let seeds: &[&[u8]] = &[b"delegate".as_ref(), &delegate_seed_hash, &[bump]];
19-
let signer_seeds: &[&[&[u8]]] = &[seeds];
2015
let transfer_accounts = TransferChecked {
2116
from: from.to_account_info(),
2217
mint: mint.to_account_info(),
2318
to: to.to_account_info(),
24-
authority: delegate.to_account_info(),
19+
authority: state.to_account_info(),
2520
};
21+
22+
let state_seed_bytes = state.seed.to_le_bytes();
23+
let seeds = &[b"state", state_seed_bytes.as_ref(), &[state_bump]];
24+
let signer_seeds = &[&seeds[..]];
25+
2626
let cpi_context = CpiContext::new_with_signer(token_program.to_account_info(), transfer_accounts, signer_seeds);
2727

2828
transfer_checked(cpi_context, amount, mint.decimals)

0 commit comments

Comments
 (0)