Token-swap: add tests for all instructions (#455)

* token-swap: Add initialize tests

* Cleanup initialize test using helper struct

* Add deposit tests

* Run cargo fmt

* Add withdraw tests, refactor swap in test

* Add swap tests

* Integrate review feedback
This commit is contained in:
Jon Cinque 2020-09-17 12:11:24 +02:00 committed by GitHub
parent ef31275709
commit 84cf55e9ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 2014 additions and 401 deletions

View File

@ -8,41 +8,56 @@ use thiserror::Error;
#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
pub enum SwapError {
/// The account cannot be initialized because it is already being used.
#[error("AlreadyInUse")]
#[error("Swap account already in use")]
AlreadyInUse,
/// The program address provided doesn't match the value generated by the program.
#[error("InvalidProgramAddress")]
#[error("Invalid program address generated from nonce and key")]
InvalidProgramAddress,
/// The owner of the input isn't set to the program address generated by the program.
#[error("InvalidOwner")]
#[error("Input account owner is not the program address")]
InvalidOwner,
/// The deserialization of the Token state returned something besides State::Token.
#[error("ExpectedToken")]
ExpectedToken,
/// The deserialization of the Token state returned something besides State::Account.
#[error("ExpectedAccount")]
/// The owner of the pool token output is set to the program address generated by the program.
#[error("Output pool account owner cannot be the program address")]
InvalidOutputOwner,
/// The deserialization of the account returned something besides State::Mint.
#[error("Deserialized account is not an SPL Token mint")]
ExpectedMint,
/// The deserialization of the account returned something besides State::Account.
#[error("Deserialized account is not an SPL Token account")]
ExpectedAccount,
/// The initialized pool had a non zero supply.
#[error("InvalidSupply")]
/// The input token account is empty.
#[error("Input token account empty")]
EmptySupply,
/// The pool token mint has a non-zero supply.
#[error("Pool token mint has a non-zero supply")]
InvalidSupply,
/// The initialized token has a delegate.
#[error("InvalidDelegate")]
/// The provided token account has a delegate.
#[error("Token account has a delegate")]
InvalidDelegate,
/// The token swap state is invalid.
#[error("InvalidState")]
InvalidState,
/// The swap info is invalid.
#[error("Swap info invalid")]
InvalidSwapInfo,
/// The input token is invalid for swap.
#[error("InvalidInput")]
InvalidInput,
/// Address of the provided swap token account is incorrect.
#[error("Address of the provided swap token account is incorrect")]
IncorrectSwapAccount,
/// Address of the provided pool token mint is incorrect
#[error("Address of the provided pool token mint is incorrect")]
IncorrectPoolMint,
/// The output token is invalid for swap.
#[error("InvalidOutput")]
InvalidOutput,
/// The calculation failed.
#[error("CalculationFailure")]
CalculationFailure,
/// Invalid instruction number passed in
/// Invalid instruction number passed in.
#[error("Invalid instruction")]
InvalidInstruction,
/// Swap input token accounts have the same mint
#[error("Swap input token accounts have the same mint")]
RepeatedMint,
}
impl From<SwapError> for ProgramError {
fn from(e: SwapError) -> Self {

View File

@ -21,8 +21,8 @@ pub enum SwapInstruction {
/// 1. `[]` $authority derived from `create_program_address(&[Token-swap account])`
/// 2. `[]` token_a Account. Must be non zero, owned by $authority.
/// 3. `[]` token_b Account. Must be non zero, owned by $authority.
/// 4. `[writable]` pool Token. Must be empty, owned by $authority.
/// 5. `[writable]` Pool Account to deposit the generated tokens, user is the owner.
/// 4. `[writable]` Pool Token Mint. Must be empty, owned by $authority.
/// 5. `[writable]` Pool Token Account to deposit the minted tokens. Must be empty, owned by user.
/// 6. '[]` Token program id
Initialize {
/// swap pool fee numerator

File diff suppressed because it is too large Load Diff

View File

@ -43,7 +43,7 @@ impl SwapInfo {
if value.is_initialized {
Ok(value)
} else {
Err(SwapError::InvalidState.into())
Err(SwapError::InvalidSwapInfo.into())
}
}
@ -216,6 +216,6 @@ mod tests {
let unpack_unchecked = SwapInfo::unpack_unchecked(&packed).unwrap();
assert_eq!(unpack_unchecked, swap_info);
let err = SwapInfo::unpack(&packed).unwrap_err();
assert_eq!(err, SwapError::InvalidState.into());
assert_eq!(err, SwapError::InvalidSwapInfo.into());
}
}