Skip to content

Commit

Permalink
Add deposity authority to initialize instruction. Existing tests pass…
Browse files Browse the repository at this point in the history
…ing with new account provided.
  • Loading branch information
VegetarianOrc committed Nov 7, 2021
1 parent 7464794 commit 020a1dd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
8 changes: 8 additions & 0 deletions token-swap/program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ pub enum SwapError {
/// The operation cannot be performed on the given curve
#[error("The operation cannot be performed on the given curve")]
UnsupportedCurveOperation,

// 28.
/// The provided deposit authority did not sign the transaction
#[error("The provided deposit authority did not sign the transaction")]
DepositAuthorityNotSigner,
/// The provided deposit authority is not valid for this pool
#[error("The provided deposit authority is not valid for this pool")]
InvalidDepositAuthority,
}
impl From<SwapError> for ProgramError {
fn from(e: SwapError) -> Self {
Expand Down
5 changes: 4 additions & 1 deletion token-swap/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ pub enum SwapInstruction {
/// Must be empty, not owned by swap authority
/// 6. `[writable]` Pool Token Account to deposit the initial pool token
/// supply. Must be empty, not owned by swap authority.
/// 7. '[]` Token program id
/// 7. `[signer]` deposit_authority. Keypair used to authorize deposits into the pool
/// 8. '[]` Token program id
Initialize(Initialize),

/// Swap the tokens in the pool.
Expand Down Expand Up @@ -339,6 +340,7 @@ pub fn initialize(
pool_pubkey: &Pubkey,
fee_pubkey: &Pubkey,
destination_pubkey: &Pubkey,
deposit_authority: &Pubkey,
fees: Fees,
swap_curve: SwapCurve,
) -> Result<Instruction, ProgramError> {
Expand All @@ -353,6 +355,7 @@ pub fn initialize(
AccountMeta::new(*pool_pubkey, false),
AccountMeta::new_readonly(*fee_pubkey, false),
AccountMeta::new(*destination_pubkey, false),
AccountMeta::new(*deposit_authority, true),
AccountMeta::new_readonly(*token_program_id, false),
];

Expand Down
36 changes: 34 additions & 2 deletions token-swap/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ impl Processor {
let pool_mint_info = next_account_info(account_info_iter)?;
let fee_account_info = next_account_info(account_info_iter)?;
let destination_info = next_account_info(account_info_iter)?;
let deposity_authority_info = next_account_info(account_info_iter)?;
let token_program_info = next_account_info(account_info_iter)?;

let token_program_id = *token_program_info.key;
Expand Down Expand Up @@ -294,6 +295,12 @@ impl Processor {
fees.validate()?;
swap_curve.calculator.validate()?;

// Verify that the deposit authority is a signer to
// ensure Pool creator can deposit
if !deposity_authority_info.is_signer {
return Err(SwapError::DepositAuthorityNotSigner.into());
}

let initial_amount = swap_curve.calculator.new_pool_supply();

Self::token_mint_to(
Expand All @@ -318,7 +325,7 @@ impl Processor {
pool_fee_account: *fee_account_info.key,
fees,
swap_curve,
deposit_authority: Pubkey::new_unique(), //TODO: Properly init from initialize accounts
deposit_authority: *deposity_authority_info.key,
});
SwapVersion::pack(obj, &mut swap_info.data.borrow_mut())?;
Ok(())
Expand Down Expand Up @@ -1130,6 +1137,12 @@ impl PrintProgramError for SwapError {
SwapError::UnsupportedCurveOperation => {
msg!("Error: The operation cannot be performed on the given curve")
}
SwapError::DepositAuthorityNotSigner => {
msg!("Error: The provided deposit authority did not sign the transaction")
}
SwapError::InvalidDepositAuthority => {
msg!("Error: The provided deposit authority is not valid for this pool")
}
}
}
}
Expand All @@ -1156,7 +1169,7 @@ mod tests {
withdraw_all_token_types, withdraw_single_token_type_exact_amount_out,
},
};
use solana_program::{instruction::Instruction, program_stubs, rent::Rent};
use solana_program::{instruction::Instruction, program_stubs, rent::Rent, system_program};
use solana_sdk::account::{create_account_for_test, create_is_signer_account_infos, Account};
use spl_token::{
error::TokenError,
Expand Down Expand Up @@ -1222,6 +1235,8 @@ mod tests {
struct SwapAccountInfo {
bump_seed: u8,
authority_key: Pubkey,
deposit_authority: Pubkey,
deposit_authority_account: Account,
fees: Fees,
swap_curve: SwapCurve,
swap_key: Pubkey,
Expand Down Expand Up @@ -1255,6 +1270,9 @@ mod tests {
let (authority_key, bump_seed) =
Pubkey::find_program_address(&[&swap_key.to_bytes()[..]], &SWAP_PROGRAM_ID);

let deposit_authority = Pubkey::new_unique();
let deposit_authority_account = Account::new(0, 0, &system_program::id());

let (pool_mint_key, mut pool_mint_account) =
create_mint(&spl_token::id(), &authority_key, None);
let (pool_token_key, pool_token_account) = mint_token(
Expand Down Expand Up @@ -1297,6 +1315,8 @@ mod tests {
SwapAccountInfo {
bump_seed,
authority_key,
deposit_authority,
deposit_authority_account,
fees,
swap_curve,
swap_key,
Expand Down Expand Up @@ -1330,6 +1350,7 @@ mod tests {
&self.pool_mint_key,
&self.pool_fee_key,
&self.pool_token_key,
&self.deposit_authority,
self.fees.clone(),
self.swap_curve.clone(),
)
Expand All @@ -1342,6 +1363,7 @@ mod tests {
&mut self.pool_mint_account,
&mut self.pool_fee_account,
&mut self.pool_token_account,
&mut self.deposit_authority_account,
&mut Account::default(),
],
)
Expand Down Expand Up @@ -2414,6 +2436,7 @@ mod tests {
&accounts.pool_mint_key,
&accounts.pool_fee_key,
&accounts.pool_token_key,
&accounts.deposit_authority,
accounts.fees.clone(),
accounts.swap_curve.clone(),
)
Expand All @@ -2426,6 +2449,7 @@ mod tests {
&mut accounts.pool_mint_account,
&mut accounts.pool_fee_account,
&mut accounts.pool_token_account,
&mut accounts.deposit_authority_account,
&mut Account::default(),
],
)
Expand Down Expand Up @@ -2599,6 +2623,7 @@ mod tests {
&accounts.pool_mint_key,
&accounts.pool_fee_key,
&accounts.pool_token_key,
&accounts.deposit_authority,
accounts.fees.clone(),
accounts.swap_curve.clone(),
)
Expand All @@ -2611,6 +2636,7 @@ mod tests {
&mut accounts.pool_mint_account,
&mut accounts.pool_fee_account,
&mut accounts.pool_token_account,
&mut accounts.deposit_authority_account,
&mut Account::default(),
],
&constraints,
Expand Down Expand Up @@ -2670,6 +2696,7 @@ mod tests {
&accounts.pool_mint_key,
&accounts.pool_fee_key,
&accounts.pool_token_key,
&accounts.deposit_authority,
accounts.fees.clone(),
accounts.swap_curve.clone(),
)
Expand All @@ -2682,6 +2709,7 @@ mod tests {
&mut accounts.pool_mint_account,
&mut accounts.pool_fee_account,
&mut accounts.pool_token_account,
&mut accounts.deposit_authority_account,
&mut Account::default(),
],
&constraints,
Expand Down Expand Up @@ -2737,6 +2765,7 @@ mod tests {
&accounts.pool_mint_key,
&accounts.pool_fee_key,
&accounts.pool_token_key,
&accounts.deposit_authority,
accounts.fees,
accounts.swap_curve.clone(),
)
Expand All @@ -2749,6 +2778,7 @@ mod tests {
&mut accounts.pool_mint_account,
&mut accounts.pool_fee_account,
&mut accounts.pool_token_account,
&mut accounts.deposit_authority_account,
&mut Account::default(),
],
&constraints,
Expand Down Expand Up @@ -5706,6 +5736,7 @@ mod tests {
&accounts.pool_mint_key,
&accounts.pool_fee_key,
&accounts.pool_token_key,
&accounts.deposit_authority,
accounts.fees.clone(),
accounts.swap_curve.clone(),
)
Expand All @@ -5718,6 +5749,7 @@ mod tests {
&mut accounts.pool_mint_account,
&mut accounts.pool_fee_account,
&mut accounts.pool_token_account,
&mut accounts.deposit_authority_account,
&mut Account::default(),
],
&constraints,
Expand Down

0 comments on commit 020a1dd

Please sign in to comment.